#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 200'010;
ll a[N];
struct Obj {
int l, r;
ll val0, val1;
} obj[N];
int rt[N];
ll val(int x) { return obj[x].val0 - obj[x].val1; }
set<pair<ll, int>, greater<pair<ll, int>>> pq;
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int n;
cin >> n;
Loop (i,0,n+2) {
if (i==0 || i==n+1)
a[i] = -1e18;
else
cin >> a[i];
obj[i].l = obj[i].r = i;
obj[i].val0 = a[i];
pq.insert({val(i), i});
rt[i] = i;
}
ll sum = 0;
Loop (i,0,(n+1)/2) {
int p = pq.begin()->second;
sum += val(p);
cout << sum << '\n';
int lrt = rt[obj[p].l-1];
int rrt = rt[obj[p].r+1];
pq.erase({val(p), p});
pq.erase({val(lrt), lrt});
pq.erase({val(rrt), rrt});
rt[obj[lrt].l] = p;
rt[obj[rrt].r] = p;
swap(obj[p].val0, obj[p].val1);
obj[p].val0 += obj[lrt].val0;
obj[p].val0 += obj[rrt].val0;
obj[p].val1 += obj[lrt].val1;
obj[p].val1 += obj[rrt].val1;
obj[p].l = obj[lrt].l;
obj[p].r = obj[rrt].r;
pq.insert({val(p), p});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
548 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
548 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
464 KB |
Output is correct |
7 |
Correct |
2 ms |
464 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 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 |
2 ms |
464 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
301 ms |
23260 KB |
Output is correct |
22 |
Correct |
294 ms |
23244 KB |
Output is correct |
23 |
Correct |
309 ms |
23260 KB |
Output is correct |
24 |
Correct |
127 ms |
23116 KB |
Output is correct |
25 |
Correct |
126 ms |
23076 KB |
Output is correct |
26 |
Correct |
135 ms |
23148 KB |
Output is correct |
27 |
Correct |
142 ms |
23288 KB |
Output is correct |
28 |
Correct |
143 ms |
23276 KB |
Output is correct |
29 |
Correct |
143 ms |
23288 KB |
Output is correct |
30 |
Correct |
149 ms |
23276 KB |
Output is correct |
31 |
Correct |
147 ms |
23280 KB |
Output is correct |
32 |
Correct |
148 ms |
23244 KB |
Output is correct |
33 |
Correct |
217 ms |
23080 KB |
Output is correct |
34 |
Correct |
213 ms |
23008 KB |
Output is correct |
35 |
Correct |
217 ms |
23284 KB |
Output is correct |
36 |
Correct |
309 ms |
23240 KB |
Output is correct |
37 |
Correct |
298 ms |
23248 KB |
Output is correct |
38 |
Correct |
304 ms |
23240 KB |
Output is correct |
39 |
Correct |
135 ms |
23044 KB |
Output is correct |
40 |
Correct |
126 ms |
23052 KB |
Output is correct |
41 |
Correct |
127 ms |
23044 KB |
Output is correct |
42 |
Correct |
140 ms |
23244 KB |
Output is correct |
43 |
Correct |
138 ms |
23304 KB |
Output is correct |
44 |
Correct |
144 ms |
23308 KB |
Output is correct |
45 |
Correct |
148 ms |
23280 KB |
Output is correct |
46 |
Correct |
149 ms |
23308 KB |
Output is correct |
47 |
Correct |
152 ms |
23340 KB |
Output is correct |
48 |
Correct |
221 ms |
23000 KB |
Output is correct |
49 |
Correct |
213 ms |
23004 KB |
Output is correct |
50 |
Correct |
223 ms |
23200 KB |
Output is correct |