提交时间:2023-08-14 12:26:29
运行 ID: 98183
#include<bits/stdc++.h> #define LL long long #define N 200005 using namespace std; int n,k;LL x,s[N],ans1,ans2; int q[N],l,r;LL mn; int main(){ scanf("%d%d%lld",&n,&k,&x); for(int i(1);i<=n;++i) scanf("%lld",&s[i]),s[i]+=s[i-1]; if(x<0) k=n-k,x=-x; q[l=r=1]=0; for(int i(1);i<=n;++i){ while(l<=r&&s[i]+i*x<=s[q[r]]+q[r]*x) --r;q[++r]=i; while(l<=r&&i-q[l]>k) ++l; ans1=max(ans1,s[i]+i*x-s[q[l]]-q[l]*x); } for(int i(k);i<=n;++i){ mn=min(mn,s[i-k]-(i-k)*x); ans2=max(ans2,s[i]-i*x-mn+2*k*x); } printf("%lld\n",max(ans1,ans2)); return 0; }