#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1ll << 55;
using D = std::array<std::array<std::vector<i64>, 2>, 2>;
std::vector<i64> mergeConcave(const std::vector<i64> &a, const std::vector<i64> &b) {
const int sl = (int)a.size(), sr = (int)b.size();
std::vector<i64> ret(sl + sr - 1);
int j = 1;
i64 val = a[0] + b[0];
ret[0] = val;
for (int i = 1; i < sl; ++i) {
while (j != sr and b[j] - b[j - 1] > a[i] - a[i - 1]) {
val += b[j] - b[j - 1];
ret[i - 1 + j] = val;
++j;
}
val += a[i] - a[i - 1];
ret[i + j - 1] = val;
}
while (j != sr) {
val += b[j] - b[j - 1];
ret[sl - 1 + j] = val;
++j;
}
return ret;
}
void chmax(std::vector<i64> &a, const std::vector<i64> &b) {
const int n = (int)std::min(a.size(), b.size());
for (int i = 0; i < n; ++i) a[i] = std::max(a[i], b[i]);
}
void solve() {
int N, K;
std::cin >> N >> K;
std::vector<i64> A(N);
for (auto &e : A) std::cin >> e;
auto merge = [&](const D &dl, const D &dr) {
const int sl = (int)dl[0][0].size(), sr = (int)dr[0][0].size();
D ret;
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
ret[x][y].assign(sl + sr - 1, -inf);
}
}
for (int xl = 0; xl < 2; ++xl) {
for (int xr = 0; xr < 2; ++xr) {
for (int yl = 0; yl < 2; ++yl) {
for (int yr = 0; yr < 2; ++yr) {
// merge !
// ret[xl][yr] <- dl[xl][xr] * dr[yl][yr]
if (xr == 1 and yl == 1) {
const auto &lVec = dl[xl][xr], &rVec = dr[yl][yr];
auto mVec = mergeConcave(lVec, rVec);
mVec.erase(mVec.begin());
mVec[0] = -inf;
chmax(ret[xl][yr], mVec);
} else {
const auto &lVec = dl[xl][xr], &rVec = dr[yl][yr];
const auto mVec = mergeConcave(lVec, rVec);
chmax(ret[xl][yr], mVec);
}
}
}
}
}
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
for (int i = 1; i < sl + sr - 1; ++i) {
ret[x][y][i] = std::max(ret[x][y][i], ret[x][y][i - 1]);
}
}
}
chmax(ret[0][0], ret[0][1]);
chmax(ret[1][0], ret[1][1]);
chmax(ret[0][0], ret[1][0]);
chmax(ret[0][1], ret[1][1]);
return ret;
};
auto solve = [&](auto &&self, const int l, const int r) -> D {
if (r - l == 1) {
D ret;
ret[0][0] = {0, A[l]};
ret[0][1] = {-inf, A[l]};
ret[1][0] = {-inf, A[l]};
ret[1][1] = {-inf, A[l]};
return ret;
}
const int m = (l + r) / 2;
const auto lRes = self(self, l, m), rRes = self(self, m, r);
const auto ret = merge(lRes, rRes);
return ret;
};
const auto res = solve(solve, 0, N);
i64 answer = -inf;
for (int i = 0; i <= K; ++i) answer = std::max(answer, res[0][0][K]);
std::cout << answer << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
701 ms |
34724 KB |
Output is correct |
2 |
Correct |
658 ms |
35176 KB |
Output is correct |
3 |
Correct |
663 ms |
34212 KB |
Output is correct |
4 |
Correct |
632 ms |
35352 KB |
Output is correct |
5 |
Correct |
617 ms |
32108 KB |
Output is correct |
6 |
Correct |
613 ms |
32132 KB |
Output is correct |
7 |
Correct |
615 ms |
31128 KB |
Output is correct |
8 |
Correct |
625 ms |
35428 KB |
Output is correct |
9 |
Correct |
624 ms |
32212 KB |
Output is correct |
10 |
Correct |
611 ms |
31376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
603 ms |
31484 KB |
Output is correct |
2 |
Correct |
619 ms |
33016 KB |
Output is correct |
3 |
Correct |
607 ms |
32104 KB |
Output is correct |
4 |
Correct |
619 ms |
33016 KB |
Output is correct |
5 |
Correct |
631 ms |
30644 KB |
Output is correct |
6 |
Correct |
596 ms |
32304 KB |
Output is correct |
7 |
Correct |
608 ms |
35208 KB |
Output is correct |
8 |
Correct |
627 ms |
35120 KB |
Output is correct |
9 |
Correct |
615 ms |
32068 KB |
Output is correct |
10 |
Correct |
625 ms |
31768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
688 ms |
32512 KB |
Output is correct |
2 |
Correct |
698 ms |
32816 KB |
Output is correct |
3 |
Correct |
682 ms |
35772 KB |
Output is correct |
4 |
Correct |
712 ms |
31316 KB |
Output is correct |
5 |
Correct |
683 ms |
32040 KB |
Output is correct |
6 |
Correct |
694 ms |
35076 KB |
Output is correct |
7 |
Correct |
707 ms |
32276 KB |
Output is correct |
8 |
Correct |
685 ms |
33036 KB |
Output is correct |
9 |
Correct |
697 ms |
32120 KB |
Output is correct |
10 |
Correct |
705 ms |
33008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
468 KB |
Output is correct |
22 |
Correct |
4 ms |
468 KB |
Output is correct |
23 |
Correct |
4 ms |
540 KB |
Output is correct |
24 |
Correct |
4 ms |
484 KB |
Output is correct |
25 |
Correct |
6 ms |
484 KB |
Output is correct |
26 |
Correct |
4 ms |
516 KB |
Output is correct |
27 |
Correct |
4 ms |
588 KB |
Output is correct |
28 |
Correct |
5 ms |
584 KB |
Output is correct |
29 |
Correct |
4 ms |
468 KB |
Output is correct |
30 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
701 ms |
34724 KB |
Output is correct |
2 |
Correct |
658 ms |
35176 KB |
Output is correct |
3 |
Correct |
663 ms |
34212 KB |
Output is correct |
4 |
Correct |
632 ms |
35352 KB |
Output is correct |
5 |
Correct |
617 ms |
32108 KB |
Output is correct |
6 |
Correct |
613 ms |
32132 KB |
Output is correct |
7 |
Correct |
615 ms |
31128 KB |
Output is correct |
8 |
Correct |
625 ms |
35428 KB |
Output is correct |
9 |
Correct |
624 ms |
32212 KB |
Output is correct |
10 |
Correct |
611 ms |
31376 KB |
Output is correct |
11 |
Correct |
603 ms |
31484 KB |
Output is correct |
12 |
Correct |
619 ms |
33016 KB |
Output is correct |
13 |
Correct |
607 ms |
32104 KB |
Output is correct |
14 |
Correct |
619 ms |
33016 KB |
Output is correct |
15 |
Correct |
631 ms |
30644 KB |
Output is correct |
16 |
Correct |
596 ms |
32304 KB |
Output is correct |
17 |
Correct |
608 ms |
35208 KB |
Output is correct |
18 |
Correct |
627 ms |
35120 KB |
Output is correct |
19 |
Correct |
615 ms |
32068 KB |
Output is correct |
20 |
Correct |
625 ms |
31768 KB |
Output is correct |
21 |
Correct |
688 ms |
32512 KB |
Output is correct |
22 |
Correct |
698 ms |
32816 KB |
Output is correct |
23 |
Correct |
682 ms |
35772 KB |
Output is correct |
24 |
Correct |
712 ms |
31316 KB |
Output is correct |
25 |
Correct |
683 ms |
32040 KB |
Output is correct |
26 |
Correct |
694 ms |
35076 KB |
Output is correct |
27 |
Correct |
707 ms |
32276 KB |
Output is correct |
28 |
Correct |
685 ms |
33036 KB |
Output is correct |
29 |
Correct |
697 ms |
32120 KB |
Output is correct |
30 |
Correct |
705 ms |
33008 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
1 ms |
316 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
320 KB |
Output is correct |
45 |
Correct |
1 ms |
320 KB |
Output is correct |
46 |
Correct |
1 ms |
316 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
4 ms |
468 KB |
Output is correct |
52 |
Correct |
4 ms |
468 KB |
Output is correct |
53 |
Correct |
4 ms |
540 KB |
Output is correct |
54 |
Correct |
4 ms |
484 KB |
Output is correct |
55 |
Correct |
6 ms |
484 KB |
Output is correct |
56 |
Correct |
4 ms |
516 KB |
Output is correct |
57 |
Correct |
4 ms |
588 KB |
Output is correct |
58 |
Correct |
5 ms |
584 KB |
Output is correct |
59 |
Correct |
4 ms |
468 KB |
Output is correct |
60 |
Correct |
4 ms |
468 KB |
Output is correct |
61 |
Correct |
695 ms |
34156 KB |
Output is correct |
62 |
Correct |
700 ms |
35396 KB |
Output is correct |
63 |
Correct |
685 ms |
34240 KB |
Output is correct |
64 |
Correct |
701 ms |
37056 KB |
Output is correct |
65 |
Correct |
699 ms |
34932 KB |
Output is correct |
66 |
Correct |
713 ms |
35164 KB |
Output is correct |
67 |
Correct |
761 ms |
35780 KB |
Output is correct |
68 |
Correct |
702 ms |
36240 KB |
Output is correct |
69 |
Correct |
676 ms |
33704 KB |
Output is correct |
70 |
Correct |
675 ms |
35740 KB |
Output is correct |