#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int Z = 200'002;
/*
Take maximum value (say A[i]) for K = 1, then we have that we either
take A[i] or both A[i-1] and A[i+1]. So add a virtual element weighing
A[i-1] - A[i] + A[i+1] replacing these three elements. Now solve
inductively for (K - 1).
*/
int N, K, L[Z], R[Z], r[Z];
ll A[Z], sum;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N;
priority_queue<array<ll, 2>> pq;
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;
R[0] = 1, L[N+1] = N;
r[0] = r[N+1] = 1;
for(int k = 1; k<=(N+1)/2; 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 |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
452 KB |
Output is correct |
18 |
Correct |
1 ms |
400 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
452 KB |
Output is correct |
18 |
Correct |
1 ms |
400 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
94 ms |
10924 KB |
Output is correct |
22 |
Correct |
108 ms |
10804 KB |
Output is correct |
23 |
Correct |
92 ms |
10912 KB |
Output is correct |
24 |
Correct |
88 ms |
10740 KB |
Output is correct |
25 |
Correct |
75 ms |
10644 KB |
Output is correct |
26 |
Correct |
115 ms |
10716 KB |
Output is correct |
27 |
Correct |
91 ms |
11056 KB |
Output is correct |
28 |
Correct |
140 ms |
10908 KB |
Output is correct |
29 |
Correct |
116 ms |
10908 KB |
Output is correct |
30 |
Correct |
96 ms |
10848 KB |
Output is correct |
31 |
Correct |
99 ms |
10840 KB |
Output is correct |
32 |
Correct |
91 ms |
10920 KB |
Output is correct |
33 |
Correct |
91 ms |
10628 KB |
Output is correct |
34 |
Correct |
91 ms |
10632 KB |
Output is correct |
35 |
Correct |
103 ms |
10652 KB |
Output is correct |
36 |
Correct |
111 ms |
10912 KB |
Output is correct |
37 |
Correct |
120 ms |
10800 KB |
Output is correct |
38 |
Correct |
99 ms |
10864 KB |
Output is correct |
39 |
Correct |
86 ms |
10660 KB |
Output is correct |
40 |
Correct |
81 ms |
10724 KB |
Output is correct |
41 |
Correct |
93 ms |
10672 KB |
Output is correct |
42 |
Correct |
98 ms |
10832 KB |
Output is correct |
43 |
Correct |
84 ms |
11052 KB |
Output is correct |
44 |
Correct |
98 ms |
10848 KB |
Output is correct |
45 |
Correct |
85 ms |
10836 KB |
Output is correct |
46 |
Correct |
91 ms |
10908 KB |
Output is correct |
47 |
Correct |
103 ms |
10876 KB |
Output is correct |
48 |
Correct |
88 ms |
10616 KB |
Output is correct |
49 |
Correct |
93 ms |
10644 KB |
Output is correct |
50 |
Correct |
124 ms |
10680 KB |
Output is correct |