提交时间:2023-08-14 12:28:34

运行 ID: 98215

#include<bits/stdc++.h> using namespace std; #define N 200001 #define int long long int n,k,x,mx[N<<2],tag[N<<2],sum[N]; #define lc (p<<1) #define rc (p<<1|1) #define mid (l+r>>1) void pushup(int p){ mx[p]=max(mx[lc],mx[rc]); } void build(int p,int l,int r){ if(l==r){ mx[p]=sum[l];return; } build(lc,l,mid);build(rc,mid+1,r); pushup(p); } void pushdown(int p,int l,int r){ if(tag[p]&&(l!=r)){ mx[lc]+=tag[p];mx[rc]+=tag[p]; tag[lc]+=tag[p];tag[rc]+=tag[p]; tag[p]=0; } } void modify(int p,int l,int r,int L,int R,int x){ if(L>R)return; if(L<=l&&r<=R){ mx[p]+=x;tag[p]+=x;return; } pushdown(p,l,r); if(L<=mid)modify(lc,l,mid,L,R,x); if(R>mid)modify(rc,mid+1,r,L,R,x); pushup(p); } int query(int p,int l,int r,int L,int R){ if(L<=l&&r<=R){ return mx[p]; } pushdown(p,l,r); int ans=INT_MIN; if(L<=mid)ans=max(ans,query(lc,l,mid,L,R)); if(R>mid)ans=max(ans,query(rc,mid+1,r,L,R)); return ans; } int ans; signed main(){ scanf("%lld%lld%lld",&n,&k,&x); if(x<0){ k=n-k;x=-x; } for(int i=1;i<=n;++i){ int a;scanf("%lld",&a); if(i<=k)a+=x; else a-=x; sum[i]=sum[i-1]+a; } build(1,1,n); for(int i=1;i<=n;++i){ int now=query(1,1,n,i,n); if(i>1)now-=query(1,1,n,i-1,i-1);ans=max(ans,now); modify(1,1,n,i,min(n,i+k-1),-2*x); } printf("%lld\n",ans); return 0; }