提交时间:2023-08-14 15:02:17
运行 ID: 98321
#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]; struct que{ int wei,date; }q[N]; int head,tail; 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; } } // for(int i=1;i<=n;i++)cout<<pre[i]<<" "; // cout<<endl; int se=0; 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]+max(f[i-k].sum,se)+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; } } head=1,tail=0; for(int i=n;i>=1;i--){//枚举 l while(head<=tail&&q[head].wei>i+k-1)head++; ans=max(ans,pre[q[head].wei]-pre[i-1]); while(head<=tail&&pre[q[head].wei]>pre[q[tail].wei]){ tail--; } q[++tail].wei=i; } cout<<ans; }