提交时间:2023-08-14 12:38:06
运行 ID: 98232
#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e5+5,inf=2e9+9; int n,k,x,a[N],ans; int L,R,K,I,sz; struct tree{ int l,r,len; int ls,rs; int w,lw,rw,sum; }tr[N<<1]; inline void chg(int id,int x) {tr[id].w+=x,tr[id].lw=tr[id].rw=tr[id].sum=tr[id].w;} inline tree pushup(tree now,tree lt,tree rt){ now.sum=lt.sum+rt.sum; now.lw=max(lt.lw,lt.sum+rt.lw); now.rw=max(rt.rw,rt.sum+lt.rw); now.w=max(max(lt.w,rt.w),lt.rw+rt.lw); return now; } inline void build(int l,int r,int id){ tr[id]=(tree){l,r,r-l+1,0}; if(l==r){tr[id].w=0,chg(id,a[l]);return;} int ls=++sz,rs=++sz,mid=l+(r-l>>1); build(l,mid,tr[id].ls=ls); build(mid+1,r,tr[id].rs=rs); tr[id]=pushup(tr[id],tr[ls],tr[rs]); } inline void upd(int id){ int l=tr[id].l,r=tr[id].r; if(l>I||r<I)return; if(l==r){chg(id,K);return;} int ls=tr[id].ls,rs=tr[id].rs; upd(ls),upd(rs); tr[id]=pushup(tr[id],tr[ls],tr[rs]); } inline tree qry(int id){ int l=tr[id].l,r=tr[id].r; if(L<=l&&r<=R)return tr[id]; int ls=tr[id].ls,rs=tr[id].rs,mid=l+(r-l>>1); if(L<=mid&&r>mid)return pushup(tr[id],qry(ls),qry(rs)); if(L<=mid)return qry(ls);if(r>mid)return qry(rs); } signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>k>>x; for(int i=1; i<=n; ++i) cin>>a[i];build(1,n,++sz); for(int i=1; i<=n; ++i) {I=i;if(i>k)K=-x;else K=x;upd(1);} L=1,R=n,ans=max(ans,qry(1).w); for(int i=2; i<=n-k+1; ++i){ I=i-1,K=-2*x,upd(1); I=i+k-1,K=2*x,upd(1); L=1,R=n,ans=max(ans,qry(1).w); }cout<<ans<<'\n';return 0; }