#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N, K;
cin >> N >> K;
vector<ll> arr(N + 1, 0);
for (int i = 1;i <= N;i ++) cin >> arr[i];
vector<vector<array<ll, 2>>> dp(N + 1, {{0, 0}, {0, 0}});
ll lo = 0, hi = 1e16 + 6789;
while (lo + 1 < hi) {
ll mid = (lo + hi) / 2LL;
for (int i = 1;i <= N;i ++) {
if ((dp[i - 1][0][0] == dp[i - 1][1][0] && dp[i - 1][0][1] > dp[i - 1][1][1]) || (dp[i - 1][0][0] > dp[i - 1][1][0])) {
dp[i][0] = {dp[i - 1][0][0], dp[i - 1][0][1]};
} else {
dp[i][0] = {dp[i - 1][1][0], dp[i - 1][1][1]};
}
if ((dp[i - 1][0][0] + arr[i] - mid == dp[i - 1][1][0] + arr[i] && dp[i - 1][0][1] + 1 > dp[i - 1][1][1]) || (dp[i - 1][0][0] + arr[i] - mid > dp[i - 1][1][0] + arr[i])) {
dp[i][1] = {dp[i - 1][0][0] + arr[i] - mid, dp[i - 1][0][1] + 1};
} else {
dp[i][1] = {dp[i - 1][1][0] + arr[i], dp[i - 1][1][1]};
}
}
if (max(dp[N][0][1], dp[N][1][1]) <= K) {
hi = mid;
} else {
lo = mid;
}
}
for (int i = 1;i <= N;i ++) {
if ((dp[i - 1][0][0] == dp[i - 1][1][0] && dp[i - 1][0][1] > dp[i - 1][1][1]) || (dp[i - 1][0][0] > dp[i - 1][1][0])) {
dp[i][0] = {dp[i - 1][0][0], dp[i - 1][0][1]};
} else {
dp[i][0] = {dp[i - 1][1][0], dp[i - 1][1][1]};
}
if ((dp[i - 1][0][0] + arr[i] - lo == dp[i - 1][1][0] + arr[i] && dp[i - 1][0][1] + 1 > dp[i - 1][1][1]) || (dp[i - 1][0][0] + arr[i] - lo > dp[i - 1][1][0] + arr[i])) {
dp[i][1] = {dp[i - 1][0][0] + arr[i] - lo, dp[i - 1][0][1] + 1};
} else {
dp[i][1] = {dp[i - 1][1][0] + arr[i], dp[i - 1][1][1]};
}
}
cout << max(dp[N][0][0], dp[N][1][0]) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
25848 KB |
Output is correct |
2 |
Correct |
102 ms |
26252 KB |
Output is correct |
3 |
Correct |
101 ms |
26616 KB |
Output is correct |
4 |
Correct |
90 ms |
26116 KB |
Output is correct |
5 |
Correct |
94 ms |
26440 KB |
Output is correct |
6 |
Correct |
103 ms |
25928 KB |
Output is correct |
7 |
Correct |
94 ms |
25752 KB |
Output is correct |
8 |
Correct |
121 ms |
26440 KB |
Output is correct |
9 |
Correct |
90 ms |
25928 KB |
Output is correct |
10 |
Correct |
85 ms |
26176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
85 ms |
24284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
26444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
25848 KB |
Output is correct |
2 |
Correct |
102 ms |
26252 KB |
Output is correct |
3 |
Correct |
101 ms |
26616 KB |
Output is correct |
4 |
Correct |
90 ms |
26116 KB |
Output is correct |
5 |
Correct |
94 ms |
26440 KB |
Output is correct |
6 |
Correct |
103 ms |
25928 KB |
Output is correct |
7 |
Correct |
94 ms |
25752 KB |
Output is correct |
8 |
Correct |
121 ms |
26440 KB |
Output is correct |
9 |
Correct |
90 ms |
25928 KB |
Output is correct |
10 |
Correct |
85 ms |
26176 KB |
Output is correct |
11 |
Incorrect |
85 ms |
24284 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |