#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define x first
#define y second
#define pi pair<ll,ll>
int main(){
ios::sync_with_stdio(0); cin.tie(0);
ll n; cin>>n;
vector<ll> v(n); for(ll &i : v) cin>>i;
ll k = (n+1)/2;
priority_queue<pi> pq;
ll l[n],r[n];
for(ll i=0;i<n;i++){
pq.push({v[i],i});
l[i]=i-1;r[i]=i+1;
}
vector<bool> vis(n,0);
ll res=0;
for(ll i=0; i< (n+1)/2;i++){
while(vis[pq.top().y]) pq.pop();
pi p = pq.top(); pq.pop();
res += p.x;
cout<<res<<endl;
ll cur = p.y;
if(r[cur]==n){
vis[cur]=1;
if(l[cur]!=-1)vis[l[cur]]=1;
if(l[l[cur]] > -1) r[l[l[cur]]]=n;
}
else if(l[cur]==-1){
vis[cur]=1;
if(r[cur]!=n)vis[r[cur]]=1;
if(r[r[cur]] < n) l[r[r[cur]]]=-1;
}
else{
v[cur] = v[r[cur]]+v[l[cur]] - v[cur];
vis[r[cur]]=1;
r[cur] = r[r[cur]];
if(r[cur] != n) l[r[cur]]=cur;
vis[l[cur]]=1;
l[cur] = l[l[cur]];
if(l[cur] != -1) r[l[cur]]=cur;
pq.push({v[cur],cur});
}
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:12:8: warning: unused variable 'k' [-Wunused-variable]
12 | ll k = (n+1)/2;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
189 ms |
12756 KB |
Output is correct |
22 |
Correct |
185 ms |
11988 KB |
Output is correct |
23 |
Correct |
177 ms |
12496 KB |
Output is correct |
24 |
Correct |
161 ms |
11728 KB |
Output is correct |
25 |
Correct |
169 ms |
12796 KB |
Output is correct |
26 |
Correct |
165 ms |
12500 KB |
Output is correct |
27 |
Correct |
205 ms |
11716 KB |
Output is correct |
28 |
Correct |
174 ms |
12776 KB |
Output is correct |
29 |
Correct |
170 ms |
12232 KB |
Output is correct |
30 |
Correct |
186 ms |
12488 KB |
Output is correct |
31 |
Correct |
177 ms |
12492 KB |
Output is correct |
32 |
Correct |
168 ms |
11712 KB |
Output is correct |
33 |
Correct |
175 ms |
12236 KB |
Output is correct |
34 |
Correct |
187 ms |
12232 KB |
Output is correct |
35 |
Correct |
171 ms |
11452 KB |
Output is correct |
36 |
Correct |
179 ms |
11840 KB |
Output is correct |
37 |
Correct |
191 ms |
12252 KB |
Output is correct |
38 |
Correct |
181 ms |
12752 KB |
Output is correct |
39 |
Correct |
165 ms |
11732 KB |
Output is correct |
40 |
Correct |
164 ms |
11720 KB |
Output is correct |
41 |
Correct |
184 ms |
12020 KB |
Output is correct |
42 |
Correct |
172 ms |
11716 KB |
Output is correct |
43 |
Correct |
177 ms |
11904 KB |
Output is correct |
44 |
Correct |
167 ms |
12496 KB |
Output is correct |
45 |
Correct |
177 ms |
12744 KB |
Output is correct |
46 |
Correct |
167 ms |
11980 KB |
Output is correct |
47 |
Correct |
174 ms |
11988 KB |
Output is correct |
48 |
Correct |
187 ms |
12500 KB |
Output is correct |
49 |
Correct |
175 ms |
12744 KB |
Output is correct |
50 |
Correct |
171 ms |
12488 KB |
Output is correct |