#include <iostream>
#include <queue>
using namespace std;
#define endl '\n'
const int MAXN=2e5+10;
struct Node {
long long pl;
int l, r;
int idx;
bool friend operator < (Node a, Node b) {
return a.pl<b.pl;
}
};
priority_queue <Node> q;
int lef[MAXN], ri[MAXN], ind[MAXN], cnt;
long long a[MAXN], pref[MAXN];
bool used[2*MAXN];
long long find_sum(int l, int r) {
if (l==1) return pref[r];
return pref[r]-pref[l-2];
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i=1;i<=n;i++) {
cin >> a[i];
if (i==1) pref[i]=a[i];
else pref[i]=pref[i-2]+a[i];
lef[i]=ri[i]=i;
q.push({a[i],i,i,i});
ind[i]=i;
}
long long sum=0;
cnt=n+2;
for (int i=1;i<=n/2+n%2;i++) {
while (used[q.top().idx]) q.pop();
auto p=q.top();
q.pop();
sum+=p.pl;
cout << sum << endl;
used[p.idx]=true;
if (p.l==1) {
used[ind[ri[p.r+1]]]=used[ind[p.r+1]]=true;
lef[ri[p.r+1]]=0;
continue;
}
if (p.r==n) {
used[ind[lef[p.l-1]]]=used[ind[p.l-1]]=true;
ri[lef[p.l-1]]=n+1;
continue;
}
int newl, newr;
newl=lef[p.l-1];
newr=ri[p.r+1];
ri[newl]=newr;
lef[newr]=newl;
used[ind[newl]]=used[ind[newr]]=true;
used[ind[p.l-1]]=used[ind[p.r+1]]=true;
if (newl==0 || newr==n+1) continue;
cnt++;
ind[newl]=ind[newr]=cnt;
long long ch=find_sum(newl,newr)-find_sum(newl+1,newr-1);
q.push({ch,newl,newr,cnt});
}
return 0;
}
/*
10
3 10 5 1 8 6 4 7 2 9
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4704 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4576 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4704 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4576 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
83 ms |
15880 KB |
Output is correct |
22 |
Correct |
82 ms |
15724 KB |
Output is correct |
23 |
Correct |
87 ms |
15536 KB |
Output is correct |
24 |
Correct |
61 ms |
14540 KB |
Output is correct |
25 |
Correct |
60 ms |
15544 KB |
Output is correct |
26 |
Correct |
68 ms |
15540 KB |
Output is correct |
27 |
Correct |
58 ms |
15696 KB |
Output is correct |
28 |
Correct |
59 ms |
15560 KB |
Output is correct |
29 |
Correct |
60 ms |
15768 KB |
Output is correct |
30 |
Correct |
61 ms |
14664 KB |
Output is correct |
31 |
Correct |
59 ms |
14472 KB |
Output is correct |
32 |
Correct |
67 ms |
15932 KB |
Output is correct |
33 |
Correct |
68 ms |
15024 KB |
Output is correct |
34 |
Correct |
65 ms |
14284 KB |
Output is correct |
35 |
Correct |
65 ms |
15544 KB |
Output is correct |
36 |
Correct |
106 ms |
15136 KB |
Output is correct |
37 |
Correct |
84 ms |
14772 KB |
Output is correct |
38 |
Correct |
85 ms |
15536 KB |
Output is correct |
39 |
Correct |
62 ms |
14284 KB |
Output is correct |
40 |
Correct |
61 ms |
14752 KB |
Output is correct |
41 |
Correct |
61 ms |
14792 KB |
Output is correct |
42 |
Correct |
69 ms |
15024 KB |
Output is correct |
43 |
Correct |
64 ms |
15184 KB |
Output is correct |
44 |
Correct |
58 ms |
14792 KB |
Output is correct |
45 |
Correct |
68 ms |
14632 KB |
Output is correct |
46 |
Correct |
59 ms |
14992 KB |
Output is correct |
47 |
Correct |
58 ms |
15040 KB |
Output is correct |
48 |
Correct |
68 ms |
14920 KB |
Output is correct |
49 |
Correct |
74 ms |
14796 KB |
Output is correct |
50 |
Correct |
82 ms |
15028 KB |
Output is correct |