提交时间:2023-08-14 12:24:31

运行 ID: 98166

#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e5+5; int a[N],b[N],c[N],s[N],ans,sum,l,n,k,x; signed main(){ ios::sync_with_stdio(0); cin>>n>>k>>x; for(int i=1;i<=n;++i){ cin>>a[i]; b[i]=a[i]+x; c[i]=a[i]-x; } l=1; for(int i=1;i<=n;++i){ sum+=b[i]; if(i-l+1>k) sum-=b[l],l++; if(sum<0) sum=0,l=i+1; ans=max(ans,sum); } l=1; sum=10000000000000000; if(x>=0){ for(int i=1;i<=n;++i) s[i]=s[i-1]+c[i]; for(int i=1;i<=n;++i){ if(i-k<0) continue; sum=min(s[i-k],sum); ans=max(ans,s[i]-sum+x*k*2); } }else{ k=n-k; for(int i=1;i<=n;++i) s[i]=s[i-1]+b[i]; for(int i=1;i<=n;++i){ if(i-k<0) continue; sum=min(s[i-k],sum); ans=max(ans,s[i]-sum-x*k*2); } } cout<<ans<<endl; return 0; }