#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = (int)2e5+10;
int n, a[N], l[N], r[N], ok[N];
priority_queue<array<int,2>> pq;
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n; int ans = 0; a[0]=a[n+1]=-2e18;
for(int i = 1; i <= n; i++)
cin >> a[i], pq.push({a[i],i}), l[i]=i-1,r[i]=i+1;
for(int j = 0; j < (n+1)/2; j++){
while(ok[pq.top()[1]]) pq.pop();
auto [w,i] = pq.top(); pq.pop();
ok[l[i]]=ok[r[i]]=1; cout << (ans+=w) << "\n";
a[i]=a[l[i]]+a[r[i]]-a[i], pq.push({a[i],i});
l[i] = l[l[i]], r[l[i]]=i; r[i] = r[r[i]], l[r[i]]=i;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
424 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
472 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
424 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
472 KB |
Output is correct |
21 |
Correct |
107 ms |
13156 KB |
Output is correct |
22 |
Correct |
105 ms |
13196 KB |
Output is correct |
23 |
Correct |
110 ms |
13296 KB |
Output is correct |
24 |
Correct |
74 ms |
13112 KB |
Output is correct |
25 |
Correct |
74 ms |
13112 KB |
Output is correct |
26 |
Correct |
72 ms |
13112 KB |
Output is correct |
27 |
Correct |
85 ms |
13368 KB |
Output is correct |
28 |
Correct |
83 ms |
13200 KB |
Output is correct |
29 |
Correct |
81 ms |
13228 KB |
Output is correct |
30 |
Correct |
85 ms |
13232 KB |
Output is correct |
31 |
Correct |
84 ms |
13168 KB |
Output is correct |
32 |
Correct |
88 ms |
13184 KB |
Output is correct |
33 |
Correct |
104 ms |
12984 KB |
Output is correct |
34 |
Correct |
92 ms |
13080 KB |
Output is correct |
35 |
Correct |
89 ms |
13116 KB |
Output is correct |
36 |
Correct |
111 ms |
13184 KB |
Output is correct |
37 |
Correct |
104 ms |
13268 KB |
Output is correct |
38 |
Correct |
101 ms |
13240 KB |
Output is correct |
39 |
Correct |
74 ms |
13080 KB |
Output is correct |
40 |
Correct |
72 ms |
13100 KB |
Output is correct |
41 |
Correct |
74 ms |
13056 KB |
Output is correct |
42 |
Correct |
103 ms |
13284 KB |
Output is correct |
43 |
Correct |
98 ms |
13228 KB |
Output is correct |
44 |
Correct |
87 ms |
13284 KB |
Output is correct |
45 |
Correct |
86 ms |
13232 KB |
Output is correct |
46 |
Correct |
87 ms |
13168 KB |
Output is correct |
47 |
Correct |
86 ms |
13276 KB |
Output is correct |
48 |
Correct |
92 ms |
13088 KB |
Output is correct |
49 |
Correct |
106 ms |
12984 KB |
Output is correct |
50 |
Correct |
106 ms |
13012 KB |
Output is correct |