#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, INF = 5e18;
int n, a[N], ans;
set < int > alive;
set < pair < int, int > > q;
main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
q.insert(make_pair(a[i], i));
}
a[0] = a[n + 1] = -INF;
for(int i = 0; i <= n + 1; i++){
alive.insert(i);
}
for(int i = 1; i <= (n + 1) / 2; i++){
int pos = q.rbegin()->second,
val = q.rbegin()->first;
q.erase(--q.end());
alive.erase(pos);
ans += val;
a[pos] = -a[pos];
auto it = alive.upper_bound(pos);
if(it != alive.end()){
a[pos] += a[*it];
q.erase(make_pair(a[*it], *it));
alive.erase(it);
}
it = alive.upper_bound(pos);
if(it != alive.begin()){
it--;
a[pos] += a[*it];
q.erase(make_pair(a[*it], *it));
alive.erase(it);
}
alive.insert(pos);
q.insert(make_pair(a[pos], pos));
cout << ans << "\n";
}
}
Compilation message
candies.cpp:18:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
10 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
636 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
4 ms |
632 KB |
Output is correct |
16 |
Correct |
4 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
4 ms |
632 KB |
Output is correct |
19 |
Correct |
3 ms |
632 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
10 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
636 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
4 ms |
632 KB |
Output is correct |
16 |
Correct |
4 ms |
632 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
4 ms |
632 KB |
Output is correct |
19 |
Correct |
3 ms |
632 KB |
Output is correct |
20 |
Correct |
3 ms |
636 KB |
Output is correct |
21 |
Correct |
540 ms |
27256 KB |
Output is correct |
22 |
Correct |
520 ms |
27276 KB |
Output is correct |
23 |
Correct |
658 ms |
27268 KB |
Output is correct |
24 |
Correct |
284 ms |
27092 KB |
Output is correct |
25 |
Correct |
273 ms |
27252 KB |
Output is correct |
26 |
Correct |
275 ms |
27172 KB |
Output is correct |
27 |
Correct |
265 ms |
27244 KB |
Output is correct |
28 |
Correct |
258 ms |
27256 KB |
Output is correct |
29 |
Correct |
253 ms |
27256 KB |
Output is correct |
30 |
Correct |
273 ms |
27416 KB |
Output is correct |
31 |
Correct |
277 ms |
27260 KB |
Output is correct |
32 |
Correct |
278 ms |
27344 KB |
Output is correct |
33 |
Correct |
452 ms |
27144 KB |
Output is correct |
34 |
Correct |
456 ms |
27136 KB |
Output is correct |
35 |
Correct |
447 ms |
27256 KB |
Output is correct |
36 |
Correct |
673 ms |
27356 KB |
Output is correct |
37 |
Correct |
637 ms |
27256 KB |
Output is correct |
38 |
Correct |
612 ms |
27300 KB |
Output is correct |
39 |
Correct |
268 ms |
27000 KB |
Output is correct |
40 |
Correct |
276 ms |
27128 KB |
Output is correct |
41 |
Correct |
268 ms |
27124 KB |
Output is correct |
42 |
Correct |
269 ms |
27340 KB |
Output is correct |
43 |
Correct |
259 ms |
27232 KB |
Output is correct |
44 |
Correct |
256 ms |
27384 KB |
Output is correct |
45 |
Correct |
306 ms |
27512 KB |
Output is correct |
46 |
Correct |
312 ms |
27292 KB |
Output is correct |
47 |
Correct |
301 ms |
27256 KB |
Output is correct |
48 |
Correct |
441 ms |
27000 KB |
Output is correct |
49 |
Correct |
473 ms |
27212 KB |
Output is correct |
50 |
Correct |
467 ms |
27128 KB |
Output is correct |