#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 200005;
int N;
long long A[MAXN];
long long Answer[MAXN];
void Read() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
}
}
bool used[MAXN];
long long pref[MAXN][2]; // (even, odd) only
void Solve() {
priority_queue<pair<long long, pair<int, int>>> pq;
set<int> unused;
for (int i = 1; i <= N; i++) {
pq.push({A[i], {i, i + 1}});
}
for (int i = 0; i <= N + 2; i++) {
unused.emplace(i);
}
for (int i = 1; i <= N + 2; i++) {
pref[i][0] = pref[i - 1][0];
pref[i][1] = pref[i - 1][1];
pref[i][i & 1] += A[i];
}
int num = 0;
long long ans = 0;
while (!pq.empty()) {
long long w = pq.top().first;
int u = pq.top().second.first;
int v = pq.top().second.second;
pq.pop();
if (used[u] || used[v]) continue;
used[u] = true, used[v] = true;
unused.erase(u);
unused.erase(v);
Answer[++num] = (ans += w);
int L = *prev(unused.lower_bound(u));
int R = *unused.lower_bound(v);
if (1 <= L && R <= N + 1) {
long long NotTakeUV = (pref[R - 1][L & 1] - pref[L - 1][L & 1]);
long long TakeUV = (pref[R - 1][(L & 1) ^ 1] - pref[L - 1][(L & 1) ^ 1]);
pq.push({NotTakeUV - TakeUV, {L, R}});
}
}
}
void Write() {
for (int i = 1; i <= (N + 1) / 2; i++) {
cout << Answer[i] << "\n";
}
}
int main() {
Read();
Solve();
Write();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
6 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
287 ms |
20576 KB |
Output is correct |
22 |
Correct |
279 ms |
20448 KB |
Output is correct |
23 |
Correct |
278 ms |
20448 KB |
Output is correct |
24 |
Correct |
185 ms |
20320 KB |
Output is correct |
25 |
Correct |
185 ms |
20324 KB |
Output is correct |
26 |
Correct |
185 ms |
20320 KB |
Output is correct |
27 |
Correct |
197 ms |
20448 KB |
Output is correct |
28 |
Correct |
202 ms |
20448 KB |
Output is correct |
29 |
Correct |
201 ms |
20624 KB |
Output is correct |
30 |
Correct |
206 ms |
20448 KB |
Output is correct |
31 |
Correct |
206 ms |
20448 KB |
Output is correct |
32 |
Correct |
211 ms |
20448 KB |
Output is correct |
33 |
Correct |
230 ms |
20320 KB |
Output is correct |
34 |
Correct |
230 ms |
20316 KB |
Output is correct |
35 |
Correct |
229 ms |
20320 KB |
Output is correct |
36 |
Correct |
275 ms |
20448 KB |
Output is correct |
37 |
Correct |
288 ms |
20532 KB |
Output is correct |
38 |
Correct |
277 ms |
20448 KB |
Output is correct |
39 |
Correct |
192 ms |
20320 KB |
Output is correct |
40 |
Correct |
185 ms |
20320 KB |
Output is correct |
41 |
Correct |
182 ms |
20320 KB |
Output is correct |
42 |
Correct |
195 ms |
20448 KB |
Output is correct |
43 |
Correct |
198 ms |
20448 KB |
Output is correct |
44 |
Correct |
202 ms |
20616 KB |
Output is correct |
45 |
Correct |
210 ms |
20448 KB |
Output is correct |
46 |
Correct |
209 ms |
20448 KB |
Output is correct |
47 |
Correct |
205 ms |
20452 KB |
Output is correct |
48 |
Correct |
234 ms |
20320 KB |
Output is correct |
49 |
Correct |
228 ms |
20320 KB |
Output is correct |
50 |
Correct |
232 ms |
20320 KB |
Output is correct |