#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int MAX_N = 2e5 + 5;
int A[MAX_N];
int L[MAX_N], R[MAX_N];
long long Val[MAX_N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> A[i];
}
set<array<long long, 3>> Set;
for (int i = 1; i <= N; ++i) {
L[i] = R[i] = i;
Val[i] = A[i];
Set.insert({-A[i], i, i});
}
long long ans = 0;
for (int i = 1; i <= (N + 1) / 2; ++i) {
auto [val, l, r] = *Set.begin();
Set.erase({val, l, r});
ans += -val;
int newL = L[l - 1];
int newR = R[r + 1];
long long newVal = Val[L[l - 1]] + Val[r + 1] + val;
if (L[l - 1]) {
int tl = L[l - 1];
Set.erase({-Val[tl], tl, l - 1});
}
if (R[r + 1]) {
int tr = R[r + 1];
Set.erase({-Val[r + 1], r + 1, tr});
}
if (newL && newR) {
assert((newR - newL + 1) % 2) ;
Set.insert({-newVal, newL, newR});
}
if (newR) {
L[newR] = newL;
}
R[newL] = newR;
Val[newL] = newVal;
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2692 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
2 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2692 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2648 KB |
Output is correct |
18 |
Correct |
2 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2684 KB |
Output is correct |
21 |
Correct |
272 ms |
18452 KB |
Output is correct |
22 |
Correct |
236 ms |
18208 KB |
Output is correct |
23 |
Correct |
281 ms |
18264 KB |
Output is correct |
24 |
Correct |
135 ms |
18304 KB |
Output is correct |
25 |
Correct |
127 ms |
18216 KB |
Output is correct |
26 |
Correct |
157 ms |
18256 KB |
Output is correct |
27 |
Correct |
145 ms |
18240 KB |
Output is correct |
28 |
Correct |
139 ms |
18268 KB |
Output is correct |
29 |
Correct |
135 ms |
18216 KB |
Output is correct |
30 |
Correct |
132 ms |
18124 KB |
Output is correct |
31 |
Correct |
144 ms |
18200 KB |
Output is correct |
32 |
Correct |
143 ms |
18112 KB |
Output is correct |
33 |
Correct |
192 ms |
18352 KB |
Output is correct |
34 |
Correct |
190 ms |
18276 KB |
Output is correct |
35 |
Correct |
210 ms |
18256 KB |
Output is correct |
36 |
Correct |
272 ms |
18216 KB |
Output is correct |
37 |
Correct |
242 ms |
18360 KB |
Output is correct |
38 |
Correct |
265 ms |
18200 KB |
Output is correct |
39 |
Correct |
120 ms |
18256 KB |
Output is correct |
40 |
Correct |
119 ms |
18260 KB |
Output is correct |
41 |
Correct |
124 ms |
18320 KB |
Output is correct |
42 |
Correct |
132 ms |
18280 KB |
Output is correct |
43 |
Correct |
141 ms |
18532 KB |
Output is correct |
44 |
Correct |
150 ms |
18168 KB |
Output is correct |
45 |
Correct |
146 ms |
18208 KB |
Output is correct |
46 |
Correct |
130 ms |
18480 KB |
Output is correct |
47 |
Correct |
130 ms |
18260 KB |
Output is correct |
48 |
Correct |
180 ms |
18372 KB |
Output is correct |
49 |
Correct |
211 ms |
18260 KB |
Output is correct |
50 |
Correct |
165 ms |
18264 KB |
Output is correct |