#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];
//deb(L[0]) cout << '\n';
//deb(l) deb(r) deb(newL) deb(newR) cout << '\n';
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) {
Set.insert({-newVal, newL, newR});
}
if (newR) {
L[newR] = newL;
}
R[newL] = newR;
Val[newL] = newVal;
//deb(Set) cout << '\n';
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2544 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2544 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
248 ms |
20120 KB |
Output is correct |
22 |
Correct |
227 ms |
20052 KB |
Output is correct |
23 |
Correct |
240 ms |
20304 KB |
Output is correct |
24 |
Correct |
115 ms |
20068 KB |
Output is correct |
25 |
Correct |
114 ms |
20048 KB |
Output is correct |
26 |
Correct |
125 ms |
20196 KB |
Output is correct |
27 |
Correct |
126 ms |
20308 KB |
Output is correct |
28 |
Correct |
121 ms |
20308 KB |
Output is correct |
29 |
Correct |
134 ms |
20292 KB |
Output is correct |
30 |
Correct |
125 ms |
20308 KB |
Output is correct |
31 |
Correct |
150 ms |
20132 KB |
Output is correct |
32 |
Correct |
153 ms |
20180 KB |
Output is correct |
33 |
Correct |
180 ms |
20100 KB |
Output is correct |
34 |
Correct |
213 ms |
20048 KB |
Output is correct |
35 |
Correct |
216 ms |
20068 KB |
Output is correct |
36 |
Correct |
281 ms |
20236 KB |
Output is correct |
37 |
Correct |
259 ms |
20220 KB |
Output is correct |
38 |
Correct |
293 ms |
20188 KB |
Output is correct |
39 |
Correct |
141 ms |
19996 KB |
Output is correct |
40 |
Correct |
146 ms |
20264 KB |
Output is correct |
41 |
Correct |
132 ms |
20012 KB |
Output is correct |
42 |
Correct |
135 ms |
20308 KB |
Output is correct |
43 |
Correct |
132 ms |
20244 KB |
Output is correct |
44 |
Correct |
134 ms |
20308 KB |
Output is correct |
45 |
Correct |
140 ms |
20144 KB |
Output is correct |
46 |
Correct |
145 ms |
20124 KB |
Output is correct |
47 |
Correct |
160 ms |
20100 KB |
Output is correct |
48 |
Correct |
200 ms |
20064 KB |
Output is correct |
49 |
Correct |
188 ms |
20052 KB |
Output is correct |
50 |
Correct |
180 ms |
20012 KB |
Output is correct |