1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<x<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); mt19937 sd(std::random_device{}()); int qpow (int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n,_; int a[200005],seg[800005],s2[800005],tag[800005]; void build(int l,int r,int p){ tag[p]=0; if(l==r)return seg[p]=s2[p]=a[l],void(); int m=(l+r)>>1; build(l,m,p*2+1);build(m+1,r,p*2+2); seg[p]=max(seg[p*2+1],seg[p*2+2]);s2[p]=min(s2[p*2+1],s2[p*2+2]); } void pushdown(int p){ if(!tag[p])return; tag[p*2+1]+=tag[p];tag[p*2+2]+=tag[p]; seg[p*2+1]+=tag[p];seg[p*2+2]+=tag[p]; s2[p*2+1]+=tag[p];s2[p*2+2]+=tag[p]; tag[p]=0; } void update(int l,int r,int s,int t,int p,int val){ if(l<=s&&t<=r){ seg[p]+=val;s2[p]+=val;tag[p]+=val; return; } int m=(s+t)>>1; pushdown(p); if(m>=l)update(l,r,s,m,p*2+1,val); if(m<r)update(l,r,m+1,t,p*2+2,val); seg[p]=max(seg[p*2+1],seg[p*2+2]);s2[p]=min(s2[p*2+1],s2[p*2+2]); } int query(int l,int r,int s,int t,int p){ if(l<=s&&t<=r)return seg[p]; int m=(s+t)>>1; pushdown(p); if(m>=r)return query(l,r,s,m,p*2+1); if(m<l)return query(l,r,m+1,t,p*2+2); return max(query(l,r,s,m,p*2+1),query(l,r,m+1,t,p*2+2)); } int query2(int l,int r,int s,int t,int p){ if(l<=s&&t<=r)return s2[p]; int m=(s+t)>>1; pushdown(p); if(m>=r)return query2(l,r,s,m,p*2+1); if(m<l)return query2(l,r,m+1,t,p*2+2); return min(query2(l,r,s,m,p*2+1),query2(l,r,m+1,t,p*2+2)); } pii b[200005]; void Main() { cin>>n>>_; REP(i,0,n)cin>>a[i]; if(_==0){ if(*max_element(a,a+n)<0)cout<<(*max_element(a,a+n))<<endl; else{ int ans=0,cur=0; REP(i,0,n){ cur=max(0ll,cur+a[i]); ans=max(ans,cur); } cout<<ans<<endl; } return; } set<int>s; REP(i,0,n)b[i]={a[i],i}; REP(i,1,n)a[i]+=a[i-1]; build(0,n-1,0); sort(b,b+n,greater<pii>()); REP(i,0,_-1){ s.insert(b[i].second); update(b[i].second,n-1,0,n-1,0,-b[i].first); } int ans=0; REP(i,_-1,n)if(b[i].first>=0){ update(b[i].second,n-1,0,n-1,0,-b[i].first); vector<int>v1,v2; auto it=s.lower_bound(b[i].second); REP(j,0,_-1)if(it!=s.begin()){ --it; v1.pb(*it); }else break; it=s.lower_bound(b[i].second); REP(j,0,_-1)if(it!=s.end()){ v2.pb(*it); ++it; } reverse(all(v1));v1.pb(b[i].second);for(auto j:v2)v1.pb(j); // for(auto j:v1)cout<<j+1<<' '; // cout<<endl; REP(j,0,v1.size()-_+1){ if(v1[j]==0)ans=max(ans,query(v1[j+_-1],n-1,0,n-1,0)); else ans=max(ans,query(v1[j+_-1],n-1,0,n-1,0)-min(0ll,query2(0,v1[j]-1,0,n-1,0))); // cout<<query(v1[j+_-1],n-1,0,n-1,0)<<' '<<query2(0,v1[j]-1,0,n-1,0)<<' '<<ans<<endl; } s.insert(b[i].second); // REP(j,0,n)cout<<query(j,j,0,n-1,0)<<' '; // cout<<endl; }else break; cout<<ans<<endl; } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("exchange.in","r",stdin); freopen("exchange.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2912
- Time
- 6000ms
- Memory
- 1024MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 5
- Accepted
- 2
- Uploaded By