CF1703G.Good Key, Bad Key

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn chests. The ii-th chest contains aia_i 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⌋a_i = \lfloor{\frac{a_i}{2}\rfloor}, ai+1=⌊ai+12⌋,…,an=⌊an2⌋a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, \dots, a_n = \lfloor \frac{a_n}{2}\rfloor;
  • 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.

共有 nn 个宝箱。第 ii 个宝箱中包含 aia_i 枚金币。你需要按顺序从第 11 个宝箱到第 nn 个宝箱依次打开全部 nn 个宝箱。

打开一个宝箱时,你可以使用两种类型的钥匙:

  • 优质钥匙:使用一次需花费 kk 枚金币;
  • 劣质钥匙:使用不花费金币,但会使所有尚未打开的宝箱(包括即将打开的当前宝箱)中的金币数量减半(向下取整)。换言之,若用劣质钥匙打开第 ii 个宝箱,则会对所有 j∈[i,n]j \in [i, n] 执行操作 aj=⌊aj2⌋a_j = \left\lfloor \frac{a_j}{2} \right\rfloor;
  • 任意一种钥匙(优质或劣质)均仅能使用一次,即为一次性用品。

你总共需使用 nn 把钥匙,每把钥匙对应打开一个宝箱。初始时,你既没有金币,也没有任何钥匙。若想使用优质钥匙,则必须花钱购买。

在过程中允许透支;例如,若你当前仅有 11 枚金币,仍可购买价值 k=3k = 3 的优质钥匙,此时你的余额将变为 −2-2 枚金币。

求在按顺序从第 11 个宝箱到第 nn 个宝箱全部打开后,你能拥有的最多金币数量。

输入格式

The first line contains a single integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

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

The second line of each test case contains nn integers aia_i (0≤ai≤1090 \leq a_i \leq 10^9) — the amount of coins in each chest.

The sum of nn over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤1051 \leq n \leq 10^5;0≤k≤1090 \leq k \leq 10^9)—— 分别表示宝箱的数量和一把优质钥匙的成本。

每个测试用例的第二行包含 nn 个整数 aia_i(0≤ai≤1090 \leq a_i \leq 10^9)—— 表示每个宝箱中的金币数量。

所有测试用例中 nn 的总和不超过 10510^5。

输出格式

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++).

对于每个测试用例,输出一个整数——即按从宝箱 11 到宝箱 nn 的顺序打开所有宝箱后,你能获得的最多金币数量。

请注意,某些测试用例的答案无法用 32 位整数类型表示,因此在编程语言中你应至少使用 64 位整数类型(例如 C++ 中的 long long)。

输入输出样例

  • 输入#1

    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

    输出#1

    11
    0
    13
    60
    58

说明/提示

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\left\lfloor \frac{3}{2} \right\rfloor = 1, and the number of coins in chest 44 becomes ⌊12⌋=0\left\lfloor \frac{1}{2} \right\rfloor = 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\left\lfloor \frac{0}{2} \right\rfloor = 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.

在第一个测试用例中,一种可能的策略如下:

  • 花费 55 枚硬币购买一把好钥匙,打开宝箱 11,获得 1010 枚硬币。此时你的余额为 0+10−5=50 + 10 - 5 = 5 枚硬币。
  • 再花费 55 枚硬币购买一把好钥匙,打开宝箱 22,获得 1010 枚硬币。此时你的余额为 5+10−5=105 + 10 - 5 = 10 枚硬币。
  • 使用一把坏钥匙打开宝箱 33。由于使用了坏钥匙,宝箱 33 中的硬币数变为 ⌊32⌋=1\left\lfloor \frac{3}{2} \right\rfloor = 1,而宝箱 44 中的硬币数变为 ⌊12⌋=0\left\lfloor \frac{1}{2} \right\rfloor = 0。此时你的余额为 10+1=1110 + 1 = 11。
  • 使用一把坏钥匙打开宝箱 44。由于使用了坏钥匙,宝箱 44 中的硬币数变为 ⌊02⌋=0\left\lfloor \frac{0}{2} \right\rfloor = 0。此时你的余额为 11+0=1111 + 0 = 11。

整个过程结束后,你共拥有 1111 枚硬币,可以证明这是最大值。

输入解题思路,AI测评打分。不知道怎么写?

首页