Codeforces Round #432 B. Arpa and a list of numbers

来源:互联网 发布:风力太阳能路灯 知乎 编辑:程序博客网 时间:2024/05/18 23:55
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty andgcd (see notes section for more information) of numbers in the list is1.

Arpa can perform two types of operations:

  • Choose a number and delete it with cost x.
  • Choose a number and increase it by 1 with cost y.

Arpa can apply these operations to as many numbers as he wishes, and he is allowed to apply the second operation arbitrarily many times on the same number.

Help Arpa to find the minimum possible cost to make the list good.

Input

First line contains three integers n, x and y (1 ≤ n ≤ 5·105,1 ≤ x, y ≤ 109) — the number of elements in the list and the integersx and y.

Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the list.

Output

Print a single integer: the minimum possible cost to make the list good.

Examples
Input
4 23 171 17 17 16
Output
40
Input
10 6 2100 49 71 73 66 96 8 60 41 63
Output
10
Note

In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).


这个题我居然错了5次。全是1错了一次,于是加了个if;超时了一次,把cin换成了scanf并且在if里加了等号。

这题的意思是,坏序列:非空且最大公约数为1.我们可以对每个数执行两种操作,加1或者删除。可重复加1,每种操作分别花费a,b,现在在花费最小的情况下让当前数组不是坏序列。

这题没什么巧的。两重循环少不了。既然要让最大公约数不为1,那么我们就从2开始到最大可能值一个一个试吧。如果当前最大公约数是2,那么对每一个数选择最优处理,加起来得到一个代价。对不同最大公约数的最小代价即为所求。这里我们考虑一次剪枝,如果所有不合格的数花费的最小代价依然没有当前结果小,那么就不用计算了。


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const LL maxn=1000086;LL n,a,b;LL val[maxn];LL flag[maxn];LL asd[maxn];int main(){    cin>>n>>a>>b;    memset(val,0,sizeof(val));    memset(flag,0,sizeof(flag));    LL mi = __INT64_MAX__ ;    LL ma = 0;    for(LL i=0;i<n;i++)    {        scanf("%d",&asd[i]);        flag[asd[i]]++;        if(asd[i]<mi&&asd[i]!=1)            mi=asd[i];        if(asd[i]>ma)            ma=asd[i];    }    if(ma==1)    {        cout<< min(a,b)*n <<endl;    }    else{        LL ans = __INT64_MAX__;        for(LL i=2;i<=ma;i++)        {            if(val[i]) continue;            LL tem = flag[i];            for(LL j = i+i;j<=ma;j+=i)            {                val[j]=1;                tem+=flag[j];            }            //这里考虑一下剪枝            if( (n-tem)*min(a,b) >= ans )    continue;            LL ca=0;            for(LL j=0;j<n;j++)                 if(asd[j]%i)                        ca+=(a>b*(i-asd[j]%i)) ? b*(i-asd[j]%i) : a;            ans = min(ans,ca);        }        cout<<ans<<endl;    }    return 0;}



阅读全文
0 0
原创粉丝点击