#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using State = array<array<vector<ll>, 2>, 2>;
vector<ll> merge(const vector<ll> &lf, const vector<ll> &rg) {
if (lf.empty()) return rg;
if (rg.empty()) return lf;
deque<ll> lfd;
deque<ll> rgd;
for (int i = 0; i + 1 < int(lf.size()); ++i) {
lfd.emplace_back(lf[i + 1] - lf[i]);
}
for (int i = 0; i + 1 < int(rg.size()); ++i) {
rgd.emplace_back(rg[i + 1] - rg[i]);
}
vector<ll> ans = {lf[0] + rg[0]};
while (lfd.size() && rgd.size()) {
if (lfd.front() > rgd.front()) {
ans.emplace_back(ans.back() + lfd.front());
lfd.pop_front();
} else {
ans.emplace_back(ans.back() + rgd.front());
rgd.pop_front();
}
}
while (lfd.size()) {
ans.emplace_back(ans.back() + lfd.front());
lfd.pop_front();
}
while (rgd.size()) {
ans.emplace_back(ans.back() + rgd.front());
rgd.pop_front();
}
return ans;
}
void optimize(vector<ll> &x, const vector<ll> &y) {
while (x.size() < y.size()) {
x.emplace_back(0ll);
}
for (int i = 0; i < int(y.size()); ++i) {
x[i] = max(x[i], y[i]);
}
}
State merge(const State &lf, const State &rg) {
State ans = {};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
if (!j || !k) {
optimize(ans[i][l], merge(lf[i][j], rg[k][l]));
}
}
}
}
}
return ans;
}
void modify(State &s) {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
if (i) optimize(s[i][j], s[i - 1][j]);
if (j) optimize(s[i][j], s[i][j - 1]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
function<State(int, int)> solve = [&](int l, int r) {
State ans = {};
if (l == r) {
ans[0][0] = {0};
ans[1][1] = {0, A[l]};
} else {
int md = (l + r) >> 1;
ans = merge(solve(l, md), solve(md + 1, r));
}
modify(ans);
return ans;
};
auto ans = solve(0, N - 1);
for (int i = 1; i <= (N + 1) / 2; ++i) cout << ans[1][1][i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
504 KB |
Output is correct |
2 |
Correct |
18 ms |
504 KB |
Output is correct |
3 |
Correct |
18 ms |
504 KB |
Output is correct |
4 |
Correct |
19 ms |
376 KB |
Output is correct |
5 |
Correct |
18 ms |
504 KB |
Output is correct |
6 |
Correct |
17 ms |
508 KB |
Output is correct |
7 |
Correct |
18 ms |
504 KB |
Output is correct |
8 |
Correct |
20 ms |
504 KB |
Output is correct |
9 |
Correct |
18 ms |
504 KB |
Output is correct |
10 |
Correct |
17 ms |
504 KB |
Output is correct |
11 |
Correct |
18 ms |
504 KB |
Output is correct |
12 |
Correct |
18 ms |
504 KB |
Output is correct |
13 |
Correct |
18 ms |
504 KB |
Output is correct |
14 |
Correct |
18 ms |
504 KB |
Output is correct |
15 |
Correct |
18 ms |
376 KB |
Output is correct |
16 |
Correct |
18 ms |
504 KB |
Output is correct |
17 |
Correct |
18 ms |
504 KB |
Output is correct |
18 |
Correct |
18 ms |
504 KB |
Output is correct |
19 |
Correct |
18 ms |
508 KB |
Output is correct |
20 |
Correct |
18 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
504 KB |
Output is correct |
2 |
Correct |
18 ms |
504 KB |
Output is correct |
3 |
Correct |
18 ms |
504 KB |
Output is correct |
4 |
Correct |
19 ms |
376 KB |
Output is correct |
5 |
Correct |
18 ms |
504 KB |
Output is correct |
6 |
Correct |
17 ms |
508 KB |
Output is correct |
7 |
Correct |
18 ms |
504 KB |
Output is correct |
8 |
Correct |
20 ms |
504 KB |
Output is correct |
9 |
Correct |
18 ms |
504 KB |
Output is correct |
10 |
Correct |
17 ms |
504 KB |
Output is correct |
11 |
Correct |
18 ms |
504 KB |
Output is correct |
12 |
Correct |
18 ms |
504 KB |
Output is correct |
13 |
Correct |
18 ms |
504 KB |
Output is correct |
14 |
Correct |
18 ms |
504 KB |
Output is correct |
15 |
Correct |
18 ms |
376 KB |
Output is correct |
16 |
Correct |
18 ms |
504 KB |
Output is correct |
17 |
Correct |
18 ms |
504 KB |
Output is correct |
18 |
Correct |
18 ms |
504 KB |
Output is correct |
19 |
Correct |
18 ms |
508 KB |
Output is correct |
20 |
Correct |
18 ms |
504 KB |
Output is correct |
21 |
Correct |
1499 ms |
15072 KB |
Output is correct |
22 |
Correct |
1461 ms |
15624 KB |
Output is correct |
23 |
Correct |
1437 ms |
13600 KB |
Output is correct |
24 |
Correct |
1411 ms |
13480 KB |
Output is correct |
25 |
Correct |
1375 ms |
13436 KB |
Output is correct |
26 |
Correct |
1391 ms |
13456 KB |
Output is correct |
27 |
Correct |
1423 ms |
13608 KB |
Output is correct |
28 |
Correct |
1397 ms |
13608 KB |
Output is correct |
29 |
Correct |
1418 ms |
13604 KB |
Output is correct |
30 |
Correct |
1410 ms |
13600 KB |
Output is correct |
31 |
Correct |
1387 ms |
15268 KB |
Output is correct |
32 |
Correct |
1403 ms |
13608 KB |
Output is correct |
33 |
Correct |
1417 ms |
15340 KB |
Output is correct |
34 |
Correct |
1396 ms |
13732 KB |
Output is correct |
35 |
Correct |
1419 ms |
14980 KB |
Output is correct |
36 |
Correct |
1471 ms |
13500 KB |
Output is correct |
37 |
Correct |
1460 ms |
14916 KB |
Output is correct |
38 |
Correct |
1462 ms |
13528 KB |
Output is correct |
39 |
Correct |
1382 ms |
13416 KB |
Output is correct |
40 |
Correct |
1353 ms |
13388 KB |
Output is correct |
41 |
Correct |
1389 ms |
13376 KB |
Output is correct |
42 |
Correct |
1392 ms |
13556 KB |
Output is correct |
43 |
Correct |
1371 ms |
13760 KB |
Output is correct |
44 |
Correct |
1357 ms |
13640 KB |
Output is correct |
45 |
Correct |
1416 ms |
13644 KB |
Output is correct |
46 |
Correct |
1405 ms |
15088 KB |
Output is correct |
47 |
Correct |
1387 ms |
13600 KB |
Output is correct |
48 |
Correct |
1448 ms |
15232 KB |
Output is correct |
49 |
Correct |
1402 ms |
14864 KB |
Output is correct |
50 |
Correct |
1421 ms |
14888 KB |
Output is correct |