cf#806G

题目

There are nn chests. The ii-th chest contains aiai coins. You need to open all nn chests in order from chest 11 to chest nn.

There are two types of keys you can use to open a chest:

  • a good key, which costs kk coins to use;
  • a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest ii will do ai=⌊ai2⌋ai=⌊ai2⌋, ai+1=⌊ai+12⌋,…,an=⌊an2⌋ai+1=⌊ai+12⌋,…,an=⌊an2⌋;
  • any key (both good and bad) breaks after a usage, that is, it is a one-time use.

You need to use in total nn keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.

During the process, you are allowed to go into debt; for example, if you have 11 coin, you are allowed to buy a good key worth k=3k=3 coins, and your balance will become −2−2 coins.

Find the maximum number of coins you can have after opening all nn chests in order from chest 11 to chest nn.

解答

注意初始化

#include <iostream>
using namespace std;
 const int N=2e5+4;
 #define int long long
 int inf=1e18;
 int a[N],n,K,w[34];
 int f[N][34];
 
 void solve(){
 	int i,j;
 	f[0][0]=0;
 	for(i=0;i<=n;i++)
 	 for(j=0;j<=32;j++) f[i][j]=-inf;
 	 f[0][0]=0;
 	 
 	for(i=1;i<=n;i++){
 		for(j=0;j<=32&&j<=i;j++){
 			f[i][j]=max(f[i][j],f[i-1][j]+a[i]/w[j]-K);
 			if(j>=1) f[i][j]=max(f[i][j],f[i-1][j-1]+a[i]/w[j]);
 			if(j>=31) f[i][j]=max(f[i][j],f[i-1][j]);
		 }
	 }
	 int ans=0;
	 for(j=0;j<=32;j++){
	 	ans=max(ans,f[n][j]);
	 }
	 cout<<ans<<endl;
 }
 signed main(){
 	w[0]=1;
 	int i,cas;
 	for(i=1;i<=32;i++) w[i]=w[i-1]*2;
 	cin>>cas;
 	
 	while(cas--){
 	cin>>n>>K;
 		for(i=1;i<=n;i++) cin>>a[i];
 		solve();
 	}
 }
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值