#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n;
vector <ll> a;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
a.resize(n + 2);
set <int> alive;
set <pair <ll, int> > s;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
s.insert(make_pair(a[i], i));
alive.insert(i);
}
a[0] = a[n + 1] = -1e18;
alive.insert(0);
alive.insert(n + 1);
ll ans = 0;
for(int itt = 0 ; itt < (n + 1) / 2 ; itt++){
auto top = *--s.end();
s.erase(--s.end());
ans += top.first;
cout << ans << endl;
int pos = top.second;
alive.erase(pos);
a[pos] *= -1;
auto it = alive.upper_bound(pos);
if(it != alive.end()){
a[pos] += a[*it];
s.erase(make_pair(a[*it], *it));
alive.erase(*it);
}
it = alive.upper_bound(pos);
if(it != alive.begin()){
it--;
a[pos] += a[*it];
s.erase(make_pair(a[*it], *it));
alive.erase(*it);
}
alive.insert(pos);
s.insert(make_pair(a[pos], pos));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
632 KB |
Output is correct |
2 |
Correct |
8 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
7 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
628 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
7 ms |
632 KB |
Output is correct |
10 |
Correct |
7 ms |
632 KB |
Output is correct |
11 |
Correct |
8 ms |
636 KB |
Output is correct |
12 |
Correct |
8 ms |
632 KB |
Output is correct |
13 |
Correct |
7 ms |
632 KB |
Output is correct |
14 |
Correct |
7 ms |
632 KB |
Output is correct |
15 |
Correct |
8 ms |
636 KB |
Output is correct |
16 |
Correct |
7 ms |
632 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
632 KB |
Output is correct |
20 |
Correct |
7 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
632 KB |
Output is correct |
2 |
Correct |
8 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
7 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
628 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
7 ms |
632 KB |
Output is correct |
9 |
Correct |
7 ms |
632 KB |
Output is correct |
10 |
Correct |
7 ms |
632 KB |
Output is correct |
11 |
Correct |
8 ms |
636 KB |
Output is correct |
12 |
Correct |
8 ms |
632 KB |
Output is correct |
13 |
Correct |
7 ms |
632 KB |
Output is correct |
14 |
Correct |
7 ms |
632 KB |
Output is correct |
15 |
Correct |
8 ms |
636 KB |
Output is correct |
16 |
Correct |
7 ms |
632 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
632 KB |
Output is correct |
20 |
Correct |
7 ms |
632 KB |
Output is correct |
21 |
Correct |
1016 ms |
27472 KB |
Output is correct |
22 |
Correct |
1033 ms |
27448 KB |
Output is correct |
23 |
Correct |
1002 ms |
27432 KB |
Output is correct |
24 |
Correct |
610 ms |
27192 KB |
Output is correct |
25 |
Correct |
611 ms |
27056 KB |
Output is correct |
26 |
Correct |
613 ms |
27256 KB |
Output is correct |
27 |
Correct |
621 ms |
27384 KB |
Output is correct |
28 |
Correct |
619 ms |
27400 KB |
Output is correct |
29 |
Correct |
628 ms |
27376 KB |
Output is correct |
30 |
Correct |
638 ms |
27252 KB |
Output is correct |
31 |
Correct |
640 ms |
27508 KB |
Output is correct |
32 |
Correct |
639 ms |
27376 KB |
Output is correct |
33 |
Correct |
779 ms |
27128 KB |
Output is correct |
34 |
Correct |
804 ms |
27088 KB |
Output is correct |
35 |
Correct |
778 ms |
27248 KB |
Output is correct |
36 |
Correct |
1046 ms |
27324 KB |
Output is correct |
37 |
Correct |
993 ms |
27296 KB |
Output is correct |
38 |
Correct |
1010 ms |
27356 KB |
Output is correct |
39 |
Correct |
607 ms |
27128 KB |
Output is correct |
40 |
Correct |
636 ms |
27128 KB |
Output is correct |
41 |
Correct |
605 ms |
27252 KB |
Output is correct |
42 |
Correct |
619 ms |
27384 KB |
Output is correct |
43 |
Correct |
633 ms |
27356 KB |
Output is correct |
44 |
Correct |
621 ms |
27504 KB |
Output is correct |
45 |
Correct |
641 ms |
27200 KB |
Output is correct |
46 |
Correct |
639 ms |
27512 KB |
Output is correct |
47 |
Correct |
648 ms |
27384 KB |
Output is correct |
48 |
Correct |
782 ms |
27052 KB |
Output is correct |
49 |
Correct |
779 ms |
27128 KB |
Output is correct |
50 |
Correct |
796 ms |
27128 KB |
Output is correct |