提交时间:2023-08-14 12:24:32
运行 ID: 98169
#include<bits/stdc++.h>//100pts #define int long long using namespace std; int n,k,x; int a[200010],pre[200010]; int f[200010]; int q[200010],h,t; signed main() { cin>>n>>k>>x; for(int i=1;i<=n;i++) cin>>a[i],pre[i]=pre[i-1]+a[i]; if(x<0) { k=n-k; x=-x; } for(int i=0;i<=n;i++) f[i]=pre[i]-x*i; int ans=0; int minn=0; for(int i=k;i<=n;i++) { int res=f[i]-minn+2*k*x; ans=max(res,ans); minn=min(minn,f[i-k+1]); } if(k==0) { cout<<ans<<endl; return 0; } for(int i=0;i<=n;i++) f[i]+=2*i*x; h=1,t=0; q[++t]=0; for(int i=1;i<=n;i++) { while(t<=h&&q[h]<i-k) h--; ans=max(ans,f[i]-f[q[h]]); while(t<=h&&f[q[t]]>f[i]) t++; q[--t]=i; } cout<<ans<<endl; return 0; } /* 4 1 2 2 -1 2 3 6 2 -8 4 -1 9 -3 7 -8 3 0 5 3 2 4 5 1 4 1 9 1 9 8 */