#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = (int)2e5+10;
priority_queue<array<int,2>> pq;
int n, ans, a[N], l[N], r[N], ok[N];
main() {
cin >> n; 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;
}
}
Compilation message
candies.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
8 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
162 ms |
11240 KB |
Output is correct |
22 |
Correct |
169 ms |
11268 KB |
Output is correct |
23 |
Correct |
173 ms |
11296 KB |
Output is correct |
24 |
Correct |
143 ms |
11224 KB |
Output is correct |
25 |
Correct |
129 ms |
11400 KB |
Output is correct |
26 |
Correct |
127 ms |
11252 KB |
Output is correct |
27 |
Correct |
137 ms |
11292 KB |
Output is correct |
28 |
Correct |
176 ms |
11324 KB |
Output is correct |
29 |
Correct |
146 ms |
11356 KB |
Output is correct |
30 |
Correct |
141 ms |
11300 KB |
Output is correct |
31 |
Correct |
140 ms |
11280 KB |
Output is correct |
32 |
Correct |
147 ms |
11308 KB |
Output is correct |
33 |
Correct |
148 ms |
11220 KB |
Output is correct |
34 |
Correct |
161 ms |
11300 KB |
Output is correct |
35 |
Correct |
150 ms |
11340 KB |
Output is correct |
36 |
Correct |
160 ms |
11200 KB |
Output is correct |
37 |
Correct |
173 ms |
11268 KB |
Output is correct |
38 |
Correct |
165 ms |
11404 KB |
Output is correct |
39 |
Correct |
130 ms |
11240 KB |
Output is correct |
40 |
Correct |
140 ms |
11256 KB |
Output is correct |
41 |
Correct |
135 ms |
11220 KB |
Output is correct |
42 |
Correct |
148 ms |
11324 KB |
Output is correct |
43 |
Correct |
160 ms |
11184 KB |
Output is correct |
44 |
Correct |
135 ms |
11260 KB |
Output is correct |
45 |
Correct |
134 ms |
11436 KB |
Output is correct |
46 |
Correct |
149 ms |
11240 KB |
Output is correct |
47 |
Correct |
141 ms |
11288 KB |
Output is correct |
48 |
Correct |
154 ms |
11312 KB |
Output is correct |
49 |
Correct |
138 ms |
11216 KB |
Output is correct |
50 |
Correct |
139 ms |
11304 KB |
Output is correct |