#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define Z 200'002
int N, K, L[Z], R[Z], r[Z];
ll A[Z], sum;
priority_queue<array<ll, 2>> pq;
int main() {
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> A[i];
L[i] = i - 1, R[i] = i + 1;
pq.push({A[i], i});
}
R[N+1] = N+1, L[N+1] = N;
R[0] = r[0] = r[N+1] = 1;
for(int k = 1; k*2 <= N+1; k++) {
while(r[pq.top()[1]]) pq.pop();
auto [v, i] = pq.top(); pq.pop();
cout << (sum += v) << '\n';
r[i] = r[L[i]] || r[R[i]];
if(!r[L[i]]) v -= A[L[i]], r[L[i]] = 1, R[L[i] = L[L[i]]] = i;
if(!r[R[i]]) v -= A[R[i]], r[R[i]] = 1, L[R[i] = R[R[i]]] = i;
if(!r[i]) pq.push({A[i] = -v, i});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
352 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
400 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
428 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
428 KB |
Output is correct |
16 |
Correct |
2 ms |
324 KB |
Output is correct |
17 |
Correct |
2 ms |
428 KB |
Output is correct |
18 |
Correct |
2 ms |
440 KB |
Output is correct |
19 |
Correct |
2 ms |
432 KB |
Output is correct |
20 |
Correct |
3 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
352 KB |
Output is correct |
2 |
Correct |
2 ms |
436 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
400 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
428 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
428 KB |
Output is correct |
16 |
Correct |
2 ms |
324 KB |
Output is correct |
17 |
Correct |
2 ms |
428 KB |
Output is correct |
18 |
Correct |
2 ms |
440 KB |
Output is correct |
19 |
Correct |
2 ms |
432 KB |
Output is correct |
20 |
Correct |
3 ms |
440 KB |
Output is correct |
21 |
Correct |
189 ms |
9388 KB |
Output is correct |
22 |
Correct |
159 ms |
9332 KB |
Output is correct |
23 |
Correct |
196 ms |
9272 KB |
Output is correct |
24 |
Correct |
149 ms |
9320 KB |
Output is correct |
25 |
Correct |
139 ms |
9364 KB |
Output is correct |
26 |
Correct |
136 ms |
9236 KB |
Output is correct |
27 |
Correct |
142 ms |
9276 KB |
Output is correct |
28 |
Correct |
140 ms |
9292 KB |
Output is correct |
29 |
Correct |
162 ms |
9364 KB |
Output is correct |
30 |
Correct |
143 ms |
9292 KB |
Output is correct |
31 |
Correct |
152 ms |
9268 KB |
Output is correct |
32 |
Correct |
139 ms |
9304 KB |
Output is correct |
33 |
Correct |
147 ms |
9224 KB |
Output is correct |
34 |
Correct |
151 ms |
9288 KB |
Output is correct |
35 |
Correct |
155 ms |
9240 KB |
Output is correct |
36 |
Correct |
154 ms |
9416 KB |
Output is correct |
37 |
Correct |
156 ms |
9256 KB |
Output is correct |
38 |
Correct |
146 ms |
9284 KB |
Output is correct |
39 |
Correct |
141 ms |
9320 KB |
Output is correct |
40 |
Correct |
125 ms |
9276 KB |
Output is correct |
41 |
Correct |
121 ms |
9352 KB |
Output is correct |
42 |
Correct |
179 ms |
9284 KB |
Output is correct |
43 |
Correct |
149 ms |
9304 KB |
Output is correct |
44 |
Correct |
168 ms |
9232 KB |
Output is correct |
45 |
Correct |
135 ms |
9252 KB |
Output is correct |
46 |
Correct |
133 ms |
9248 KB |
Output is correct |
47 |
Correct |
157 ms |
9276 KB |
Output is correct |
48 |
Correct |
130 ms |
9228 KB |
Output is correct |
49 |
Correct |
150 ms |
9256 KB |
Output is correct |
50 |
Correct |
143 ms |
9272 KB |
Output is correct |