提交时间:2023-08-14 11:57:38

运行 ID: 98064

#include<bits/stdc++.h> using namespace std; #define ll long long ll a[200010],ans[200010],cnt; inline int ls(int x) { return x<<1; } inline int rs(int x) { return x<<1|1; } void pushup(int p) { ans[p]=ans[ls(p)]+ans[rs(p)]; //cout<<p<<" "<<ans[p]<<endl; } void build(int p,int l,int r) { //cout<<p<<" "<<l<<" "<<r<<endl; if(l==r) { ans[p]=a[l]; return ; } int mid=(l+r)>>1; build(ls(p),l,mid); build(rs(p),mid+1,r); pushup(p); } ll query(int p,int nl,int nr,int l,int r) { ll c=0; //cout<<p<<" "<<nl<<" "<<nr<<" "<<l<<" "<<r<<" "<<endl; if(l<=nl&&nr<=r) { //cout<<ans[p]<<endl; //cout<<ans[p]<<endl; return ans[p]; } int mid=(nl+nr)>>1; //cout<<mid<<endl; if(l<=mid) { c+=query(ls(p),nl,mid,l,r); } if(mid<r) { c+=query(rs(p),mid+1,nr,l,r); } //cout<<c<<endl; return c; } int main() { int n,k,x; cin>>n>>k>>x; for(int i=1; i<=n; i++) { cin>>a[i]; } build(1,1,n); ll answer=0; for(int l=1; l<=n; l++) { for(int r=l+1; r<=n; r++) { if(x>=0) { if(r-l+1<=k) { answer=max(answer,(r-l+1)*x+query(1,1,n,l,r)); }else{ answer=max(answer,k*x-(r-l+1-k)*x+query(1,1,n,l,r)); } }else{ if(r-l+1<=n-k) { answer=max(answer,(r-l+1)*(-x)+query(1,1,n,l,r)); }else{ answer=max(answer,(n-k)*(-x)+(r-l+1-(n-k))*x+query(1,1,n,l,r)); } } //cout<<endl<<endl; } } cout<<answer<<endl; return 0; }