//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int ar[500005];
int ps[2][500005];
set<pair<int,pair<int,int>>>stv[2];
set<pair<pair<int,int>,int>>st[2];
signed main()
{
cin>>n;
ar[0]=-1e16;ar[n+1]=-1e16;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
ps[0][0]=ar[0];
for(int i=1;i<=n+1;i++){
ps[0][i]=ps[0][i-1];
ps[1][i]=ps[1][i-1];
ps[i%2][i]+=ar[i];
}
int ans=0;
for(int i=0;i<=n+1;i++){
stv[i%2].insert({ar[i],{i,i}});
st[i%2].insert({{i,i},ar[i]});
}
for(int ttt=1;ttt<=(n+1)/2;ttt++){
int v0=(*prev(stv[0].end())).first;
int v1=(*prev(stv[1].end())).first;
if(v0>v1){
ans+=v0;
int ogl=(*prev(stv[0].end())).second.first;
int ogr=(*prev(stv[0].end())).second.second;
stv[0].erase({v0,{ogl,ogr}});
st[0].erase({{ogl,ogr},v0});
int l;int r;
auto it=st[1].lower_bound({{ogl,-1},-1000});
int a;int b;int c;//{ {l,r},v }
a=(*it).first.first;
b=(*it).first.second;
c=(*it).second;
r=b;
it--;
st[1].erase({{a,b},c});
stv[1].erase({c,{a,b}});
a=(*it).first.first;
b=(*it).first.second;
c=(*it).second;
l=a;
st[1].erase({{a,b},c});
stv[1].erase({c,{a,b}});
int v=(ps[1][r]-ps[1][l-1])-(ps[0][r]-ps[0][l-1]);
st[1].insert({{l,r},v});
stv[1].insert({v,{l,r}});
}else{
//continue;
ans+=v1;
int ogl=(*prev(stv[1].end())).second.first;
int ogr=(*prev(stv[1].end())).second.second;
stv[1].erase({v1,{ogl,ogr}});
st[1].erase({{ogl,ogr},v1});
int l;int r;
auto it=st[0].upper_bound({{ogl,-1},-100 });
int a;int b;int c;//{ {l,r},v }
a=(*it).first.first;
b=(*it).first.second;
c=(*it).second;
r=b;
it--;
st[0].erase({{a,b},c});
stv[0].erase({c,{a,b}});
a=(*it).first.first;
b=(*it).first.second;
c=(*it).second;
l=a;
st[0].erase({{a,b},c});
stv[0].erase({c,{a,b}});
//cout<<l<<" "<<r<<"\n";
int v=(ps[0][r]-ps[0][l-1])-(ps[1][r]-ps[1][l-1]);
// cout<<"hehe";
st[0].insert({{l,r},v});
stv[0].insert({v,{l,r}});
}
cout<<ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2760 KB |
Output is correct |
3 |
Correct |
3 ms |
2804 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2660 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2904 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2756 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2760 KB |
Output is correct |
3 |
Correct |
3 ms |
2804 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
3 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2660 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
3 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2904 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2756 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2664 KB |
Output is correct |
21 |
Correct |
489 ms |
37444 KB |
Output is correct |
22 |
Correct |
506 ms |
37460 KB |
Output is correct |
23 |
Correct |
441 ms |
37652 KB |
Output is correct |
24 |
Correct |
267 ms |
37424 KB |
Output is correct |
25 |
Correct |
275 ms |
37412 KB |
Output is correct |
26 |
Correct |
247 ms |
37460 KB |
Output is correct |
27 |
Correct |
279 ms |
37412 KB |
Output is correct |
28 |
Correct |
272 ms |
37456 KB |
Output is correct |
29 |
Correct |
283 ms |
37528 KB |
Output is correct |
30 |
Correct |
288 ms |
37656 KB |
Output is correct |
31 |
Correct |
290 ms |
37796 KB |
Output is correct |
32 |
Correct |
294 ms |
37468 KB |
Output is correct |
33 |
Correct |
375 ms |
37656 KB |
Output is correct |
34 |
Correct |
363 ms |
37460 KB |
Output is correct |
35 |
Correct |
342 ms |
37324 KB |
Output is correct |
36 |
Correct |
483 ms |
37444 KB |
Output is correct |
37 |
Correct |
467 ms |
37624 KB |
Output is correct |
38 |
Correct |
468 ms |
37496 KB |
Output is correct |
39 |
Correct |
252 ms |
37408 KB |
Output is correct |
40 |
Correct |
263 ms |
37496 KB |
Output is correct |
41 |
Correct |
258 ms |
37460 KB |
Output is correct |
42 |
Correct |
270 ms |
37544 KB |
Output is correct |
43 |
Correct |
271 ms |
37456 KB |
Output is correct |
44 |
Correct |
277 ms |
37460 KB |
Output is correct |
45 |
Correct |
300 ms |
37824 KB |
Output is correct |
46 |
Correct |
293 ms |
37516 KB |
Output is correct |
47 |
Correct |
309 ms |
38032 KB |
Output is correct |
48 |
Correct |
357 ms |
37200 KB |
Output is correct |
49 |
Correct |
371 ms |
37404 KB |
Output is correct |
50 |
Correct |
345 ms |
37204 KB |
Output is correct |