提交时间:2023-08-14 12:21:23

运行 ID: 98137

#include<bits/stdc++.h> #define int long long using namespace std; const int Maxn=2e5+5; int a[Maxn],sum[Maxn],res[Maxn]; int n,k,x; signed main() { //freopen("data.txt","r",stdin); //freopen("ans2.txt","w",stdout); ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>k>>x; if(x<0) k=n-k,x=-x; for(int i=1;i<=n;i++) { cin>>a[i]; sum[i]=sum[i-1]+a[i]; } int pos=0,val=0; for(int i=1;i<=n-k+1;i++) { int rec=sum[i+k-1]-sum[i-1]; if(val<rec) val=rec,pos=i; } int val1=0,val2=0; for(int i=pos-1;i>=1;i--) { val1=max(val1,sum[i]-i*x); } for(int i=pos+k;i<=n;i++) { val2=max(val2,sum[i]-sum[pos+k-1]-(i-pos-k+1)*x); } cout<<val+k*x+val1+val2<<'\n'; return 0; } /* 4 -1 9 -3 7 -8 9 12 5 12+32=44 */