CodeForces 639D Bear and Contribution

time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.

Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.

  • Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5.
  • Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1.

Note that it's possible that Limak reads blogs faster than comments.

Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.

How much time does Limak need to achieve his goal?

Input

The first line contains four integers nkb and c (2 ≤ k ≤ n ≤ 200 000, 1 ≤ b, c ≤ 1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.

The second line contains n integers t1, t2, ..., tn (|ti| ≤ 109) where ti denotes contribution of the i-th registered user.

Output

Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.

Examples
input
4 3 100 30
12 2 6 1
output
220
input
4 3 30 100
12 2 6 1
output
190
input
6 2 987 789
-8 42 -4 -65 -8 -8
output
0
Note

In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows.

  • He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6.
  • Then he spends 4·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1).

In the given scenario, Limak spends 100 + 4·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6.

In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3·30 = 190 minutes:

  • Spend 2·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12.

  • Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.

 有n个数字,要使至少有k个相同,可以花费b使一个数+5,可以花费c使一个数+1,求最小花费。
最后达到的数肯定是原来的数中[v,v+4]的范围,所以求出这5n个数的花费即可。
从左到右求,记录模5为[0,4]的五种情况和花费,即可快速转移。
#include<iostream>
#include<stack>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int mod=1e9+7;
const int N=200005;
int n,k;
long long b,c;
long long t[N],s[N];
long long a[5][N],pre[5],pr[5];
int vis[5][5],num[5];
int main()
{
    int i,j,ii,q;
    long long ans;
    long long now;
    while(scanf("%d%d%I64d%I64d",&n,&k,&b,&c)!=EOF)
    {
        for(i=1; i<=n; i++) scanf("%I64d",&t[i]);
        ans=1000000000;
        ans*=ans;
        sort(t+1,t+1+n);
        for(i=1; i<=n; i++) t[i]+=1000000000;
        s[0]=0;
        for(i=1; i<=n; i++) s[i]=s[i-1]+t[i];
        for(i=k; i<=n; i++) ans=min(k*t[i]-(s[i]-s[i-k]),ans);
        for(i=0; i<5; i++) pre[i]=-1,pr[i]=-ans;
        long long tp;
        ans*=c;
        for(i=0; i<5; i++) for(j=0; j<5; j++) vis[i][j]=1;
        for(i=1; i<=k; i++)
        {
            j=t[i]%5;
            num[j]++;
            a[j][num[j]]=t[i];
        }
        for(now=t[k]; now<t[k]+5; now++)
        {
            j=now%5;
            pr[j]=now;
            pre[j]=0;
            for(ii=0; ii<5; ii++)
            {
                for(q=vis[j][ii]; q<=num[ii]; q++)
                {
                    pre[j]+=(now-a[ii][q])/5*b+(now-a[ii][q])%5*c;
                }
            }
            ans=min(ans,pre[j]);
        }
        for(; i<=n; i++)
        {
            j=t[i]%5;
            num[j]++;
            a[j][num[j]]=t[i];
            for(now=t[i]; now<t[i]+5; now++)
            {
                j=now%5;
                long long mx=-1,mi=-1;
                for(ii=0; ii<5; ii++)
                {
                    if(vis[j][ii]<=num[ii])
                    {
                        tp=(pr[j]-a[ii][vis[j][ii]])/5*b+(pr[j]-a[ii][vis[j][ii]])%5*c;
                        if(tp>mx) mx=tp,mi=ii;
                    }
                }
                vis[j][mi]++;
                pre[j]-=mx;
                pre[j]+=(now-pr[j])/5*(k-1)*b+(now-t[i])*c;
                pr[j]=now;
                ans=min(ans,pre[j]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(CodeForces 639D Bear and Contribution)