提交时间:2023-08-14 14:23:25
运行 ID: 98291
#include<bits/stdc++.h> #define int long long using namespace std; inline int read() { int f=1,x=0; char c=getchar(); while(c>'9'||c<'0') { if(c=='-') f*=-1; c=getchar(); } while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } const int N=2e5+7; int s[N],n,k,x,ans; int q[N],head,tail,f[N],g[N]; signed main() { n=read(); k=read(); x=read(); if(x<=0) { k=n-k; x=abs(x); } ans=0; for(int i=1;i<=n;i++) { s[i]=read(); f[i]=max(s[i]-x,f[i-1]+s[i]-x); s[i]+=s[i-1]+x; } head=1; tail=0; for(int i=1;i<=n;i++) { while(head<=tail&&s[q[tail]-1]>=s[i-1]) tail--; q[++tail]=i; while(head<=tail&&i-q[head]+1>k) head++; if(head<=tail) { g[i]=max(ans,s[i]-s[q[head]-1]); // cout<<i<<' '<<q[head]<<endl; } g[i]=max(s[i]-s[max(i-k,0ll)]+f[max(i-k,0ll)],g[i]); // cout<<g[i]<<endl; ans=max(ans,g[i]); } printf("%lld",ans); return 0; }