#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pli pair<ll, int>
#define f first
#define s second
const int N = 200005;
int n, arr[N]; ll psum[N];
set<pii> se; ll ans;
priority_queue<pair<pli, pii>> pq;
ll sum(pii x){
return psum[x.s]-(x.f>=2?psum[x.f-2]:0);
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 0; i<n; ++i)
cin >> arr[i];
for(int i = 0; i<n; ++i)
psum[i] = arr[i]+(i>=2?psum[i-2]:0);
for(int i = 0; i<n; ++i)
pq.push({{(ll)arr[i], 1}, {i, i-2}});
for(int i = 1; i<=(n+1)/2;){
auto tmp = pq.top(); pq.pop();
ll gain = tmp.f.f;
int type = tmp.f.s;
int l = tmp.s.f, r = tmp.s.s;
if(r!=l-2 && !se.count({l, r})) continue;
int nl, nr;
if(r==l-2){
auto it = se.lower_bound({l, r});
if((it!=se.end() && it->f<=l+2) || (it!=se.begin() && prev(it)->s>=l-2))
continue;
nl = nr = l;
} else if(type==0){
auto it = se.lower_bound({l, r});
if(it!=se.begin() && prev(it)->s==l-3)
continue;
if(it!=se.begin() && prev(it)->s==l-4){
nl = prev(it)->f, nr = r;
se.erase(prev(it));
se.erase({l, r});
} else{
nl = l-2, nr = r;
se.erase({l, r});
}
} else if(type==1){
auto it = se.upper_bound({l, r});
if(it!=se.end() && it->f==r+3)
continue;
if(it!=se.end() && it->f==r+4){
nl = l, nr = it->s;
se.erase(it);
se.erase({l, r});
} else{
nl = l, nr = r+2;
se.erase({l, r});
}
} else{
auto it = se.lower_bound({l, r});
pii p1 = {-1, -1}, p2 = {-1, -1};
if(next(it)!=se.end() && next(it)->f==r+3)
nr = next(it)->s, p2 = *next(it);
else nr = r+1;
if(it!=se.begin() && prev(it)->s==l-3)
nl = prev(it)->f, p1 = *prev(it);
else nl = l-1;
se.erase({l, r});
if(p1.f!=-1) se.erase(p1);
if(p2.f!=-1) se.erase(p2);
}
ans += gain;
se.insert({nl, nr});
auto it = se.lower_bound({nl, nr});
if(nl>=2 && (it==se.begin() || prev(it)->s!=nl-3))
pq.push({{arr[nl-2], 0}, {nl, nr}});
if(nr<=n-1-2 && (next(it)==se.end() || next(it)->f!=nr+3))
pq.push({{arr[nr+2], 1}, {nl, nr}});
if(nl>=1 && nr<=n-1-1)
pq.push({{sum({nl-1, nr+1})-sum({nl, nr}), 2}, {nl, nr}});
cout << ans << '\n'; ++i;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
452 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
452 KB |
Output is correct |
12 |
Correct |
2 ms |
456 KB |
Output is correct |
13 |
Correct |
2 ms |
440 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
452 KB |
Output is correct |
16 |
Correct |
2 ms |
480 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
452 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
452 KB |
Output is correct |
12 |
Correct |
2 ms |
456 KB |
Output is correct |
13 |
Correct |
2 ms |
440 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
452 KB |
Output is correct |
16 |
Correct |
2 ms |
480 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
267 ms |
13476 KB |
Output is correct |
22 |
Correct |
279 ms |
13592 KB |
Output is correct |
23 |
Correct |
292 ms |
13544 KB |
Output is correct |
24 |
Correct |
164 ms |
12012 KB |
Output is correct |
25 |
Correct |
155 ms |
12068 KB |
Output is correct |
26 |
Correct |
175 ms |
12068 KB |
Output is correct |
27 |
Correct |
201 ms |
13576 KB |
Output is correct |
28 |
Correct |
208 ms |
13468 KB |
Output is correct |
29 |
Correct |
192 ms |
13408 KB |
Output is correct |
30 |
Correct |
193 ms |
13564 KB |
Output is correct |
31 |
Correct |
202 ms |
13516 KB |
Output is correct |
32 |
Correct |
205 ms |
13504 KB |
Output is correct |
33 |
Correct |
217 ms |
13340 KB |
Output is correct |
34 |
Correct |
228 ms |
13328 KB |
Output is correct |
35 |
Correct |
222 ms |
13448 KB |
Output is correct |
36 |
Correct |
264 ms |
13552 KB |
Output is correct |
37 |
Correct |
291 ms |
13516 KB |
Output is correct |
38 |
Correct |
293 ms |
13596 KB |
Output is correct |
39 |
Correct |
154 ms |
12036 KB |
Output is correct |
40 |
Correct |
176 ms |
12092 KB |
Output is correct |
41 |
Correct |
151 ms |
12064 KB |
Output is correct |
42 |
Correct |
199 ms |
13504 KB |
Output is correct |
43 |
Correct |
187 ms |
13536 KB |
Output is correct |
44 |
Correct |
218 ms |
13484 KB |
Output is correct |
45 |
Correct |
192 ms |
13472 KB |
Output is correct |
46 |
Correct |
192 ms |
13544 KB |
Output is correct |
47 |
Correct |
200 ms |
13588 KB |
Output is correct |
48 |
Correct |
218 ms |
13308 KB |
Output is correct |
49 |
Correct |
235 ms |
13404 KB |
Output is correct |
50 |
Correct |
250 ms |
13336 KB |
Output is correct |