Gym - 103107K

本文主要介绍了一个关于蛋糕的问题,其中Megumi会在Aki的派对上选择并吃掉蛋糕,每次不超过其重量的一半,但她不会吃小于特定重量K的蛋糕。她还可以将两个蛋糕合并成一个更重的蛋糕。问题是要找出她能吃的蛋糕最大总重量。程序通过读取输入,计算符合条件的蛋糕总和,并考虑合并操作,最后输出最大总重量。

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值