#include <algorithm>
#include <array>
#include <ios>
#include <iostream>
#include <utility>
#include <vector>
using State = std::pair<long long, int>;
auto solve() {
auto k = 0;
auto n = 0;
std::cin >> n >> k;
auto a = std::vector<int>(n);
for (auto& x : a) {
std::cin >> x;
}
auto lo = -1ll;
auto mx_pts = 0ll;
for (auto x : a) {
mx_pts += std::max(x, 0);
}
const auto compute = [&](long long penalty) {
auto dp = std::array<State, 2>();
dp[1].first = -(mx_pts + 1);
for (auto x : a) {
const auto maximize = [](State& lhs, const State& rhs) {
auto& [pts_l, cnt_l] = lhs;
const auto& [pts_r, cnt_r] = rhs;
if (pts_r > pts_l || (pts_r == pts_l && cnt_r < cnt_l)) {
lhs = rhs;
}
};
auto new_dp = std::array<State, 2>();
new_dp[0].first = -(mx_pts + 1);
new_dp[1].first = -(mx_pts + 1);
maximize(new_dp[0], dp[0]);
maximize(new_dp[0], State({dp[0].first + x - penalty, dp[0].second + 1}));
maximize(new_dp[1], State({dp[0].first + x, dp[0].second}));
maximize(new_dp[0], State({dp[1].first + x - penalty, dp[1].second + 1}));
maximize(new_dp[1], State({dp[1].first + x, dp[1].second}));
dp = std::move(new_dp);
}
return dp;
};
auto hi = mx_pts + 1;
while (hi - lo > 1) {
const auto mid = (lo + hi) >> 1;
(compute(mid)[0].second < k ? hi : lo) = mid;
}
const auto [pts, cnt] = std::move(compute(lo)[0]);
std::cout << pts + lo * cnt << '\n';
}
auto main() -> int {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
4180 KB |
Output is correct |
2 |
Correct |
52 ms |
4280 KB |
Output is correct |
3 |
Correct |
54 ms |
4436 KB |
Output is correct |
4 |
Correct |
58 ms |
4436 KB |
Output is correct |
5 |
Correct |
51 ms |
4428 KB |
Output is correct |
6 |
Correct |
52 ms |
4188 KB |
Output is correct |
7 |
Correct |
56 ms |
4300 KB |
Output is correct |
8 |
Correct |
52 ms |
4408 KB |
Output is correct |
9 |
Correct |
50 ms |
4176 KB |
Output is correct |
10 |
Correct |
51 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2648 KB |
Output is correct |
2 |
Correct |
33 ms |
2652 KB |
Output is correct |
3 |
Correct |
33 ms |
2652 KB |
Output is correct |
4 |
Correct |
42 ms |
2652 KB |
Output is correct |
5 |
Correct |
50 ms |
4284 KB |
Output is correct |
6 |
Correct |
32 ms |
2652 KB |
Output is correct |
7 |
Correct |
36 ms |
2648 KB |
Output is correct |
8 |
Correct |
49 ms |
4436 KB |
Output is correct |
9 |
Correct |
53 ms |
4252 KB |
Output is correct |
10 |
Correct |
33 ms |
2520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
4444 KB |
Output is correct |
2 |
Correct |
65 ms |
4436 KB |
Output is correct |
3 |
Correct |
65 ms |
4440 KB |
Output is correct |
4 |
Correct |
68 ms |
4432 KB |
Output is correct |
5 |
Correct |
66 ms |
4416 KB |
Output is correct |
6 |
Correct |
66 ms |
4436 KB |
Output is correct |
7 |
Correct |
66 ms |
4444 KB |
Output is correct |
8 |
Correct |
64 ms |
4420 KB |
Output is correct |
9 |
Correct |
71 ms |
4432 KB |
Output is correct |
10 |
Correct |
66 ms |
4552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
480 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
4180 KB |
Output is correct |
2 |
Correct |
52 ms |
4280 KB |
Output is correct |
3 |
Correct |
54 ms |
4436 KB |
Output is correct |
4 |
Correct |
58 ms |
4436 KB |
Output is correct |
5 |
Correct |
51 ms |
4428 KB |
Output is correct |
6 |
Correct |
52 ms |
4188 KB |
Output is correct |
7 |
Correct |
56 ms |
4300 KB |
Output is correct |
8 |
Correct |
52 ms |
4408 KB |
Output is correct |
9 |
Correct |
50 ms |
4176 KB |
Output is correct |
10 |
Correct |
51 ms |
4176 KB |
Output is correct |
11 |
Correct |
32 ms |
2648 KB |
Output is correct |
12 |
Correct |
33 ms |
2652 KB |
Output is correct |
13 |
Correct |
33 ms |
2652 KB |
Output is correct |
14 |
Correct |
42 ms |
2652 KB |
Output is correct |
15 |
Correct |
50 ms |
4284 KB |
Output is correct |
16 |
Correct |
32 ms |
2652 KB |
Output is correct |
17 |
Correct |
36 ms |
2648 KB |
Output is correct |
18 |
Correct |
49 ms |
4436 KB |
Output is correct |
19 |
Correct |
53 ms |
4252 KB |
Output is correct |
20 |
Correct |
33 ms |
2520 KB |
Output is correct |
21 |
Correct |
66 ms |
4444 KB |
Output is correct |
22 |
Correct |
65 ms |
4436 KB |
Output is correct |
23 |
Correct |
65 ms |
4440 KB |
Output is correct |
24 |
Correct |
68 ms |
4432 KB |
Output is correct |
25 |
Correct |
66 ms |
4416 KB |
Output is correct |
26 |
Correct |
66 ms |
4436 KB |
Output is correct |
27 |
Correct |
66 ms |
4444 KB |
Output is correct |
28 |
Correct |
64 ms |
4420 KB |
Output is correct |
29 |
Correct |
71 ms |
4432 KB |
Output is correct |
30 |
Correct |
66 ms |
4552 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
600 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
480 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
96 ms |
4444 KB |
Output is correct |
62 |
Correct |
101 ms |
4556 KB |
Output is correct |
63 |
Correct |
77 ms |
4432 KB |
Output is correct |
64 |
Correct |
104 ms |
4552 KB |
Output is correct |
65 |
Correct |
97 ms |
4428 KB |
Output is correct |
66 |
Correct |
100 ms |
4440 KB |
Output is correct |
67 |
Correct |
103 ms |
4444 KB |
Output is correct |
68 |
Correct |
69 ms |
4548 KB |
Output is correct |
69 |
Correct |
69 ms |
4440 KB |
Output is correct |
70 |
Correct |
66 ms |
4520 KB |
Output is correct |