提交时间:2023-08-14 15:20:09

运行 ID: 98335

#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,q[N]; 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; sum=0; l=1; int r=1; for(int i=1;i<=n;++i) s[i]=s[i-1]+c[i]; for(int i=1;i<=n;++i){ while(l<r&&i-q[l]>k) l++; ans=max(ans,s[i]-s[q[l]]); while(l<=r&&s[q[r]]>s[i]) r--; q[++r]=i; } sum=10000000000000000; 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; }