#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10,inf=1e18;
int pre[N],nxt[N],st[N],c[N],n,ans;
void del(int u){
pre[nxt[u]]=pre[u];
nxt[pre[u]]=nxt[u];
st[u]=1;
}
priority_queue<pair<int,int> > q;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
pre[i]=i-1,nxt[i]=i+1;
q.push({c[i],i});
}
c[0]=c[++n]=-inf;
for(int i=1;i<=n/2;i++){
while(st[q.top().second]) q.pop();
auto [w,p]=q.top();q.pop();
ans+=w;cout<<ans<<"\n";
c[p]=c[pre[p]]+c[nxt[p]]-c[p];
q.push({c[p],p});
del(pre[p]);del(nxt[p]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2808 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2528 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2528 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2528 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2808 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2528 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2528 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2528 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
74 ms |
13268 KB |
Output is correct |
22 |
Correct |
75 ms |
13504 KB |
Output is correct |
23 |
Correct |
70 ms |
13380 KB |
Output is correct |
24 |
Correct |
57 ms |
13952 KB |
Output is correct |
25 |
Correct |
55 ms |
13584 KB |
Output is correct |
26 |
Correct |
55 ms |
13252 KB |
Output is correct |
27 |
Correct |
62 ms |
13256 KB |
Output is correct |
28 |
Correct |
64 ms |
13240 KB |
Output is correct |
29 |
Correct |
63 ms |
13308 KB |
Output is correct |
30 |
Correct |
64 ms |
13312 KB |
Output is correct |
31 |
Correct |
63 ms |
13284 KB |
Output is correct |
32 |
Correct |
63 ms |
13248 KB |
Output is correct |
33 |
Correct |
67 ms |
13584 KB |
Output is correct |
34 |
Correct |
65 ms |
13188 KB |
Output is correct |
35 |
Correct |
67 ms |
13992 KB |
Output is correct |
36 |
Correct |
74 ms |
13760 KB |
Output is correct |
37 |
Correct |
75 ms |
13928 KB |
Output is correct |
38 |
Correct |
73 ms |
13244 KB |
Output is correct |
39 |
Correct |
54 ms |
13208 KB |
Output is correct |
40 |
Correct |
56 ms |
14024 KB |
Output is correct |
41 |
Correct |
57 ms |
13244 KB |
Output is correct |
42 |
Correct |
62 ms |
13252 KB |
Output is correct |
43 |
Correct |
63 ms |
13216 KB |
Output is correct |
44 |
Correct |
63 ms |
13900 KB |
Output is correct |
45 |
Correct |
70 ms |
13304 KB |
Output is correct |
46 |
Correct |
67 ms |
13244 KB |
Output is correct |
47 |
Correct |
62 ms |
13360 KB |
Output is correct |
48 |
Correct |
64 ms |
13008 KB |
Output is correct |
49 |
Correct |
66 ms |
13044 KB |
Output is correct |
50 |
Correct |
67 ms |
12992 KB |
Output is correct |