#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n + 2);
vector<int> l(n + 2);
vector<int> r(n + 2);
priority_queue<pair<long long, int>> pq;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
l[i] = i - 1;
r[i] = i + 1;
pq.emplace(a[i], i);
}
const long long MINUS_INF = -1e15;
a[0] = MINUS_INF;
a[n + 1] = MINUS_INF;
l[n + 1] = n;
r[0] = 1;
long long curr_ans = 0;
vector<bool> used(n + 2);
for (int j = 1; j <= (n + 1) / 2; ++j) {
auto [value, i] = pq.top();
while (used[i]) {
pq.pop();
tie(value, i) = pq.top();
}
pq.pop();
curr_ans += value;
cout << curr_ans << '\n';
a[i] = a[l[i]] + a[r[i]] - a[i];
used[l[i]] = true;
used[r[i]] = true;
pq.emplace(a[i], i);
l[i] = l[l[i]];
r[l[i]] = i;
r[i] = r[r[i]];
l[r[i]] = i;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
99 ms |
10052 KB |
Output is correct |
22 |
Correct |
93 ms |
10120 KB |
Output is correct |
23 |
Correct |
86 ms |
10052 KB |
Output is correct |
24 |
Correct |
74 ms |
9892 KB |
Output is correct |
25 |
Correct |
71 ms |
10012 KB |
Output is correct |
26 |
Correct |
79 ms |
9908 KB |
Output is correct |
27 |
Correct |
77 ms |
10076 KB |
Output is correct |
28 |
Correct |
75 ms |
10148 KB |
Output is correct |
29 |
Correct |
100 ms |
10044 KB |
Output is correct |
30 |
Correct |
87 ms |
10048 KB |
Output is correct |
31 |
Correct |
80 ms |
10112 KB |
Output is correct |
32 |
Correct |
92 ms |
10056 KB |
Output is correct |
33 |
Correct |
89 ms |
9856 KB |
Output is correct |
34 |
Correct |
90 ms |
9924 KB |
Output is correct |
35 |
Correct |
80 ms |
9888 KB |
Output is correct |
36 |
Correct |
113 ms |
10136 KB |
Output is correct |
37 |
Correct |
94 ms |
10172 KB |
Output is correct |
38 |
Correct |
87 ms |
10156 KB |
Output is correct |
39 |
Correct |
74 ms |
10004 KB |
Output is correct |
40 |
Correct |
73 ms |
10012 KB |
Output is correct |
41 |
Correct |
68 ms |
9920 KB |
Output is correct |
42 |
Correct |
79 ms |
10052 KB |
Output is correct |
43 |
Correct |
79 ms |
10072 KB |
Output is correct |
44 |
Correct |
83 ms |
10172 KB |
Output is correct |
45 |
Correct |
89 ms |
10168 KB |
Output is correct |
46 |
Correct |
76 ms |
10068 KB |
Output is correct |
47 |
Correct |
94 ms |
10228 KB |
Output is correct |
48 |
Correct |
89 ms |
9916 KB |
Output is correct |
49 |
Correct |
106 ms |
9928 KB |
Output is correct |
50 |
Correct |
89 ms |
9864 KB |
Output is correct |