提交时间:2023-08-14 12:28:02

运行 ID: 98210

#include<bits/stdc++.h>//T2 using namespace std; const int N=200011; #define int long long int n,k,x; int a[N]; struct node{ int sum,l; }f[N]; int pre[N]; signed main(){ cin>>n>>k>>x; if(x<0){ x=-x; k=n-k; } for(int i=1;i<=n;i++)cin>>a[i]; int ans=0; //l>k for(int i=1;i<=n;i++)a[i]-=x; for(int i=1;i<=n;i++){ pre[i]=pre[i-1]+a[i]; if(f[i-1].sum>0){ f[i].sum=f[i-1].sum+a[i]; f[i].l=f[i-1].l; } else{ f[i].sum=a[i]; f[i].l=i; } // cout<<f[i].sum<<" "<<f[i].l<<endl; } // for(int i=1;i<=n;i++)cout<<pre[i]<<" "; // cout<<endl; for(int i=k;i<=n;i++){ // cout<<i<<" "<<pre[i]-pre[i-k]+max(f[i-k].sum,0)+2*k*x<<endl; ans=max(ans,pre[i]-pre[i-k]+f[i-k].sum+2*k*x); }//l<=k // cout<<ans<<endl; for(int i=1;i<=n;i++)a[i]+=2*x; for(int i=1;i<=n;i++){ pre[i]=pre[i-1]+a[i]; if(f[i-1].sum>0){ f[i].sum=f[i-1].sum+a[i]; f[i].l=f[i-1].l; } else{ f[i].sum=a[i]; f[i].l=i; } } for(int i=1;i<=n;i++){ if(i-f[i].l+1>k){ ans=max(ans,pre[i]-pre[i-k]); } else ans=max(ans,f[i].sum); } cout<<ans; }