提交时间:2023-08-14 12:27:03

运行 ID: 98194

#include<bits/stdc++.h> using namespace std; #define ll long long template<typename T>inline void read(T &ret){ ret=0;T fh=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')fh=-1;ch=getchar();} while(isdigit(ch))ret=ret*10+(ch^48),ch=getchar(); ret*=fh; } const int N=2e5+5; ll n,k,x,a[N],sum[N],mx=-2e9,ans; ll q[N],l,r; void solve(){ static ll b[N],aans,ssum; for(int i=1;i<=n-k+1;++i){ for(int j=1;j<=n;++j) if(i<=j&&j<=i+k-1)b[j]=a[j]+x; else b[j]=a[j]-x; for(int bl=1;bl<=n;++bl){ ssum=0; for(int br=bl;br<=n;++br) aans=max(aans,ssum+=b[br]); } } printf("%lld\n",aans); } inline ll work(int i,int j){ return x*min(i-q[j],k)-x*max(0ll,i-q[j]-k); } int main(){ read(n);read(k);read(x); if(x<0)x=-x,k=n-k; for(int i=1;i<=n;++i){ read(a[i]); mx=max(mx,a[i]); sum[i]=sum[i-1]+a[i]; } if(n<=100){solve();return 0;} ans=k?mx+x:mx-x; if(ans<=0)return puts("0"),0; for(int i=1;i<=n;++i){ while(l<r&&sum[l]-work(i,l)>=sum[l+1]-work(i,l+1))++l; ans=max(ans,sum[i]-sum[q[l]]+work(i,l)); while(l<=r&&sum[q[r]]-work(i+1,r)>=sum[i]-(k?x:-x))--r; q[++r]=i; } printf("%lld\n",ans); return 0; }