#include <bits/stdc++.h>
using namespace std;
#define N 300000
#define INF 0x3f3f3f3f3f3f3f3f
int n, k, a[N + 1];
pair<long double, int> dp[N + 1][2];
template<class T, class U> T lastTrue(T lo, T hi, U f) {
assert(lo <= hi); --lo; /* assuming f is decreasing */
while (lo < hi) { /* find last index such that f is true */
T mid = lo + (hi - lo + 1) / 2;
f(mid) ? lo = mid : hi = mid - 1;
} return lo;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
auto check = [&](long double penalty) {
dp[0][0] = {0, 0}, dp[0][1] = {-INF, 0};
for (int i = 1; i <= n; ++i) {
pair<long double, int> last = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][0] = max(make_pair(last.first - penalty, last.second + 1), last);
dp[i][1] = max(make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second),
make_pair(dp[i - 1][0].first + a[i] - penalty, dp[i - 1][0].second + 1));
}
return max(dp[n][0], dp[n][1]).second >= k;
};
long double lower = 0, upper = INF;
for (int it = 0; it < 100; ++it) {
long double mid = (lower + upper) / 2;
if (check(mid)) {
lower = mid;
} else {
upper = mid;
}
}
long double penalty = lower;
check(penalty);
printf("%lld\n", (long long) round(penalty * k + max(dp[n][0], dp[n][1]).first));
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
feast.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d", a + i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
725 ms |
19620 KB |
Output is correct |
2 |
Correct |
752 ms |
22848 KB |
Output is correct |
3 |
Correct |
742 ms |
23100 KB |
Output is correct |
4 |
Correct |
744 ms |
22968 KB |
Output is correct |
5 |
Correct |
760 ms |
22852 KB |
Output is correct |
6 |
Correct |
728 ms |
22436 KB |
Output is correct |
7 |
Correct |
723 ms |
22432 KB |
Output is correct |
8 |
Correct |
782 ms |
22980 KB |
Output is correct |
9 |
Correct |
724 ms |
22468 KB |
Output is correct |
10 |
Correct |
764 ms |
22720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
655 ms |
19756 KB |
Output is correct |
2 |
Correct |
658 ms |
20292 KB |
Output is correct |
3 |
Correct |
644 ms |
19728 KB |
Output is correct |
4 |
Correct |
713 ms |
19976 KB |
Output is correct |
5 |
Correct |
635 ms |
22452 KB |
Output is correct |
6 |
Correct |
629 ms |
20824 KB |
Output is correct |
7 |
Correct |
771 ms |
21316 KB |
Output is correct |
8 |
Correct |
665 ms |
22976 KB |
Output is correct |
9 |
Correct |
672 ms |
22392 KB |
Output is correct |
10 |
Correct |
740 ms |
21192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
721 ms |
19900 KB |
Output is correct |
2 |
Correct |
681 ms |
19760 KB |
Output is correct |
3 |
Correct |
700 ms |
19980 KB |
Output is correct |
4 |
Correct |
693 ms |
19760 KB |
Output is correct |
5 |
Correct |
709 ms |
19956 KB |
Output is correct |
6 |
Correct |
707 ms |
20188 KB |
Output is correct |
7 |
Correct |
711 ms |
20140 KB |
Output is correct |
8 |
Correct |
693 ms |
19960 KB |
Output is correct |
9 |
Correct |
695 ms |
20200 KB |
Output is correct |
10 |
Correct |
693 ms |
20084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
5 ms |
332 KB |
Output is correct |
22 |
Correct |
5 ms |
440 KB |
Output is correct |
23 |
Correct |
6 ms |
460 KB |
Output is correct |
24 |
Correct |
7 ms |
452 KB |
Output is correct |
25 |
Correct |
6 ms |
460 KB |
Output is correct |
26 |
Correct |
6 ms |
464 KB |
Output is correct |
27 |
Correct |
7 ms |
448 KB |
Output is correct |
28 |
Correct |
7 ms |
440 KB |
Output is correct |
29 |
Correct |
5 ms |
460 KB |
Output is correct |
30 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
725 ms |
19620 KB |
Output is correct |
2 |
Correct |
752 ms |
22848 KB |
Output is correct |
3 |
Correct |
742 ms |
23100 KB |
Output is correct |
4 |
Correct |
744 ms |
22968 KB |
Output is correct |
5 |
Correct |
760 ms |
22852 KB |
Output is correct |
6 |
Correct |
728 ms |
22436 KB |
Output is correct |
7 |
Correct |
723 ms |
22432 KB |
Output is correct |
8 |
Correct |
782 ms |
22980 KB |
Output is correct |
9 |
Correct |
724 ms |
22468 KB |
Output is correct |
10 |
Correct |
764 ms |
22720 KB |
Output is correct |
11 |
Correct |
655 ms |
19756 KB |
Output is correct |
12 |
Correct |
658 ms |
20292 KB |
Output is correct |
13 |
Correct |
644 ms |
19728 KB |
Output is correct |
14 |
Correct |
713 ms |
19976 KB |
Output is correct |
15 |
Correct |
635 ms |
22452 KB |
Output is correct |
16 |
Correct |
629 ms |
20824 KB |
Output is correct |
17 |
Correct |
771 ms |
21316 KB |
Output is correct |
18 |
Correct |
665 ms |
22976 KB |
Output is correct |
19 |
Correct |
672 ms |
22392 KB |
Output is correct |
20 |
Correct |
740 ms |
21192 KB |
Output is correct |
21 |
Correct |
721 ms |
19900 KB |
Output is correct |
22 |
Correct |
681 ms |
19760 KB |
Output is correct |
23 |
Correct |
700 ms |
19980 KB |
Output is correct |
24 |
Correct |
693 ms |
19760 KB |
Output is correct |
25 |
Correct |
709 ms |
19956 KB |
Output is correct |
26 |
Correct |
707 ms |
20188 KB |
Output is correct |
27 |
Correct |
711 ms |
20140 KB |
Output is correct |
28 |
Correct |
693 ms |
19960 KB |
Output is correct |
29 |
Correct |
695 ms |
20200 KB |
Output is correct |
30 |
Correct |
693 ms |
20084 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
5 ms |
332 KB |
Output is correct |
52 |
Correct |
5 ms |
440 KB |
Output is correct |
53 |
Correct |
6 ms |
460 KB |
Output is correct |
54 |
Correct |
7 ms |
452 KB |
Output is correct |
55 |
Correct |
6 ms |
460 KB |
Output is correct |
56 |
Correct |
6 ms |
464 KB |
Output is correct |
57 |
Correct |
7 ms |
448 KB |
Output is correct |
58 |
Correct |
7 ms |
440 KB |
Output is correct |
59 |
Correct |
5 ms |
460 KB |
Output is correct |
60 |
Correct |
5 ms |
460 KB |
Output is correct |
61 |
Correct |
787 ms |
22760 KB |
Output is correct |
62 |
Correct |
786 ms |
23296 KB |
Output is correct |
63 |
Correct |
726 ms |
22724 KB |
Output is correct |
64 |
Correct |
795 ms |
23108 KB |
Output is correct |
65 |
Correct |
820 ms |
23108 KB |
Output is correct |
66 |
Correct |
815 ms |
23044 KB |
Output is correct |
67 |
Correct |
782 ms |
22696 KB |
Output is correct |
68 |
Correct |
709 ms |
23132 KB |
Output is correct |
69 |
Correct |
707 ms |
22724 KB |
Output is correct |
70 |
Correct |
672 ms |
22576 KB |
Output is correct |