K. Keep Eating
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Megumi is a girl who likes to eat.
Today there will be a party at Aki's home. There are NN cakes for Megumi of different weight on the table. She will choose one cake and eat no more than half of it every time. But out of conscience, she won't eat those cakes whose weight is smaller than KK.
However, she knows a magic to merge two cakes. That means she can put two cakes together and combine them into a larger cake whose weight is equal to the sum of weight of previous cakes. She can do this operation every time.
Now Aki wants to know the maximum total weight of cakes Megumi can eat. Please note that she can eat as many times as she can, and each time the weight she eat is a positive integer no more than the half of the weight of cake.
Input
The first line contains two integers N,K (1≤N≤200000; 2≤K≤106)N,K (1≤N≤200000; 2≤K≤106).
The second line contains NN integers a1,a2,⋯,an (1≤ai≤106)a1,a2,⋯,an (1≤ai≤106) denoting the weight of each cake.
Output
Output one integer denoting the maximum total weight of cakes she can eat.
Example
input
5 10 9 8 7 10 10
output
39
#include<iostream>
using namespace std;
#include<algorithm>
int main()
{
long long sum=0,sum1=0,n,k,i,w,cout=0;//注意这,得用long long.
scanf("%lld %lld",&n,&k);
for(i=0;i<n;i++)
{
scanf("%lld",&w);
if(w>=k)
{
sum=sum+w-k;
cout++;
}
else
{
sum1=sum1+w;
}
}
if(sum1>=k)
{
sum=sum+sum1-k;
sum=sum+cout*k+k/2;
}
else{
if(cout==1)
{
sum=sum+sum1+k/2;
}
else if(cout==0)
{
}
else
{
sum=sum+sum1+(cout-1)*k+k/2;
}
}
printf("%lld",sum);
return 0;
}
本文主要介绍了一个关于蛋糕的问题,其中Megumi会在Aki的派对上选择并吃掉蛋糕,每次不超过其重量的一半,但她不会吃小于特定重量K的蛋糕。她还可以将两个蛋糕合并成一个更重的蛋糕。问题是要找出她能吃的蛋糕最大总重量。程序通过读取输入,计算符合条件的蛋糕总和,并考虑合并操作,最后输出最大总重量。

被折叠的 条评论
为什么被折叠?



