#include <bits/stdc++.h>
using namespace std;
#define int long long
multiset<pair<int, pair<int, int> > >s;
multiset<pair<int, pair<int, int> > >:: iterator it;
int A[200005], p[200005];
pair<int, pair<int, int> >r[200005];
int px(int x){
if(p[x] == x)return p[x];
else return p[x] = px(p[x]);
}
void merge(int a, int b, int c){
a = px(a), b = px(b), c = px(c);
if(a != b){
p[c] = p[b] = a;
r[a].first += r[c].first - r[b].first;
r[a].second.first = min({r[a].second.first, r[b].second.first, r[c].second.first});
r[a].second.second = max({r[c].second.second, r[a].second.second, r[b].second.second});
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int ans = 0;
int n;cin >> n;
r[0].first = -1e15;
r[0].second.first = n + 1;
for(int i=1;i<=n;i++)cin >> A[i], s.insert(make_pair(A[i], make_pair(i,i))), p[i] = i, r[i].first = A[i], r[i].second.first = r[i].second.second = i;
for(int i=1;i<=(n+1)/2;i++){
it = --s.end();
int x = it->first;
int y = it->second.first;
int z = it->second.second;
while(!s.empty() && (r[px(y)].second.first != y || r[px(y)].second.second != z)){
s.erase(it);
it = --s.end();
x = it->first;
y = it->second.first;
z = it->second.second;
}
ans += x;
merge(y-1,y,z+1);
s.insert(r[px(y)]);
cout << ans << '\n';
}
}
Compilation message
candies.cpp:21:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
21 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
464 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 |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
572 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
464 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 |
468 KB |
Output is correct |
21 |
Correct |
219 ms |
24056 KB |
Output is correct |
22 |
Correct |
245 ms |
24076 KB |
Output is correct |
23 |
Correct |
229 ms |
24016 KB |
Output is correct |
24 |
Correct |
104 ms |
23832 KB |
Output is correct |
25 |
Correct |
117 ms |
23852 KB |
Output is correct |
26 |
Correct |
109 ms |
23860 KB |
Output is correct |
27 |
Correct |
121 ms |
24048 KB |
Output is correct |
28 |
Correct |
119 ms |
24012 KB |
Output is correct |
29 |
Correct |
121 ms |
24004 KB |
Output is correct |
30 |
Correct |
122 ms |
24000 KB |
Output is correct |
31 |
Correct |
124 ms |
24004 KB |
Output is correct |
32 |
Correct |
122 ms |
24092 KB |
Output is correct |
33 |
Correct |
164 ms |
23900 KB |
Output is correct |
34 |
Correct |
153 ms |
23824 KB |
Output is correct |
35 |
Correct |
155 ms |
23892 KB |
Output is correct |
36 |
Correct |
215 ms |
24064 KB |
Output is correct |
37 |
Correct |
214 ms |
24060 KB |
Output is correct |
38 |
Correct |
217 ms |
24116 KB |
Output is correct |
39 |
Correct |
105 ms |
23836 KB |
Output is correct |
40 |
Correct |
105 ms |
23880 KB |
Output is correct |
41 |
Correct |
105 ms |
23804 KB |
Output is correct |
42 |
Correct |
120 ms |
23988 KB |
Output is correct |
43 |
Correct |
116 ms |
24048 KB |
Output is correct |
44 |
Correct |
117 ms |
24000 KB |
Output is correct |
45 |
Correct |
124 ms |
24140 KB |
Output is correct |
46 |
Correct |
121 ms |
23988 KB |
Output is correct |
47 |
Correct |
123 ms |
24080 KB |
Output is correct |
48 |
Correct |
156 ms |
23752 KB |
Output is correct |
49 |
Correct |
156 ms |
23844 KB |
Output is correct |
50 |
Correct |
155 ms |
23840 KB |
Output is correct |