题目大意:
两把钥匙:选第一把打开时第i个宝箱时,花费K元,选第二把不花钱,但从i到N的宝箱价值减半
如果按从1到N的顺序打开宝箱,问总价值最大是多少;
分析:
假设有X把钥匙是第一把, N - X 把钥匙是第二把,对于X个第一把来说,那负债其实是固定的是K*X,那么我为了让answer最大,那肯定是这N - X 把钥匙集中分布在尾部的,从X 到 N 全是 第二把, 这是我们只要把X从1 到N 枚举,取所有结果的MAX就行, 综上,有下面的AC 代码;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
ll a[maxn];
ll sum[maxn];
vector<ll>vv[maxn];
int main(){
int t;
cin>>t;
while(t--){
int n;
ll k;
cin>>n>>k;
for(int i = 1; i <= n; i++){
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
ll tem = a[i];
while(tem != 0){
vv[i].push_back(tem);
tem = tem/2;
}
}
ll ans = 0;
for(int x = 0; x <= n; x++){
ll diff = sum[x] - k*((long long)x);
for(int i = x + 1; i <= n; i++){
if(i - x > 30){
break;
}
int len = vv[i].size() - 1;
if(i - x <= len){
diff = diff + vv[i][i - x];
}
}
ans = max(ans, diff);
}
cout<<ans<<endl;
for(int i = 1; i <= n; i++) vv[i].clear();
}
return 0;
}