// @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 = 1e18;
pii solve(const vi &nums, tint lambda) {
tint n = sz(nums);
vector<array<pii, 2>> dp(n + 1, array<pii, 2>({make_pair(-INF, 0), make_pair(-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][1].first + nums[i], dp[i][1].second});
if (dp[i + 1][0].first > -INF) {
chmax(dp[i + 1][1], {dp[i][0].first + nums[i] - lambda, dp[i][0].second - 1});
}
}
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 = n * *max_element(all(nums));
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 |
209 ms |
11732 KB |
Output is correct |
2 |
Correct |
216 ms |
12008 KB |
Output is correct |
3 |
Correct |
224 ms |
12480 KB |
Output is correct |
4 |
Correct |
216 ms |
12028 KB |
Output is correct |
5 |
Correct |
216 ms |
12116 KB |
Output is correct |
6 |
Correct |
210 ms |
11728 KB |
Output is correct |
7 |
Correct |
223 ms |
11732 KB |
Output is correct |
8 |
Correct |
212 ms |
12060 KB |
Output is correct |
9 |
Correct |
220 ms |
12064 KB |
Output is correct |
10 |
Correct |
218 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
11804 KB |
Output is correct |
2 |
Correct |
129 ms |
12376 KB |
Output is correct |
3 |
Correct |
131 ms |
11868 KB |
Output is correct |
4 |
Correct |
130 ms |
11868 KB |
Output is correct |
5 |
Correct |
214 ms |
11720 KB |
Output is correct |
6 |
Correct |
136 ms |
11868 KB |
Output is correct |
7 |
Correct |
130 ms |
12132 KB |
Output is correct |
8 |
Correct |
215 ms |
12016 KB |
Output is correct |
9 |
Correct |
212 ms |
11748 KB |
Output is correct |
10 |
Correct |
134 ms |
12120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
12064 KB |
Output is correct |
2 |
Correct |
266 ms |
11776 KB |
Output is correct |
3 |
Correct |
236 ms |
11856 KB |
Output is correct |
4 |
Correct |
246 ms |
11776 KB |
Output is correct |
5 |
Correct |
234 ms |
11868 KB |
Output is correct |
6 |
Correct |
232 ms |
12036 KB |
Output is correct |
7 |
Correct |
236 ms |
12028 KB |
Output is correct |
8 |
Correct |
230 ms |
11992 KB |
Output is correct |
9 |
Correct |
233 ms |
12112 KB |
Output is correct |
10 |
Correct |
234 ms |
12112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
360 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
11732 KB |
Output is correct |
2 |
Correct |
216 ms |
12008 KB |
Output is correct |
3 |
Correct |
224 ms |
12480 KB |
Output is correct |
4 |
Correct |
216 ms |
12028 KB |
Output is correct |
5 |
Correct |
216 ms |
12116 KB |
Output is correct |
6 |
Correct |
210 ms |
11728 KB |
Output is correct |
7 |
Correct |
223 ms |
11732 KB |
Output is correct |
8 |
Correct |
212 ms |
12060 KB |
Output is correct |
9 |
Correct |
220 ms |
12064 KB |
Output is correct |
10 |
Correct |
218 ms |
11864 KB |
Output is correct |
11 |
Correct |
129 ms |
11804 KB |
Output is correct |
12 |
Correct |
129 ms |
12376 KB |
Output is correct |
13 |
Correct |
131 ms |
11868 KB |
Output is correct |
14 |
Correct |
130 ms |
11868 KB |
Output is correct |
15 |
Correct |
214 ms |
11720 KB |
Output is correct |
16 |
Correct |
136 ms |
11868 KB |
Output is correct |
17 |
Correct |
130 ms |
12132 KB |
Output is correct |
18 |
Correct |
215 ms |
12016 KB |
Output is correct |
19 |
Correct |
212 ms |
11748 KB |
Output is correct |
20 |
Correct |
134 ms |
12120 KB |
Output is correct |
21 |
Correct |
232 ms |
12064 KB |
Output is correct |
22 |
Correct |
266 ms |
11776 KB |
Output is correct |
23 |
Correct |
236 ms |
11856 KB |
Output is correct |
24 |
Correct |
246 ms |
11776 KB |
Output is correct |
25 |
Correct |
234 ms |
11868 KB |
Output is correct |
26 |
Correct |
232 ms |
12036 KB |
Output is correct |
27 |
Correct |
236 ms |
12028 KB |
Output is correct |
28 |
Correct |
230 ms |
11992 KB |
Output is correct |
29 |
Correct |
233 ms |
12112 KB |
Output is correct |
30 |
Correct |
234 ms |
12112 KB |
Output is correct |
31 |
Correct |
0 ms |
344 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 |
360 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
2 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
348 KB |
Output is correct |
55 |
Correct |
2 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
348 KB |
Output is correct |
58 |
Correct |
2 ms |
348 KB |
Output is correct |
59 |
Correct |
2 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
265 ms |
11856 KB |
Output is correct |
62 |
Correct |
270 ms |
12116 KB |
Output is correct |
63 |
Correct |
241 ms |
11856 KB |
Output is correct |
64 |
Correct |
270 ms |
12028 KB |
Output is correct |
65 |
Correct |
272 ms |
12040 KB |
Output is correct |
66 |
Correct |
270 ms |
11868 KB |
Output is correct |
67 |
Correct |
269 ms |
11796 KB |
Output is correct |
68 |
Correct |
245 ms |
12004 KB |
Output is correct |
69 |
Correct |
233 ms |
11868 KB |
Output is correct |
70 |
Correct |
229 ms |
11744 KB |
Output is correct |