提交时间:2023-08-14 15:47:55

运行 ID: 98351

#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],r; 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;r=0; for(int i=1;i<=n;++i) s[i]=s[i-1]+b[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; } 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; r=1; memset(q,0,sizeof(q)); 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; }