提交时间:2023-08-14 13:56:47

运行 ID: 98269

#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*=-1; } ans=0; for(int i=1;i<=n;i++) { s[i]=read(); g[N]=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) { ans=max(ans,s[i]-s[q[head]-1]); } ans=max(s[i]-s[max(i-k,0ll)]+f[max(i-k,0ll)],ans); } printf("%lld",ans); return 0; }