#include "bits/stdc++.h"
using namespace std;
template<typename T>
using V = vector<T>;
using ll = long long;
using pi = pair<ll, ll>;
const ll INF = 1e18;
pi solve(const V<ll>& arr, ll k, ll p) {
ll n = arr.size();
V<V<pi>> dp(n + 1, V<pi>(2, {-INF, 0}));
dp[0][0] = {0, 0};
for (ll i = 1; i <= n; ++i) {
dp[i][0] = {0, 0};
pi prev0 = dp[i - 1][0];
pi prev1 = dp[i - 1][1];
// For l = 0
dp[i][0] = max(dp[i][0], prev0); // Carry forward dp[i-1][0]
dp[i][0] = max(dp[i][0], {prev1.first - p, prev1.second + 1}); // Transfer from dp[i-1][1] with penalty
// For l = 1
dp[i][1] = max(dp[i][1], prev0); // Transfer from dp[i-1][0]
dp[i][1] = max(dp[i][1], {prev1.first + arr[i - 1], prev1.second}); // Keep dp[i-1][1] and add arr[i-1]
}
return max(dp[n][0], dp[n][1]);
}
int main() {
ll n, k;
cin >> n >> k;
V<ll> arr(n);
for (ll i = 0; i < n; ++i) {
cin >> arr[i];
}
ll l = 0, r = 1e18;
int iter = 0;
while (r > l) {
ll mid = l + (r - l + 1) / 2;
if (solve(arr, k, mid).second >= k)
l = mid;
else
r = mid - 1;
if (++iter > 62) exit(5); // Ensure no infinite loop
}
cout << solve(arr, k, l).first + l * k << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
903 ms |
25952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
871 ms |
24788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
954 ms |
26300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
903 ms |
25952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |