Codeforces Round #339 (Div. 2) D. Skills

D. Skills
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.

Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:

  • The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
  • The minimum skill level among all skills (min ai), multiplied by coefficient cm.

Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.

Input

The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ cf, cm ≤ 1000, 0 ≤ m ≤ 1015).

The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.

Output

On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.

On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.

Sample test(s)
Input
3 5 10 1 5
1 3 1
Output
12
2 5 2 
Input
3 5 10 1 339
1 3 1
Output
35
5 5 5 
Note

In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.

In the second test one should increase all skills to maximum.

题意:第一行告诉你5个数n,A,cf,cm,m;

第二行为n个数;

让你对这些数进行操作,每次操作可以把一个数加1,操作数不超过m,并且每个数不超过A。现在记操作后最大的数A的个数为maxNum,最小的值为minval,让你求maxNum*cf+minval*cm的最大值。

分析:我们可以通过确定最大数A的个数,让后把其余的操作数留给最小值,确定最小值时不能瞎暴力,提示一下:假使现在有num个最小值minval,剩余操作数为M,那么这num个数要一起向上加(因为只顾一个的话最小值还是不会变,都变了最小值才会改变),所以最小值可以变为minval+M/num;这个还是很好理解的。此题还需要各种细节,好的姿势才会敲得轻松,不在累述。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
struct node
{
    LL num,id;///记录值和位置,方便最后输出结果
}a[100010];
bool cmp1(node x,node y)///让num从大到小排列
{
    return x.num>y.num;
}
bool cmp2(node x,node y)///输出结果时用
{
    return x.id<y.id;
}
LL sum[100010],ne[100010],need[100010];
int main()
{
    LL n,A,cf,cm,m;
    while(scanf("%lld%lld%lld%lld%lld",&n,&A,&cf,&cm,&m)!=EOF)
    {
        for(LL i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].num);
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp1);
        sum[0]=0;
        for(LL i=1;i<=n;i++) sum[i]=sum[i-1]+A-a[i].num;///预处理前i个数填满到A需要多少次
        ne[n]=0;
        for(LL i=n-1;i>=1;i--) ne[i]=ne[i+1]+(a[i].num-a[i+1].num)*(n-i);///假如当前数为num,要把比它小的所有数填到num这个数那么多需要ne[i]次
        for(LL i=1;i<=n;i++) need[i]=ne[n-i+1];///这里只是为了二分查找而反过来了,要不然是递减序列
        LL ans=-1,minval,minnum,maxnum;
        for(LL i=0;i<=n;i++)
        {
            LL M=m;
            if(sum[i]>M) break;
            if(i==n)///特殊处理全为A的情况
            {
                LL tem=n*cf+A*cm;
                if(ans<tem)
                {
                    ans=tem;
                    minnum=n;
                    minval=A;
                    maxnum=n;
                }
                break;
            }
            M-=sum[i];
            LL pos=upper_bound(need+1,need+n+1,M)-need;
            pos--;
            if(pos>=n-i)
            {
                 LL x=n-i;
                 M-=need[x];
                 LL y=a[i+1].num+M/x;
                 LL tem=i*cf+min(y,A)*cm;///取min是保证不大于A而已
                 if(tem>ans)
                 {
                     maxnum=i;
                     ans=tem;
                     minnum=x;
                     minval=min(y,A);
                 }
            }
            else
            {
                LL x=pos;
                M-=need[pos];
                LL y=a[n-pos+1].num+M/x;
                LL tem=i*cf+y*cm;
                if(tem>ans)
                {
                    maxnum=i;
                    ans=tem;
                    minnum=x;
                    minval=y;
                }
            }
        }
        printf("%lld\n",ans);
        for(LL i=1;i<=maxnum;i++) a[i].num=A;///前maxnum个数都为A
        for(LL i=n;i>=n-minnum+1;i--) a[i].num=minval;///最后minnum个数为minval
        sort(a+1,a+n+1,cmp2);
        for(LL i=1;i<n;i++) printf("%lld ",a[i].num);
        printf("%lld\n",a[n].num);
    }
    return 0;
}

你可能感兴趣的:(Codeforces Round #339 (Div. 2) D. Skills)