提交时间:2023-08-14 16:42:16

运行 ID: 98377

#include<bits/stdc++.h> using namespace std; #define ll long long #define N 200005 ll sum[N]; ll a[N]; deque<ll>q; int main(){ ll n,k,x; scanf("%lld%lld%lld",&n,&k,&x); if(x<0)x=-x,k=n-k; for(int i=1;i<=n;i++) scanf("%lld",&a[i]),a[i]-=x,sum[i]=sum[i-1]+a[i]; ll ans=0,minn=0; q.push_back(0); for(ll i=1;i<=n;i++){ if(i>=k){ minn=min(minn,sum[i-k]); ans=max(ans,sum[i]-minn+2ll*x*k); } if(!q.empty()&&i-q.front()+1>k)q.pop_front(); while(!q.empty()&&sum[q.back()]+2ll*q.back()*x>sum[i]+2ll*i*x)q.pop_back(); q.push_back(i); ans=max(ans,sum[i]+2ll*i*x-(sum[q.front()]+2ll*q.front()*x)); } printf("%lld",ans); return 0; }