#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, t, a[N], l[N], r[N], ok[N];
main() {
cin >> n; t=(n+1)/2; 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;
while(t--){
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 |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 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 |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 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 |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
150 ms |
11296 KB |
Output is correct |
22 |
Correct |
163 ms |
11280 KB |
Output is correct |
23 |
Correct |
152 ms |
11184 KB |
Output is correct |
24 |
Correct |
129 ms |
11220 KB |
Output is correct |
25 |
Correct |
134 ms |
11312 KB |
Output is correct |
26 |
Correct |
128 ms |
11232 KB |
Output is correct |
27 |
Correct |
137 ms |
11260 KB |
Output is correct |
28 |
Correct |
145 ms |
11184 KB |
Output is correct |
29 |
Correct |
143 ms |
11220 KB |
Output is correct |
30 |
Correct |
149 ms |
11276 KB |
Output is correct |
31 |
Correct |
149 ms |
11216 KB |
Output is correct |
32 |
Correct |
136 ms |
11184 KB |
Output is correct |
33 |
Correct |
144 ms |
11232 KB |
Output is correct |
34 |
Correct |
133 ms |
11248 KB |
Output is correct |
35 |
Correct |
144 ms |
11272 KB |
Output is correct |
36 |
Correct |
151 ms |
11300 KB |
Output is correct |
37 |
Correct |
158 ms |
11268 KB |
Output is correct |
38 |
Correct |
178 ms |
11232 KB |
Output is correct |
39 |
Correct |
131 ms |
11224 KB |
Output is correct |
40 |
Correct |
119 ms |
11280 KB |
Output is correct |
41 |
Correct |
124 ms |
11180 KB |
Output is correct |
42 |
Correct |
141 ms |
11184 KB |
Output is correct |
43 |
Correct |
141 ms |
11304 KB |
Output is correct |
44 |
Correct |
136 ms |
11268 KB |
Output is correct |
45 |
Correct |
148 ms |
11220 KB |
Output is correct |
46 |
Correct |
155 ms |
11260 KB |
Output is correct |
47 |
Correct |
156 ms |
11252 KB |
Output is correct |
48 |
Correct |
143 ms |
11236 KB |
Output is correct |
49 |
Correct |
145 ms |
11312 KB |
Output is correct |
50 |
Correct |
150 ms |
11304 KB |
Output is correct |