#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::priority_queue<std::pair<i64, int>> q;
std::vector<i64> a(n + 2), l(n + 2), r(n + 2);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
q.emplace(a[i], i);
l[i] = i - 1;
r[i] = i + 1;
}
a[0] = a[n + 1] = -1E15;
std::vector<bool> del(n + 2);
i64 ans = 0;
for (int i = 1; i <= (n + 1) / 2; i++) {
while (del[q.top().second])
q.pop();
auto [v, j] = q.top();
q.pop();
ans += v;
std::cout << ans << "\n";
del[l[j]] = del[r[j]] = 1;
a[j] = a[l[j]] + a[r[j]] - a[j];
l[j] = l[l[j]];
r[j] = r[r[j]];
r[l[j]] = j;
l[r[j]] = j;
q.emplace(a[j], j);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
448 KB |
Output is correct |
13 |
Correct |
1 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
452 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
114 ms |
11620 KB |
Output is correct |
22 |
Correct |
101 ms |
11628 KB |
Output is correct |
23 |
Correct |
107 ms |
11688 KB |
Output is correct |
24 |
Correct |
87 ms |
11536 KB |
Output is correct |
25 |
Correct |
75 ms |
11540 KB |
Output is correct |
26 |
Correct |
75 ms |
11532 KB |
Output is correct |
27 |
Correct |
82 ms |
11616 KB |
Output is correct |
28 |
Correct |
84 ms |
11616 KB |
Output is correct |
29 |
Correct |
79 ms |
11672 KB |
Output is correct |
30 |
Correct |
86 ms |
11700 KB |
Output is correct |
31 |
Correct |
81 ms |
11596 KB |
Output is correct |
32 |
Correct |
82 ms |
11700 KB |
Output is correct |
33 |
Correct |
100 ms |
11428 KB |
Output is correct |
34 |
Correct |
94 ms |
11516 KB |
Output is correct |
35 |
Correct |
89 ms |
11520 KB |
Output is correct |
36 |
Correct |
99 ms |
11636 KB |
Output is correct |
37 |
Correct |
99 ms |
11816 KB |
Output is correct |
38 |
Correct |
97 ms |
11604 KB |
Output is correct |
39 |
Correct |
72 ms |
11524 KB |
Output is correct |
40 |
Correct |
75 ms |
11444 KB |
Output is correct |
41 |
Correct |
80 ms |
11452 KB |
Output is correct |
42 |
Correct |
81 ms |
11656 KB |
Output is correct |
43 |
Correct |
79 ms |
11708 KB |
Output is correct |
44 |
Correct |
85 ms |
11624 KB |
Output is correct |
45 |
Correct |
84 ms |
11664 KB |
Output is correct |
46 |
Correct |
83 ms |
11624 KB |
Output is correct |
47 |
Correct |
85 ms |
11688 KB |
Output is correct |
48 |
Correct |
96 ms |
11424 KB |
Output is correct |
49 |
Correct |
103 ms |
11532 KB |
Output is correct |
50 |
Correct |
87 ms |
11440 KB |
Output is correct |