CodeForces - 613B Skills

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 nAcfcm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 1090 ≤ 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.

Example

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.

 

题意:

n个技能,m个技能点,每个技能升一级需要1个技能点 每个技能 最高 A 级,现在有个战斗力评分,分数为 cf*满级技能数+cm*最低的技能等级

问:怎么升级技能使得战斗力最高,最高为多少

分析:

对当前技能等级排序,枚举后面的技能多少个升满级的情况,然后用剩下的技能点二分答案看最多能使最低技能到达多少。

二分答案时需要使用前缀和+二分搜索让时间复杂度降到log(n)否则会超时

总时间复杂度 n*log(n)*log(n)

吐槽:

二分写不好真的会被卡啊。。。。。。。

所以推荐阅读:http://blog.csdn.net/turne/article/details/51628640

代码:

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long LL;
LL a[110000],A,cf,cm,m,n;
LL sum[110000];
int binsearch(int k){
	int l=0,r=n-1;
	int mid;
	while(l<=r){
		mid=(l+r)/2;
		if(a[mid]>=k){
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	return r;
}
int search(LL k){
	if(k<=0) return 1;
	LL p=m;
	int t=binsearch(k);
	if(t<0) return 1;
	t++;
	LL xy=k*t-sum[t];
	if(xy<=p) return 1;else return 0;
}
int sx=1;
struct date{
	LL x;
	int index;
	bool operator<(const struct date&b)const{
		if(sx)
		return this->x<b.x;
		else
		return this->index<b.index;
	}
};
date b[110000];
int main(){
	LL ans=0,zans=0;
	cin>>n>>A>>cf>>cm>>m;
	for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
	for(int i=0;i<n;i++) b[i].index=i,b[i].x=a[i];
	sort(b,b+n);
	sort(a,a+n);
	sum[0]=0;
	for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i-1];
	LL pt=0;
	int j;
	int h=n,q=-1;
	for(j=n;m>=0&&j>=0;j--){
		if(j!=n){
		if(a[j]<A) {
		m=m-(A-a[j]);
		a[j]=A;
		}
		if(m<0) break;
		pt++;
		}
		int l=1,r=A,s=0;
		while(l<r){
			if(l==r-1){
				if(search(r)) s=r;
				break;
			}
			int mid=(l+r)/2;
			int ans=search(mid);
			if(ans==1){
				l=mid;
				if(mid>s) s=mid;
			}
			else{
				r=mid;
			}
		}
		ans=cf*pt+cm*s;
		if(ans>zans) {
		zans=ans;
		h=j,q=s;
		}
	}
	printf("%I64d\n",zans);
	for(int i=0;i<n;i++){
		if(b[i].x<q) b[i].x=q;
		if(b[i].x>q) break;
	}
	for(int i=n-1;i>=h;i--){
		b[i].x=A;
	}
	sx=0;
	sort(b,b+n);
	for(int i=0;i<n;i++) printf("%I64d ",b[i].x);
	return 0;
}

  

posted @ 2017-12-06 10:27  晓风微微  阅读(359)  评论(0编辑  收藏  举报