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

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 and gcd (see notes section for more information) of numbers in the list is 1.

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 integers x 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 17
1 17 17 16
Output
40
Input
10 6 2
100 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值