//#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.tie(0);
ios_base::sync_with_stdio(0);
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";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2744 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
2 ms |
2648 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 |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2744 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
2 ms |
2648 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 |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
478 ms |
35676 KB |
Output is correct |
22 |
Correct |
458 ms |
35664 KB |
Output is correct |
23 |
Correct |
439 ms |
35668 KB |
Output is correct |
24 |
Correct |
209 ms |
35536 KB |
Output is correct |
25 |
Correct |
209 ms |
35668 KB |
Output is correct |
26 |
Correct |
217 ms |
35668 KB |
Output is correct |
27 |
Correct |
236 ms |
35840 KB |
Output is correct |
28 |
Correct |
235 ms |
35664 KB |
Output is correct |
29 |
Correct |
236 ms |
35668 KB |
Output is correct |
30 |
Correct |
264 ms |
35676 KB |
Output is correct |
31 |
Correct |
251 ms |
35664 KB |
Output is correct |
32 |
Correct |
264 ms |
35912 KB |
Output is correct |
33 |
Correct |
365 ms |
35664 KB |
Output is correct |
34 |
Correct |
324 ms |
35768 KB |
Output is correct |
35 |
Correct |
321 ms |
35688 KB |
Output is correct |
36 |
Correct |
461 ms |
35712 KB |
Output is correct |
37 |
Correct |
487 ms |
35704 KB |
Output is correct |
38 |
Correct |
450 ms |
35664 KB |
Output is correct |
39 |
Correct |
227 ms |
35668 KB |
Output is correct |
40 |
Correct |
203 ms |
35668 KB |
Output is correct |
41 |
Correct |
213 ms |
35628 KB |
Output is correct |
42 |
Correct |
240 ms |
35464 KB |
Output is correct |
43 |
Correct |
234 ms |
35504 KB |
Output is correct |
44 |
Correct |
234 ms |
35648 KB |
Output is correct |
45 |
Correct |
250 ms |
35700 KB |
Output is correct |
46 |
Correct |
250 ms |
35572 KB |
Output is correct |
47 |
Correct |
264 ms |
35728 KB |
Output is correct |
48 |
Correct |
327 ms |
35708 KB |
Output is correct |
49 |
Correct |
343 ms |
35520 KB |
Output is correct |
50 |
Correct |
322 ms |
35664 KB |
Output is correct |