#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll prt[200009];
ll val[200009];
ll lef[200009];
ll rig[200009];
ll evsum[200009];
ll oddsum[200009];
ll findprt(ll x){
if(prt[x] == x)
return x;
return (prt[x] = findprt(prt[x]));
}
void merge(ll x, ll y){
x = findprt(x);
y = findprt(y);
if(x == y) return;
prt[x] = y;
val[y] += val[x];
lef[y] = min(lef[x], lef[y]);
rig[y] = max(rig[x], rig[y]);
evsum[y] += evsum[x];
oddsum[y] += oddsum[x];
}
ll n;
ll a[200009];
ll ans[200009];
set<pll, greater<pll>> s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 0; i < n; ++i){
cin >> a[i];
prt[i] = i;
val[i] = a[i];
lef[i] = rig[i] = i;
if(i%2)
oddsum[i] = a[i];
else
evsum[i] = a[i];
s.insert({val[i], i});
}
ll sz = 0;
ll cur = 0;
while(sz < (n+1)/2){
pll curit = *s.begin();
s.erase(s.begin());
ll curnode = curit.ss;
cur += curit.ff;
ans[++sz] = cur;
//cout << lef[curnode] << ' ' << rig[curnode] << '\n';
ll befl = lef[curnode];
ll befr = rig[curnode];
if(lef[curnode]-1 >= 0){
ll x = findprt(lef[curnode]-1);
s.erase({val[x], x});
merge(x, curnode);
}
curnode = findprt(curnode);
if(rig[curnode]+1 < n){
ll x = findprt(rig[curnode]+1);
s.erase({val[x], x});
merge(x, curnode);
}
curnode = findprt(curnode);
lef[curnode] = min(befl-1, lef[curnode]);
rig[curnode] = max(befr+1, rig[curnode]);
if(befl%2 == 0)
val[curnode] = oddsum[curnode]-evsum[curnode];
else
val[curnode] = evsum[curnode]-oddsum[curnode];
if(lef[curnode] >= 0 && rig[curnode] < n)
s.insert({val[curnode], curnode});
}
for(ll i = 1; i <= (n+1)/2; ++i)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
5 |
Correct |
6 ms |
632 KB |
Output is correct |
6 |
Correct |
6 ms |
760 KB |
Output is correct |
7 |
Correct |
6 ms |
632 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
632 KB |
Output is correct |
13 |
Correct |
6 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
6 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 KB |
Output is correct |
17 |
Correct |
6 ms |
632 KB |
Output is correct |
18 |
Correct |
6 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
632 KB |
Output is correct |
20 |
Correct |
6 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
632 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
5 |
Correct |
6 ms |
632 KB |
Output is correct |
6 |
Correct |
6 ms |
760 KB |
Output is correct |
7 |
Correct |
6 ms |
632 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
632 KB |
Output is correct |
13 |
Correct |
6 ms |
632 KB |
Output is correct |
14 |
Correct |
6 ms |
632 KB |
Output is correct |
15 |
Correct |
6 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 KB |
Output is correct |
17 |
Correct |
6 ms |
632 KB |
Output is correct |
18 |
Correct |
6 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
632 KB |
Output is correct |
20 |
Correct |
6 ms |
636 KB |
Output is correct |
21 |
Correct |
450 ms |
28000 KB |
Output is correct |
22 |
Correct |
433 ms |
28204 KB |
Output is correct |
23 |
Correct |
449 ms |
28152 KB |
Output is correct |
24 |
Correct |
179 ms |
27896 KB |
Output is correct |
25 |
Correct |
187 ms |
28024 KB |
Output is correct |
26 |
Correct |
179 ms |
27896 KB |
Output is correct |
27 |
Correct |
192 ms |
28092 KB |
Output is correct |
28 |
Correct |
192 ms |
28024 KB |
Output is correct |
29 |
Correct |
196 ms |
28024 KB |
Output is correct |
30 |
Correct |
208 ms |
28152 KB |
Output is correct |
31 |
Correct |
209 ms |
28024 KB |
Output is correct |
32 |
Correct |
208 ms |
28024 KB |
Output is correct |
33 |
Correct |
308 ms |
27768 KB |
Output is correct |
34 |
Correct |
323 ms |
27896 KB |
Output is correct |
35 |
Correct |
305 ms |
27896 KB |
Output is correct |
36 |
Correct |
451 ms |
28152 KB |
Output is correct |
37 |
Correct |
434 ms |
28024 KB |
Output is correct |
38 |
Correct |
440 ms |
28024 KB |
Output is correct |
39 |
Correct |
184 ms |
27896 KB |
Output is correct |
40 |
Correct |
197 ms |
28024 KB |
Output is correct |
41 |
Correct |
175 ms |
27896 KB |
Output is correct |
42 |
Correct |
201 ms |
28032 KB |
Output is correct |
43 |
Correct |
193 ms |
28024 KB |
Output is correct |
44 |
Correct |
192 ms |
28024 KB |
Output is correct |
45 |
Correct |
206 ms |
28024 KB |
Output is correct |
46 |
Correct |
209 ms |
28224 KB |
Output is correct |
47 |
Correct |
222 ms |
28268 KB |
Output is correct |
48 |
Correct |
303 ms |
27768 KB |
Output is correct |
49 |
Correct |
313 ms |
28028 KB |
Output is correct |
50 |
Correct |
309 ms |
27768 KB |
Output is correct |