// Source: https://oj.uz/problem/view/NOI19_feast
//
#include "bits/stdc++.h"
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
typedef long long ll;
typedef pair<ll, ll> pii;
#define s second
#define f first
const ll N = 3e5 + 10;
ll a[N];
pii dp[N][2];
ll n, k;
pii solve(ll l) {
dp[0][0] = {0, 0};
dp[0][1] = {a[0] - l, 1};
FOR(i, 1, n) {
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(make_pair(dp[i-1][0].f + a[i] - l, dp[i-1][0].s + 1), make_pair(dp[i-1][1].f + a[i], dp[i-1][1].s));
}
return max(dp[n-1][1], dp[n-1][0]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
FOR(i, 0 ,n) cin >> a[i];
ll lo = 0;
ll hi = 1e18;
while (lo < hi) {
ll m = (lo + hi + 1)/2;
pii res = solve(m);
if (res.s >= k) lo = m;
else hi = m -1;
}
// cout << lo << solve(lo).f << endl;
cout << solve(lo).f + k * lo << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
13764 KB |
Output is correct |
2 |
Correct |
74 ms |
14992 KB |
Output is correct |
3 |
Correct |
81 ms |
14928 KB |
Output is correct |
4 |
Correct |
75 ms |
14932 KB |
Output is correct |
5 |
Correct |
75 ms |
14928 KB |
Output is correct |
6 |
Correct |
73 ms |
14908 KB |
Output is correct |
7 |
Correct |
82 ms |
14672 KB |
Output is correct |
8 |
Correct |
73 ms |
14796 KB |
Output is correct |
9 |
Correct |
73 ms |
14936 KB |
Output is correct |
10 |
Correct |
79 ms |
14968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
12376 KB |
Output is correct |
2 |
Correct |
69 ms |
13308 KB |
Output is correct |
3 |
Correct |
67 ms |
13120 KB |
Output is correct |
4 |
Correct |
67 ms |
13144 KB |
Output is correct |
5 |
Correct |
77 ms |
14676 KB |
Output is correct |
6 |
Correct |
67 ms |
13148 KB |
Output is correct |
7 |
Correct |
67 ms |
13316 KB |
Output is correct |
8 |
Correct |
77 ms |
14928 KB |
Output is correct |
9 |
Correct |
75 ms |
14684 KB |
Output is correct |
10 |
Correct |
67 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
13908 KB |
Output is correct |
2 |
Correct |
99 ms |
15104 KB |
Output is correct |
3 |
Correct |
98 ms |
15152 KB |
Output is correct |
4 |
Correct |
96 ms |
14932 KB |
Output is correct |
5 |
Correct |
96 ms |
15152 KB |
Output is correct |
6 |
Correct |
110 ms |
15084 KB |
Output is correct |
7 |
Correct |
99 ms |
15216 KB |
Output is correct |
8 |
Correct |
97 ms |
15152 KB |
Output is correct |
9 |
Correct |
98 ms |
15220 KB |
Output is correct |
10 |
Correct |
110 ms |
15208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2408 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
13764 KB |
Output is correct |
2 |
Correct |
74 ms |
14992 KB |
Output is correct |
3 |
Correct |
81 ms |
14928 KB |
Output is correct |
4 |
Correct |
75 ms |
14932 KB |
Output is correct |
5 |
Correct |
75 ms |
14928 KB |
Output is correct |
6 |
Correct |
73 ms |
14908 KB |
Output is correct |
7 |
Correct |
82 ms |
14672 KB |
Output is correct |
8 |
Correct |
73 ms |
14796 KB |
Output is correct |
9 |
Correct |
73 ms |
14936 KB |
Output is correct |
10 |
Correct |
79 ms |
14968 KB |
Output is correct |
11 |
Correct |
67 ms |
12376 KB |
Output is correct |
12 |
Correct |
69 ms |
13308 KB |
Output is correct |
13 |
Correct |
67 ms |
13120 KB |
Output is correct |
14 |
Correct |
67 ms |
13144 KB |
Output is correct |
15 |
Correct |
77 ms |
14676 KB |
Output is correct |
16 |
Correct |
67 ms |
13148 KB |
Output is correct |
17 |
Correct |
67 ms |
13316 KB |
Output is correct |
18 |
Correct |
77 ms |
14928 KB |
Output is correct |
19 |
Correct |
75 ms |
14684 KB |
Output is correct |
20 |
Correct |
67 ms |
13148 KB |
Output is correct |
21 |
Correct |
99 ms |
13908 KB |
Output is correct |
22 |
Correct |
99 ms |
15104 KB |
Output is correct |
23 |
Correct |
98 ms |
15152 KB |
Output is correct |
24 |
Correct |
96 ms |
14932 KB |
Output is correct |
25 |
Correct |
96 ms |
15152 KB |
Output is correct |
26 |
Correct |
110 ms |
15084 KB |
Output is correct |
27 |
Correct |
99 ms |
15216 KB |
Output is correct |
28 |
Correct |
97 ms |
15152 KB |
Output is correct |
29 |
Correct |
98 ms |
15220 KB |
Output is correct |
30 |
Correct |
110 ms |
15208 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2648 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
2 ms |
2392 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2408 KB |
Output is correct |
56 |
Correct |
1 ms |
2392 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
130 ms |
15084 KB |
Output is correct |
62 |
Correct |
128 ms |
15232 KB |
Output is correct |
63 |
Correct |
110 ms |
14988 KB |
Output is correct |
64 |
Correct |
131 ms |
15196 KB |
Output is correct |
65 |
Correct |
141 ms |
15184 KB |
Output is correct |
66 |
Correct |
133 ms |
15184 KB |
Output is correct |
67 |
Correct |
130 ms |
14932 KB |
Output is correct |
68 |
Correct |
99 ms |
15128 KB |
Output is correct |
69 |
Correct |
96 ms |
15068 KB |
Output is correct |
70 |
Correct |
96 ms |
15068 KB |
Output is correct |