#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long a[200005];
set <pair <long long, long long>> choices;
long long l[200005], r[200005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long n; cin >> n;
a[0] = a[n + 1] = -INF;
for (long long i = 1; i <= n; i++){
cin >> a[i]; l[i] = i - 1; r[i] = i + 1;
choices.insert(make_pair(-a[i], i));
}
long long ans = 0;
for (long long c = 0; c < (n + 1) / 2; c++){
pair <long long, long long> mx = *choices.begin();
choices.erase(choices.begin());
ans -= mx.first;
long long id = mx.second, lf = l[id], rg = r[id];
if (lf) choices.erase(make_pair(-a[lf], lf));
if (rg <= n) choices.erase(make_pair(-a[rg], rg));
if (lf) l[id] = l[lf];
else l[id] = 0;
if (rg <= n) r[id] = r[rg];
else r[id] = n + 1;
if (l[id]) r[l[id]] = id;
if (r[id] <= n) l[r[id]] = id;
a[id] = a[lf] + a[rg] - a[id];
choices.insert(make_pair(-a[id], id));
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
680 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
680 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
447 ms |
21192 KB |
Output is correct |
22 |
Correct |
428 ms |
21100 KB |
Output is correct |
23 |
Correct |
421 ms |
21228 KB |
Output is correct |
24 |
Correct |
167 ms |
20844 KB |
Output is correct |
25 |
Correct |
157 ms |
20972 KB |
Output is correct |
26 |
Correct |
150 ms |
20844 KB |
Output is correct |
27 |
Correct |
199 ms |
20972 KB |
Output is correct |
28 |
Correct |
170 ms |
21100 KB |
Output is correct |
29 |
Correct |
197 ms |
20972 KB |
Output is correct |
30 |
Correct |
177 ms |
20972 KB |
Output is correct |
31 |
Correct |
183 ms |
20972 KB |
Output is correct |
32 |
Correct |
198 ms |
21120 KB |
Output is correct |
33 |
Correct |
282 ms |
20844 KB |
Output is correct |
34 |
Correct |
284 ms |
20844 KB |
Output is correct |
35 |
Correct |
275 ms |
20844 KB |
Output is correct |
36 |
Correct |
419 ms |
20972 KB |
Output is correct |
37 |
Correct |
431 ms |
21228 KB |
Output is correct |
38 |
Correct |
492 ms |
21100 KB |
Output is correct |
39 |
Correct |
152 ms |
20844 KB |
Output is correct |
40 |
Correct |
194 ms |
20844 KB |
Output is correct |
41 |
Correct |
162 ms |
20856 KB |
Output is correct |
42 |
Correct |
167 ms |
21100 KB |
Output is correct |
43 |
Correct |
170 ms |
21100 KB |
Output is correct |
44 |
Correct |
166 ms |
21100 KB |
Output is correct |
45 |
Correct |
181 ms |
20972 KB |
Output is correct |
46 |
Correct |
182 ms |
20976 KB |
Output is correct |
47 |
Correct |
203 ms |
21100 KB |
Output is correct |
48 |
Correct |
272 ms |
20844 KB |
Output is correct |
49 |
Correct |
287 ms |
20844 KB |
Output is correct |
50 |
Correct |
270 ms |
20844 KB |
Output is correct |