提交时间:2023-08-14 12:20:35
运行 ID: 98124
#include<bits/stdc++.h> using namespace std; const int maxx=2e5+20; int n,k,x; int a[maxx]; int qzh[maxx],mx=-1,mxx=0; int ml,mr,ans,pos,poss; int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>k>>x; for(int i=1;i<=n;i++) { cin>>a[i]; qzh[i]=qzh[i-1]+a[i]; } if(k==0 || x==0) { for(int i=1;i<=n;i++) { if(a[i]-x>0) ans+=a[i]-x; } cout<<ans<<'\n'; } else if(x>0) { for(int i=1;i<=n;i++) { if(qzh[i+k-1]-qzh[i-1]>mx) { mx=qzh[i+k-1]-qzh[i-1]; ml=i,mr=i+k-1; } else if(qzh[i+k-1]-qzh[i-1]==mx) { if(a[i-1]>a[ml-1] || a[i+k]>a[mr]) ml=i,mr=i+k-1; else continue; } } ans=mx+k*x; for(int i=ml-1;i>=1;i--) { pos+=a[i]-x; if(pos>=mxx) mxx=pos; } ans+=mxx,mxx=0; for(int i=mr+1;i<=n;i++) { pos+=a[i]-x; if(pos>=mxx) mxx=pos; } ans+=mxx; cout<<ans<<'\n'; } else if(x<0) { k=n-k; for(int i=1;i<=n;i++) { if(qzh[i+k-1]-qzh[i-1]>mx) { mx=qzh[i+k-1]-qzh[i-1]; ml=i,mr=i+k-1; } else if(qzh[i+k-1]-qzh[i-1]==mx) { if(a[i-1]>a[ml-1] || a[i+k]>a[mr]) ml=i,mr=i+k-1; else continue; } } ans=mx-k*x; for(int i=ml-1;i>=1;i--) { pos+=a[i]+x; if(pos>=mxx) mxx=pos; } ans+=mxx,mxx=0; for(int i=mr+1;i<=n;i++) { pos+=a[i]+x; if(pos>=mxx) mxx=pos; } ans+=mxx; cout<<ans<<'\n'; } return 0; }