#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<ll, ll, ll> iii;
int n;
set<iii> VLR, LRV;
ll ans = 0;
int main(){
scanf("%d",&n);
for (int i = 0; i < n; i++){
ll x;
scanf("%lld",&x);
VLR.insert({x,i,i});
LRV.insert({i,i,x});
}
for (int i = 1; i <= (n+1)/2; i++){
/*for (auto x : VLR){
ll a,b,c;
tie(a,b,c) = x;
printf("<%lld %lld %lld>",a,b,c);
}
printf("<- VLR\n");
for (auto x : LRV){
ll a,b,c;
tie(a,b,c) = x;
printf("<%lld %lld %lld>",a,b,c);
}
printf("<- LRV\n");*/
ll v,l,r;
tie(v,l,r) = *(--VLR.end());
//printf("taking %lld %lld %lld\n",l,r,v);
ans += v;
ll nv = -v, nl = l, nr = r;
auto it = LRV.find({l,r,v});
assert(it != LRV.end());
bool bad = 0;
if (it != LRV.begin()){
auto itL = prev(it);
ll vL, lL, rL;
tie(lL, rL, vL) = *itL;
nv += vL;
VLR.erase({vL,lL,rL});
LRV.erase(itL);
nl = lL;
it = LRV.find({l,r,v});
}
else bad = 1;
if (next(it) != LRV.end()){
auto itR = next(it);
ll vR, lR, rR;
tie(lR, rR, vR) = *itR;
nv += vR;
VLR.erase({vR,lR,rR});
LRV.erase(itR);
nr = rR;
it = LRV.find({l,r,v});
}
else bad = 1;
VLR.erase(--VLR.end());
LRV.erase(it);
//printf("adding %lld %lld %lld\n",nl,nr,nv);
if (!bad){
VLR.insert({nv,nl,nr});
LRV.insert({nl,nr,nv});
}
printf("%lld\n",ans);
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
candies.cpp:12:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&x);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
3 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
597 ms |
27004 KB |
Output is correct |
22 |
Correct |
589 ms |
26912 KB |
Output is correct |
23 |
Correct |
594 ms |
26872 KB |
Output is correct |
24 |
Correct |
289 ms |
26868 KB |
Output is correct |
25 |
Correct |
290 ms |
27000 KB |
Output is correct |
26 |
Correct |
285 ms |
26872 KB |
Output is correct |
27 |
Correct |
314 ms |
26872 KB |
Output is correct |
28 |
Correct |
307 ms |
27000 KB |
Output is correct |
29 |
Correct |
308 ms |
26872 KB |
Output is correct |
30 |
Correct |
329 ms |
27128 KB |
Output is correct |
31 |
Correct |
337 ms |
26872 KB |
Output is correct |
32 |
Correct |
330 ms |
26872 KB |
Output is correct |
33 |
Correct |
477 ms |
27000 KB |
Output is correct |
34 |
Correct |
436 ms |
27000 KB |
Output is correct |
35 |
Correct |
432 ms |
26872 KB |
Output is correct |
36 |
Correct |
626 ms |
26952 KB |
Output is correct |
37 |
Correct |
624 ms |
27000 KB |
Output is correct |
38 |
Correct |
601 ms |
27140 KB |
Output is correct |
39 |
Correct |
297 ms |
27000 KB |
Output is correct |
40 |
Correct |
291 ms |
27000 KB |
Output is correct |
41 |
Correct |
288 ms |
27000 KB |
Output is correct |
42 |
Correct |
307 ms |
27004 KB |
Output is correct |
43 |
Correct |
308 ms |
26872 KB |
Output is correct |
44 |
Correct |
305 ms |
26872 KB |
Output is correct |
45 |
Correct |
327 ms |
26980 KB |
Output is correct |
46 |
Correct |
324 ms |
26872 KB |
Output is correct |
47 |
Correct |
328 ms |
26872 KB |
Output is correct |
48 |
Correct |
440 ms |
26896 KB |
Output is correct |
49 |
Correct |
435 ms |
27000 KB |
Output is correct |
50 |
Correct |
464 ms |
26872 KB |
Output is correct |