// @tarche
// Feast
// OJUZ
// Singapore NOI 2019
// https://oj.uz/problem/view/NOI19_feast
// 2024-01-28
#include <bits/stdc++.h>
#define forn(i, n) for (tint i = 0; i < tint(n); i++)
#define forsn(i, s, n) for (tint i = s; i < tint(n); i++)
#define dforn(i, n) for (tint i = tint(n) - 1; i >= 0; i--)
#define dforsn(i, s, n) for (tint i = tint(n) - 1; i >= s; i--)
#define sz(x) tint(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define DBG(x) cerr << #x << " = " << x << endl
using namespace std;
#ifndef SMALL_TINT
using tint = long long;
#else
using tint = int;
#endif
using vi = vector<tint>;
using pii = pair<tint, tint>;
inline void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
inline string YN(bool x, string y = "YES", string n = "NO") {
return (x ? y : n);
}
template <typename T> inline void chmax(T &lhs, T rhs) { lhs = max(lhs, rhs); }
template <typename T> inline void chmin(T &lhs, T rhs) { lhs = min(lhs, rhs); }
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
forn(i, sz(v)) {
if (i > 0)
os << ", ";
os << v[i];
}
os << "]";
return os;
}
template <typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &v) {
os << "[";
forn(i, N) {
if (i > 0)
os << ", ";
os << v[i];
}
os << "]";
return os;
}
const tint INF = 1e13;
pii solve(const vi &nums, tint lambda) {
tint n = sz(nums);
vector<vector<pii>> dp(n + 1, vector<pii>(2, {-INF, 0}));
dp[0][0].first = 0;
forn(i, n) {
chmax(dp[i + 1][0], dp[i][0]);
chmax(dp[i + 1][0], dp[i][1]);
chmax(dp[i + 1][1],
{dp[i][0].first + nums[i] - lambda, dp[i][0].second - 1});
chmax(dp[i + 1][1], {dp[i][1].first + nums[i], dp[i][1].second});
}
pii ans = max(dp[n][0], dp[n][1]);
ans.second = -ans.second;
ans.first += lambda * ans.second;
return ans;
}
int main() {
fastIO();
tint n, k;
cin >> n >> k;
vi nums(n);
forn(i, n) cin >> nums[i];
tint lo = 0, hi = INF;
while (hi - lo > 1) {
tint lambda = (hi + lo) / 2;
if (solve(nums, lambda).second <= k) {
hi = lambda;
} else {
lo = lambda;
}
}
cout << solve(nums, hi).first << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
796 ms |
23376 KB |
Output is correct |
2 |
Correct |
785 ms |
23700 KB |
Output is correct |
3 |
Correct |
818 ms |
24268 KB |
Output is correct |
4 |
Correct |
799 ms |
23820 KB |
Output is correct |
5 |
Correct |
787 ms |
23732 KB |
Output is correct |
6 |
Correct |
821 ms |
23544 KB |
Output is correct |
7 |
Correct |
799 ms |
23508 KB |
Output is correct |
8 |
Correct |
798 ms |
24032 KB |
Output is correct |
9 |
Correct |
830 ms |
23332 KB |
Output is correct |
10 |
Correct |
820 ms |
23700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
815 ms |
23644 KB |
Output is correct |
2 |
Correct |
840 ms |
24020 KB |
Output is correct |
3 |
Correct |
768 ms |
23644 KB |
Output is correct |
4 |
Correct |
829 ms |
23608 KB |
Output is correct |
5 |
Correct |
767 ms |
23616 KB |
Output is correct |
6 |
Correct |
783 ms |
23440 KB |
Output is correct |
7 |
Correct |
818 ms |
23900 KB |
Output is correct |
8 |
Correct |
794 ms |
23824 KB |
Output is correct |
9 |
Correct |
787 ms |
23440 KB |
Output is correct |
10 |
Correct |
808 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
797 ms |
23684 KB |
Output is correct |
2 |
Correct |
799 ms |
26372 KB |
Output is correct |
3 |
Correct |
806 ms |
26616 KB |
Output is correct |
4 |
Correct |
863 ms |
26384 KB |
Output is correct |
5 |
Correct |
841 ms |
26628 KB |
Output is correct |
6 |
Correct |
834 ms |
26780 KB |
Output is correct |
7 |
Correct |
830 ms |
27088 KB |
Output is correct |
8 |
Correct |
815 ms |
26640 KB |
Output is correct |
9 |
Correct |
859 ms |
26952 KB |
Output is correct |
10 |
Correct |
835 ms |
26916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
372 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
372 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
3 ms |
604 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
388 KB |
Output is correct |
25 |
Correct |
3 ms |
600 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
3 ms |
604 KB |
Output is correct |
29 |
Correct |
4 ms |
604 KB |
Output is correct |
30 |
Correct |
3 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
796 ms |
23376 KB |
Output is correct |
2 |
Correct |
785 ms |
23700 KB |
Output is correct |
3 |
Correct |
818 ms |
24268 KB |
Output is correct |
4 |
Correct |
799 ms |
23820 KB |
Output is correct |
5 |
Correct |
787 ms |
23732 KB |
Output is correct |
6 |
Correct |
821 ms |
23544 KB |
Output is correct |
7 |
Correct |
799 ms |
23508 KB |
Output is correct |
8 |
Correct |
798 ms |
24032 KB |
Output is correct |
9 |
Correct |
830 ms |
23332 KB |
Output is correct |
10 |
Correct |
820 ms |
23700 KB |
Output is correct |
11 |
Correct |
815 ms |
23644 KB |
Output is correct |
12 |
Correct |
840 ms |
24020 KB |
Output is correct |
13 |
Correct |
768 ms |
23644 KB |
Output is correct |
14 |
Correct |
829 ms |
23608 KB |
Output is correct |
15 |
Correct |
767 ms |
23616 KB |
Output is correct |
16 |
Correct |
783 ms |
23440 KB |
Output is correct |
17 |
Correct |
818 ms |
23900 KB |
Output is correct |
18 |
Correct |
794 ms |
23824 KB |
Output is correct |
19 |
Correct |
787 ms |
23440 KB |
Output is correct |
20 |
Correct |
808 ms |
23936 KB |
Output is correct |
21 |
Correct |
797 ms |
23684 KB |
Output is correct |
22 |
Correct |
799 ms |
26372 KB |
Output is correct |
23 |
Correct |
806 ms |
26616 KB |
Output is correct |
24 |
Correct |
863 ms |
26384 KB |
Output is correct |
25 |
Correct |
841 ms |
26628 KB |
Output is correct |
26 |
Correct |
834 ms |
26780 KB |
Output is correct |
27 |
Correct |
830 ms |
27088 KB |
Output is correct |
28 |
Correct |
815 ms |
26640 KB |
Output is correct |
29 |
Correct |
859 ms |
26952 KB |
Output is correct |
30 |
Correct |
835 ms |
26916 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 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 |
344 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
372 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
344 KB |
Output is correct |
51 |
Correct |
3 ms |
604 KB |
Output is correct |
52 |
Correct |
3 ms |
604 KB |
Output is correct |
53 |
Correct |
3 ms |
604 KB |
Output is correct |
54 |
Correct |
3 ms |
388 KB |
Output is correct |
55 |
Correct |
3 ms |
600 KB |
Output is correct |
56 |
Correct |
3 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
3 ms |
604 KB |
Output is correct |
59 |
Correct |
4 ms |
604 KB |
Output is correct |
60 |
Correct |
3 ms |
612 KB |
Output is correct |
61 |
Correct |
833 ms |
26340 KB |
Output is correct |
62 |
Correct |
847 ms |
27028 KB |
Output is correct |
63 |
Correct |
785 ms |
26280 KB |
Output is correct |
64 |
Correct |
823 ms |
26804 KB |
Output is correct |
65 |
Correct |
803 ms |
26804 KB |
Output is correct |
66 |
Correct |
828 ms |
26752 KB |
Output is correct |
67 |
Correct |
824 ms |
26412 KB |
Output is correct |
68 |
Correct |
816 ms |
27084 KB |
Output is correct |
69 |
Correct |
780 ms |
26388 KB |
Output is correct |
70 |
Correct |
764 ms |
26360 KB |
Output is correct |