调了一下午,注意k为0时候下标会偏移都后一个。
贪的时候,x为整数显然是贪连续k,负数则贪心放置于头尾。
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
ll tt,n,k,x,a[(int)2e5+9],dp[(int)2e5+9],sum[(int)2e5+9];
void solve(){
cin>>n>>k>>x;
f(i,1,n)cin>>a[i];
multiset<ll> s;
s.insert(0);
for(int i=1;i<=n;++i)sum[i]=sum[i-1]+a[i]-x;
ll ans=0;
for(int i=1;i<=n;++i){
for(int j=max(i-k+1ll,1ll);j<=i;++j){
ll sz=i-j+1ll;
ll qsb=max(2ll*x*sz,max(0ll,k-(n-sz))*2ll*x);
ans=max(ans,sum[i]-sum[j-1]+qsb);
}
if(i>=k){
ll sz=max(0ll,k-(n-i));
ll qsb=max(2ll*x*k,2ll*sz*x);
ans=max(ans,sum[i]-*s.begin()+qsb);
ll mi=max(k-1ll,0ll);
s.insert(sum[i-mi]);
ll msz=min(((ll)s.size())-1,sz);
s.erase(s.find(0));
for(int j=1;j<=msz;++j){
ans=max(ans,sum[i]-*s.begin()+2ll*(sz-j)*x);
auto it=s.find(sum[j]);
s.erase(it);
}
s.insert(0);
for(int j=1;j<=msz;++j)s.insert(sum[j]);
}
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
f(sb,1,tt){
solve();
}
return 0;
}