Educational Codeforces Round 43 Well played!

Well played!

Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:

Max owns n creatures, i-th of them can be described with two numbers — its health hpi and its damage dmgi. Max also has two types of spells in stock:

  1. Doubles health of the creature (hpi := hpi·2);
  2. Assigns value of health of the creature to its damage (dmgi := hpi).

Spell of first type can be used no more than a times in total, of the second type — no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.

Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.

Input

The first line contains three integers nab (1 ≤ n ≤ 2·1050 ≤ a ≤ 200 ≤ b ≤ 2·105) — the number of creatures, spells of the first type and spells of the second type, respectively.

The i-th of the next n lines contain two number hpi and dmgi (1 ≤ hpi, dmgi ≤ 109) — description of the i-th creature.

Output

Print single integer — maximum total damage creatures can deal.

Examples
input
Copy
2 1 1
10 15
6 1
output
Copy
27
input
Copy
3 0 3
10 8
7 11
5 2
output
Copy
26
Note

In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6·2 = 27.

In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.


题意:每个魔兽都有health和damage,1操作共a次,每次可以把health翻倍,2操作共b次,每次可以把health转化为damage,

问你最终你可以得到的总damage是多少.

思路:火攻心,肯定是我们把a次都用在一个怪物上是最好的,假如不是最好的,我们对1魔兽用m次,2魔兽用n次,那么肯定可以通过只对某个魔兽使用达到更好的效果(因为是翻倍操作),所以我们就可以枚举对那个魔兽使用.

预处理魔兽,让h-d最大且大于0的先转化,然后做d的和,然后枚举每个魔兽的的时候如果操作a次后发现

①不值得转化,h<d,枚举下一个

②值得转化且此前分配给他b操作了,直接转化算总和

③值得转化且此前没分配给他b操作,还剩余有b操作,直接转化算总和

④值得转化且此前没分配给他b操作,没有b操作了,把转化率最低的那个分配给他(这个也可以预处理得到),求总和

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double esp = 1e-12;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;
struct node
{
	ll h,d;
	int mk;
} p[maxn];

ll n,a,b;

bool cmp(node x,node y)
{
	return x.h-x.d< y.h-y.d;
}

ll quick_pow(ll x,ll y)
{
	ll ans = 1;
	while(y)
	{
		if(y&1)
			ans*= x;
		x*= x;
		y>>= 1;	
	}
	
	return ans;
}

int main()
{
	mem(p,0);
	cin>>n>>a>>b;
	
	for(int i = 1;i<= n;i++)
		scanf("%lld %lld",&p[i].h,&p[i].d);
	
	sort(p+1,p+n+1,cmp);
	
	int last = n+1;//记录转化率最低的魔兽 
	for(int i = n;i>= 1;i--)
	{
		if(p[i].h-p[i].d> 0&&b> 0)
			p[i].mk = 1,b--;
		else
			break;
		last--;
	}
	
	ll sum = 0;
	for(int i = 1;i<= n;i++)
		if(p[i].mk)
			sum+= p[i].h;
		else
			sum+= p[i].d;
	
	ll ans = sum;
	for(int i = 1;i<= n;i++)
	{
		ll tmp = p[i].h*quick_pow(2,a);
		
		ll d = 0;
		if(p[i].mk)
			d = sum-p[i].h+tmp;
		else if(tmp> p[i].d)
		{
			if(b> 0)
				d = sum-p[i].d+tmp;
			else if(last<= n)
				d = sum-p[last].h+p[last].d-p[i].d+tmp;
		}
		ans = max(ans,d);
	}
	
	cout<<ans<<endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/80158290