#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
const int N = 200000;
const i64 INF = 1e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<i64> A(N);
for (auto &x : A) cin >> x;
priority_queue<pair<i64, int>> paths;
vector<int> L(N), R(N), done(N);
for (int i = 0; i < N; i++) {
L[i] = i - 1, R[i] = i + 1;
paths.push({A[i], i});
}
int take = (N + 1) / 2;
i64 total_cost = 0;
for (int rep = 0; rep < take; rep++) {
while (sz(paths) && done[paths.top().second]) paths.pop();
assert(sz(paths));
auto [cost, i] = paths.top(); paths.pop();
total_cost += cost;
cout << total_cost << '\n';
A[i] = (L[i] < 0 ? -INF : A[L[i]]) + (R[i] >= N ? -INF : A[R[i]]) - A[i];
if (L[i] >= 0) {
done[L[i]] = 1;
int u = L[L[i]];
if (u >= 0) R[u] = i;
L[i] = u;
}
if (R[i] < N) {
done[R[i]] = 1;
int u = R[R[i]];
if (u < N) L[u] = i;
R[i] = u;
}
paths.push({A[i], i});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
73 ms |
10996 KB |
Output is correct |
22 |
Correct |
77 ms |
11748 KB |
Output is correct |
23 |
Correct |
71 ms |
11748 KB |
Output is correct |
24 |
Correct |
58 ms |
10964 KB |
Output is correct |
25 |
Correct |
69 ms |
11464 KB |
Output is correct |
26 |
Correct |
57 ms |
10952 KB |
Output is correct |
27 |
Correct |
63 ms |
12228 KB |
Output is correct |
28 |
Correct |
77 ms |
11728 KB |
Output is correct |
29 |
Correct |
63 ms |
11976 KB |
Output is correct |
30 |
Correct |
67 ms |
10952 KB |
Output is correct |
31 |
Correct |
64 ms |
10972 KB |
Output is correct |
32 |
Correct |
71 ms |
10956 KB |
Output is correct |
33 |
Correct |
66 ms |
11020 KB |
Output is correct |
34 |
Correct |
66 ms |
11220 KB |
Output is correct |
35 |
Correct |
83 ms |
12488 KB |
Output is correct |
36 |
Correct |
72 ms |
11468 KB |
Output is correct |
37 |
Correct |
73 ms |
11724 KB |
Output is correct |
38 |
Correct |
69 ms |
11696 KB |
Output is correct |
39 |
Correct |
63 ms |
12004 KB |
Output is correct |
40 |
Correct |
57 ms |
11476 KB |
Output is correct |
41 |
Correct |
59 ms |
10956 KB |
Output is correct |
42 |
Correct |
73 ms |
10964 KB |
Output is correct |
43 |
Correct |
64 ms |
11208 KB |
Output is correct |
44 |
Correct |
63 ms |
11724 KB |
Output is correct |
45 |
Correct |
65 ms |
12232 KB |
Output is correct |
46 |
Correct |
65 ms |
12228 KB |
Output is correct |
47 |
Correct |
63 ms |
10948 KB |
Output is correct |
48 |
Correct |
66 ms |
11480 KB |
Output is correct |
49 |
Correct |
80 ms |
11208 KB |
Output is correct |
50 |
Correct |
69 ms |
11464 KB |
Output is correct |