#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<typename Tuple, size_t... Is>
void print_tuple(const Tuple& t, index_sequence<Is...>) {
((cerr << (Is == 0 ? "{" : ", ") << get<Is>(t)), ...);
cerr << "}";
}
template<typename... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
print_tuple(t, index_sequence_for<Args...>{});
return os;
}
ostream& operator<<(ostream& os, string& s) {
for (char c : s) os << c;
return os;
}
template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
os << "{";
int g = size(o);
for (auto i : o) os << i << ((--g) == 0 ? "" : ", ");
os << "}";
return os;
}
template<typename T, typename V>
ostream& operator<<(ostream& os, const pair<T, V> p) {
os << "{" << p.f << ", " << p.s << "}";
return os;
}
template <typename T, typename... V>
void printer(const char *names, T &&head, V &&...tail) {
int i = 0;
while (names[i] != '\0' && names[i] != ',') i++;
constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
if constexpr (is_str) cerr << head; // ignore directly passed strings
else cerr.write(names, i) << " = " << head;
if constexpr (sizeof...(tail)) cerr << (is_str ? "" : ","), printer(names + i + 1, tail...);
else cerr << endl;
}
#ifdef LOCAL
#define dbg(...) std::cerr << __LINE__ << ": ", printer(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
/*
aliens trick
dp[i] = max value of eating first i dishes
dp[i] = min(dp[i - 1], max (j < i) dp[j] + sfx[j + 1] - sfx[i + 1])
*/
ll n, k;
vt<ll> a, sfx;
// tiebreak by minimum # of arrays used
pl check(ll penalty) {
vt<pl> dp(n + 1);
ll best = sfx[0], used = 0;
F0R (i, n) {
dp[i + 1] = dp[i];
if (best - sfx[i + 1] - penalty > dp[i + 1].f) {
dp[i + 1] = {best - sfx[i + 1] - penalty, used + 1};
} else if (best - sfx[i + 1] - penalty == dp[i + 1].f && used + 1 < dp[i + 1].s) {
dp[i + 1] = {best - sfx[i + 1] - penalty, used + 1};
}
if (dp[i + 1].f + sfx[i + 1] > best) tie(best, used) = dp[i + 1], best += sfx[i + 1];
else if (dp[i + 1].f + sfx[i + 1] == best && used > dp[i + 1].s) tie(best, used) = dp[i + 1], best += sfx[i + 1];
}
return dp[n];
}
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
a.resize(n);
sfx.resize(n + 1);
F0R (i, n) cin >> a[i];
copy(all(a), sfx.begin());
ROF (i, 0, n) sfx[i] += sfx[i + 1];
// more penalty = fewer arrays
// want to find smallest penalty such that <= k arrays
ll lo = -1, hi = 1e12;
while (hi - lo > 1) {
ll m = (lo + hi) / 2;
auto [cost, used] = check(m);
if (used > k) lo = m;
else hi = m;
}
auto [cost, used] = check(hi);
ll ans = cost + hi * k;
cout << ans << endl;
}
Compilation message
feast.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
9360 KB |
Output is correct |
2 |
Correct |
39 ms |
9556 KB |
Output is correct |
3 |
Correct |
47 ms |
9820 KB |
Output is correct |
4 |
Correct |
39 ms |
9776 KB |
Output is correct |
5 |
Correct |
46 ms |
9700 KB |
Output is correct |
6 |
Correct |
39 ms |
9560 KB |
Output is correct |
7 |
Correct |
38 ms |
9436 KB |
Output is correct |
8 |
Correct |
39 ms |
9748 KB |
Output is correct |
9 |
Correct |
38 ms |
9432 KB |
Output is correct |
10 |
Correct |
49 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9596 KB |
Output is correct |
2 |
Correct |
35 ms |
9820 KB |
Output is correct |
3 |
Correct |
33 ms |
9856 KB |
Output is correct |
4 |
Correct |
35 ms |
9564 KB |
Output is correct |
5 |
Correct |
43 ms |
9560 KB |
Output is correct |
6 |
Correct |
35 ms |
9608 KB |
Output is correct |
7 |
Correct |
36 ms |
9820 KB |
Output is correct |
8 |
Correct |
46 ms |
9740 KB |
Output is correct |
9 |
Correct |
44 ms |
9372 KB |
Output is correct |
10 |
Correct |
34 ms |
9736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
9560 KB |
Output is correct |
2 |
Correct |
55 ms |
9596 KB |
Output is correct |
3 |
Correct |
56 ms |
9708 KB |
Output is correct |
4 |
Correct |
57 ms |
12672 KB |
Output is correct |
5 |
Correct |
59 ms |
12624 KB |
Output is correct |
6 |
Correct |
58 ms |
12788 KB |
Output is correct |
7 |
Correct |
57 ms |
12812 KB |
Output is correct |
8 |
Correct |
56 ms |
12720 KB |
Output is correct |
9 |
Correct |
69 ms |
12812 KB |
Output is correct |
10 |
Correct |
64 ms |
12824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 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 |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 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 |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
452 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 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 |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 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 |
452 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
512 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 |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
9360 KB |
Output is correct |
2 |
Correct |
39 ms |
9556 KB |
Output is correct |
3 |
Correct |
47 ms |
9820 KB |
Output is correct |
4 |
Correct |
39 ms |
9776 KB |
Output is correct |
5 |
Correct |
46 ms |
9700 KB |
Output is correct |
6 |
Correct |
39 ms |
9560 KB |
Output is correct |
7 |
Correct |
38 ms |
9436 KB |
Output is correct |
8 |
Correct |
39 ms |
9748 KB |
Output is correct |
9 |
Correct |
38 ms |
9432 KB |
Output is correct |
10 |
Correct |
49 ms |
9560 KB |
Output is correct |
11 |
Correct |
35 ms |
9596 KB |
Output is correct |
12 |
Correct |
35 ms |
9820 KB |
Output is correct |
13 |
Correct |
33 ms |
9856 KB |
Output is correct |
14 |
Correct |
35 ms |
9564 KB |
Output is correct |
15 |
Correct |
43 ms |
9560 KB |
Output is correct |
16 |
Correct |
35 ms |
9608 KB |
Output is correct |
17 |
Correct |
36 ms |
9820 KB |
Output is correct |
18 |
Correct |
46 ms |
9740 KB |
Output is correct |
19 |
Correct |
44 ms |
9372 KB |
Output is correct |
20 |
Correct |
34 ms |
9736 KB |
Output is correct |
21 |
Correct |
56 ms |
9560 KB |
Output is correct |
22 |
Correct |
55 ms |
9596 KB |
Output is correct |
23 |
Correct |
56 ms |
9708 KB |
Output is correct |
24 |
Correct |
57 ms |
12672 KB |
Output is correct |
25 |
Correct |
59 ms |
12624 KB |
Output is correct |
26 |
Correct |
58 ms |
12788 KB |
Output is correct |
27 |
Correct |
57 ms |
12812 KB |
Output is correct |
28 |
Correct |
56 ms |
12720 KB |
Output is correct |
29 |
Correct |
69 ms |
12812 KB |
Output is correct |
30 |
Correct |
64 ms |
12824 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
448 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 |
344 KB |
Output is correct |
39 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 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 |
452 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
344 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
512 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 |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
86 ms |
12628 KB |
Output is correct |
62 |
Correct |
83 ms |
12884 KB |
Output is correct |
63 |
Correct |
70 ms |
12420 KB |
Output is correct |
64 |
Correct |
88 ms |
12796 KB |
Output is correct |
65 |
Correct |
92 ms |
12840 KB |
Output is correct |
66 |
Correct |
87 ms |
12744 KB |
Output is correct |
67 |
Correct |
86 ms |
12384 KB |
Output is correct |
68 |
Correct |
67 ms |
12656 KB |
Output is correct |
69 |
Correct |
69 ms |
12428 KB |
Output is correct |
70 |
Correct |
55 ms |
12448 KB |
Output is correct |