1703G. Good Key, Bad Key(806 (Div. 4))

G. Good Key, Bad Key

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤1051≤n≤105; 0≤k≤1090≤k≤109) — the number of chests and the cost of a good key respectively.

The second line of each test case contains nn integers aiai (0≤ai≤1090≤ai≤109)  — the amount of coins in each chest.

The sum of nn over all test cases does not exceed 105105.

Output

For each test case output a single integer  — the maximum number of coins you can obtain after opening the chests in order from chest 11 to chest nn.

Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example

input

Copy

 

5

4 5

10 10 3 1

1 2

1

3 12

10 10 29

12 51

5 74 89 45 18 69 67 67 11 96 23 59

2 57

85 60

output

Copy

11
0
13
60
58

Note

In the first test case, one possible strategy is as follows:

  • Buy a good key for 55 coins, and open chest 11, receiving 1010 coins. Your current balance is 0+10−5=50+10−5=5 coins.
  • Buy a good key for 55 coins, and open chest 22, receiving 1010 coins. Your current balance is 5+10−5=105+10−5=10 coins.
  • Use a bad key and open chest 33. As a result of using a bad key, the number of coins in chest 33 becomes ⌊32⌋=1⌊32⌋=1, and the number of coins in chest 44 becomes ⌊12⌋=0⌊12⌋=0. Your current balance is 10+1=1110+1=11.
  • Use a bad key and open chest 44. As a result of using a bad key, the number of coins in chest 44 becomes ⌊02⌋=0⌊02⌋=0. Your current balance is 11+0=1111+0=11.

At the end of the process, you have 1111 coins, which can be proven to be maximal.

题意就是说有两把钥匙一把好钥匙需要花钱买来开箱子,用好钥匙开的箱子可以得到里面所有的钱,还有一把坏钥匙,坏钥匙不花钱,但用坏钥匙开箱子会导致所有未开的箱子里的钱都除以2,求怎么样开箱能得到最多钱。

题解,我们可以用贪心的思想,不难发现,如果我们使用坏钥匙的话,之后如果再用好钥匙得到的钱一定不是最多的,所以当使用一次坏钥匙时,之后都只能使用坏钥匙,那么我们只用算出什么时候开始用坏钥匙就行了,有两种做法,一种的是用dp写,还一种就是直接暴力,因为1e9<2的32次方,枚举32次后就都是0了。

因为我不会dp,所以这里只展示暴力代码。

AC code:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int manx = 3e6 + 10;
typedef long long ll;
ll a[manx];
void solve()
{
	ll n,k;
	cin >> n>>k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	ll ans = 0, s = 0;
	for (ll i = 0; i < n; i++)
	{
		ll cs = s;
		for (ll j = i + 1; j <= min(n, i + 32); j++)
		{
			ll cnt = a[j];
			cnt >>= j - i;
			cs += cnt;
		}
		ans = max(cs, ans);
		s += a[i+1] - k;
	}
	ans = max(ans, s);
	cout << ans << endl;
}
int main()
{
	ll t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值