链接:

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.

思路:

1,用了坏key,就要一直用,

2,坏key最多有log2(1e9)=30次左右,暴力模拟选最优

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")//
const int maxj=2e5+100,mod=1e9+7;
int a[maxj];
void solve(){int n,k;cin>>n>>k;for(int i=1;i<=n;++i){cin>>a[i];}int sum=0;int ans=0;a[0]=k;for(int i=0;i<n;++i){int cc=sum;for(int j=i+1;j<=min(n,i+32);++j){int cnt=a[j];cnt>>=j-i;cc+=cnt;}ans=max(ans,cc);sum+=a[i+1]-k;}ans=max(sum,ans);cout<<ans<<'\n';
}
signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;cin>>t;// t=1;while(t--)solve();return 0;
}

G. Good Key, Bad Key(思维)相关推荐

  1. python 学习笔记 - for循环: 字典遍历, 分别打印key, value, key:value

    #遍历字典, 分别打印key, value, key:value emp = {'name':'Tom', 'age':20, 'salary' : 8800.00}for k in emp.keys ...

  2. 出圈问题(java)-----n个人围成一圈,数到key或者key的倍数,出圈,问剩下的最后一个人原来的位置是多少?

    出圈问题---n个人围成一圈,数到key或者key的倍数,出圈,问剩下的最后一个人原来的位置是多少? java版 1.n个人围成一圈,说明它是重复循环的,头就是尾. 2.每次数key或者key的倍数, ...

  3. candidate key, primary key, superkey的区别

    理解前觉得很复杂 理解后觉得很简单 简单的东西简单记录 他们的共同点是:都能确定一个relation里每个tuple的唯一性 关系: superkey包括candidate key, candidat ...

  4. #{key}和${key} 取值的区别

    mybatis中 #{key}和${key} 取值的区别 mapper映射文件 <select id="selectUser" resultType="com.rp ...

  5. can not load key value key was removed or redis-server went away 关于spring 整合redis 以及保存到redis

    spring 整合redis 1. 引用依赖 <!--Redis--> <dependency><groupId>org.springframework.boot& ...

  6. G. Good Key, Bad Key(暴力)

    Problem - 1703G - Codeforces 有n个箱子.第i个箱子里有ai个硬币.你需要按顺序打开所有n个箱子,从箱子1到箱子n. 你可以用两种类型的钥匙来打开箱子. 一把好钥匙,使用它 ...

  7. codeforce:G. Good Key, Bad Key【贪心】

    分析 有两个操作,我们不知道选哪个 我们就考虑先a后b和先b后a # x y # good and bad: x + y // 2 - k # bad and good: x // 2 + y // ...

  8. CHIL-ORACLE-主外键约束(primary key / foreign key)

    主键约束 要求主键列数据唯一,并且不允许为空 外键约束 用于量表建立关系,需要指定引用朱彪的那列(主表必须是主键)1.主键约束 ( primary key )--例如1:create table te ...

  9. 微信支付 商户Key 支付Key API密钥 的获取

    读了微信支付的开发文档,感觉是不同阶段,不同的同学写的,有些专业名词比较混乱,甚至还会有错别字,以及接口更新了,而文档不更新的情况. 使用微信支付,必须要用到 api密钥进行签名 其中 ,商户Key ...

最新文章

  1. B-JUI(Best jQuery UI) 前端框架
  2. 优化MySQL数据类型——《深究MySQL》
  3. 在TCP客户端连接成功的回调函数里,无法访问到客户端套接字的明细
  4. 教你玩转CSS 分组选择器和嵌套选择器
  5. springboot-添加拦截器
  6. packageinfo.java_package-info.java文件详解
  7. Hive 外部表关联分区数据
  8. 《零基础》MySQL GROUP BY 语句(十九)
  9. 嵌入式软件工程师笔试题
  10. 十大经典排序算法动画演示
  11. 对软件工程实践课程的预定目标
  12. html和vue的区别,2018-09-16第四课 (v-html和v-text的区别,v-once和v-pre的区别,vue的生命周期,选项卡)...
  13. PHP中各种Hash算法性能比较
  14. Java获取世界各国各城市代码_获取世界各国、全国省份、城市、县
  15. 基于FPGA的四轴运动控制IC设计(verilog)
  16. 十大模拟炒黄金白银的软件
  17. OpenGL教程——windows安装openGL
  18. 16岁的雅虎问答,因“不再受欢迎”将永久关闭
  19. 计算机中vlookup函数公式,Excel表中VLOOKUP函数使用方法和操作实例
  20. 含8的数字的个数 (10分)

热门文章

  1. CYQ.Blog(QBlog) 单用户版本V3.0 发布下载[免费,简洁,高性能,双语言,8套皮肤,4种数据库支持]...
  2. 单应性矩阵在标定中的应用
  3. JavaScript——字符串对象
  4. C语言笔记(1.3版本,目前3.5w字)----未完待续
  5. 并行,串行,流水线,线程和进程傻傻分不清楚
  6. dl4j+fnlp关联度TopN
  7. 5.2 数据可视化分析——词云图绘制
  8. Python缩进快捷键
  9. ZoomEye:网络空间搜索引擎
  10. php中soap使用,SoapServer服务端编写,SoapClient客户端编写