#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
int n, k;
vector<ll> a;
pair<ll, int> check(ll cost) {
vector<array<ll, 2>> dp(n + 1);
vector<int> cnt0(n + 1), cnt1(n + 1);
dp[1][1] = a[0] - cost;
cnt1[1] = 1;
for (int i = 1; i <= n; i++) {
if (dp[i - 1][0] > dp[i - 1][1]) {
dp[i][0] = dp[i - 1][0];
cnt0[i] = cnt0[i - 1];
} else {
dp[i][0] = dp[i - 1][1];
cnt0[i] = cnt1[i - 1];
}
if (dp[i - 1][0] + cost > dp[i - 1][1]) {
dp[i][1] = dp[i - 1][0] + a[i - 1] + cost;
cnt1[i] = cnt0[i - 1] + 1;
} else {
dp[i][1] = dp[i - 1][1] + a[i - 1];
cnt1[i] = cnt1[i - 1] + 1;
}
}
if (dp[n][0] == dp[n][1])
return {dp[n][0], max(cnt0[n], cnt1[n])};
if (dp[n][0] > dp[n][1])
return {dp[n][0], cnt0[n]};
else
return {dp[n][1], cnt1[n]};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
ll left = 0, right = 5 + 3e5;
while (left <= right) {
ll mid = (left + right) / 2;
if (check(mid).second >= k)
right = mid - 1;
else
left = mid + 1;
}
cout << check(right).first << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |