提交时间:2023-08-14 12:21:12
运行 ID: 98133
#include<bits/stdc++.h> #define ls (x<<1) #define rs (x<<1)|1 #define int long long using namespace std; const int N = 2e5+2; int n,k,x,ans,inf = 1e16+7; int a[N],t[N<<2],tag[N<<2]; void pushup(int x) { t[x] = min(t[ls],t[rs]); } void pushdown(int x) { if(!tag[x]) return; t[ls] += tag[x]; t[rs] += tag[x]; tag[ls] += tag[x]; tag[rs] += tag[x]; tag[x] = 0; return; } void build(int x,int l,int r) { if(l == r) { t[x] = a[l]; return; } int mid = (l+r)>>1; build(ls,l,mid); build(rs,mid+1,r); pushup(x); return; } void add(int x,int l,int r,int L,int R,int k) { if(L <= l && r <= R) { t[x] += k; tag[x] += k; return; } pushdown(x); int mid = (l+r)>>1; if(L <= mid) add(ls,l,mid,L,R,k); if(R > mid) add(rs,mid+1,r,L,R,k); pushup(x); } int ask(int x,int l,int r,int L,int R) { if(L <= l && r <= R) return t[x]; int mid = (l+r)>>1,ans = inf; if(L <= mid) ans = min(ans,ask(ls,l,mid,L,R)); if(R > mid) ans = min(ans,ask(rs,mid+1,r,L,R)); return ans; } signed main() { scanf("%lld%lld%lld",&n,&k,&x); for(int i = 1;i <= n;i++) { scanf("%lld",&a[i]); a[i] -= x; } // for(int i = 1;i <= n;i++) // printf("%lld ",a[i]); // puts(""); for(int i = 1;i <= n;i++) a[i] += a[i-1]; for(int i = n;i >= 1;i--) a[i+1] = a[i]; // for(int i = 1;i <= n+1;i++) // printf("%lld ",a[i]); // puts(""); a[1] = 0; x = x+x; build(1,1,n+1); if(x > 0) { for(int i = 2;i <= n+1;i++) { int l,r; l = max(1ll,i-k); r = i-1; add(1,1,n+1,l,r,-x); ans = max(ans,a[i]-ask(1,1,n+1,1,i-1)); } } else { for(int i = 2;i <= n+1;i++) { int l,r,d; d = n-i; d = k-d-1; if(d > 0) add(1,1,n+1,1,d,-x); ans = max(ans,a[i]-ask(1,1,n+1,1,i-1)); } } printf("%lld",ans); return 0; }