#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#else
void setio(){}
#endif
const long long mxn=1e5+5;
multiset<long long>bg,sm;
long long n,st,d,L,R;
long long cur,v[mxn];
void sir(long l,long r,long k){
while(L>l){
bg.insert(v[--L]);
cur+=v[L];
}
while(R<r){
bg.insert(v[++R]);
cur+=v[R];
}
while(L<l){
if(bg.find(v[L])!=bg.end()){
cur-=v[L];
bg.erase(bg.find(v[L]));
}
else{
sm.erase(sm.find(v[L]));
}
L++;
}
while(R>r){
if(bg.find(v[R])!=bg.end()){
cur-=v[R];
bg.erase(bg.find(v[R]));
}
else{
sm.erase(sm.find(v[R]));
}
R--;
}
while(sz(sm) and sz(bg) and *sm.rbegin()>*bg.begin()){
int x=*sm.rbegin();
int y=*bg.begin();
bg.erase(bg.begin());
sm.erase(prev(sm.end()));
sm.insert(y);
bg.insert(x);
cur+=x-y;
}
while(sz(bg)>k){
cur-=*bg.begin();
sm.insert(*bg.begin());
bg.erase(bg.begin());
}
while(sz(bg)<k){
cur+=*prev(sm.end());
bg.insert(*prev(sm.end()));
sm.erase(prev(sm.end()));
}
}
long long ans;
void solve(long long l,long long r,long long ql,long long qr){
//cerr<<l<<' '<<r<<' '<<ql<<' '<<qr<<'\n';
if(l>r)return;
long long mm=(l+r)/2;
long long mx=0;
long long best=ql;
for(long long i=ql;i<=qr;i++){
long long x=min(mm-i+1,d-(min(st-i,mm-st)+mm-i));
//cerr<<x<<'\n';
if(x<0)break;
sir(i,mm,x);
if(cur>=mx){
best=i;
mx=cur;
}
}
chmax(ans,mx);
solve(l,mm-1,ql,best);
solve(mm+1,r,best,qr);
}
long long findMaxAttraction(int N,int ST,int D,int a[]){
n=N,st=ST,d=D,cur=0,L=0,R=n-1;
for(long long i=0;i<n;i++){
v[i]=a[i];
bg.insert(v[i]);
cur+=v[i];
}
//solve(0,st,st,n-1);
solve(st,n-1,0,st);
return ans;
}
#ifdef local
int main(){
setio();
int a,b,c;
cin>>a>>b>>c;
int x[n];
for(int i=0;i<a;i++){
cin>>x[i];
}
cout<<findMaxAttraction(a,b,c,x)<<'\n';
}
#else
#endif
/*
input:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
6236 KB |
Output is correct |
2 |
Correct |
80 ms |
6284 KB |
Output is correct |
3 |
Correct |
78 ms |
6288 KB |
Output is correct |
4 |
Correct |
76 ms |
6236 KB |
Output is correct |
5 |
Correct |
234 ms |
5724 KB |
Output is correct |
6 |
Correct |
40 ms |
2136 KB |
Output is correct |
7 |
Correct |
100 ms |
3604 KB |
Output is correct |
8 |
Correct |
146 ms |
4432 KB |
Output is correct |
9 |
Correct |
26 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
860 KB |
Output is correct |
5 |
Correct |
8 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6196 KB |
Output is correct |
2 |
Correct |
101 ms |
6284 KB |
Output is correct |
3 |
Incorrect |
15 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |