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:
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:
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;
- }