提交时间:2023-08-14 12:18:55

运行 ID: 98114

#include<bits/stdc++.h> using namespace std; int n,sum; long long a[200005]; long long ans=-0x7f7f7f7f; long long num_1[200005],num_2[200005]; int k,x; int cnt; struct tmp1 { int id,num; bool operator > (const tmp1 & a) const { return num>a.num; } }; priority_queue<tmp1,vector<tmp1>,greater<tmp1> > q; int main() { ios::sync_with_stdio(false); cin>>n>>k>>x; for(int i=1;i<=n;i++){ cin>>a[i]; num_1[i]=num_1[i-1]+a[i]; } for(int i=n;i>=1;i--) { num_2[i]=num_2[i+1]+a[i]; } tmp1 s; s.num=num_1[0],s.id=1; q.push(s); tmp1 l; for(int i=1;i<=n+1;i++) { l=q.top(); //cout<<"l="<<l.num<<endl; if(ans<num_1[n]-num_2[i]-l.num) { //cout<<"num_1[n]="<<num_1[n]<<" num_2[i]="<<num_2[i]<<" l.num="<<l.num; ans=num_1[n]-num_2[i]-l.num; cnt=l.id; sum=i; //cout<<" ans="<<ans<<endl; } l.num=num_1[i],l.id=min(i,n); q.push(l); } if(x>=0){ if(k>=sum-cnt+1) { ans+=(sum-cnt+1)*x; cout<<max(ans,1ll*0); } else { ans+=(2*k-sum+cnt-1)*x; cout<<max(ans,1ll*0); } } else { if(sum-cnt+1+k<=n){ ans-=(sum-cnt+1)*x; cout<<max(ans,1ll*0); } else ans=ans+(k+sum-cnt+1-n)*2*x-(sum-cnt+1)*x; cout<<max(ans,1ll*0); } }