#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = 0, r = 1e18, ans = - 1;
while(l <= r) {
int mid = (l + r) / 2;
pair<int,int> dp[n + 1][2];
dp[0][0] = {0, 0};
dp[0][1] = {-1e18, 0};
for(int i = 1; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
pair<int,int> best1;
pair<int,int> best2;
best1 = {dp[i - 1][0].first + a[i] - mid, dp[i - 1][0].second - 1};
best2 = {dp[i - 1][1].first + a[i], dp[i - 1][1].second};
dp[i][1] = max(best1, best2);
}
pair<int,int> e = max(dp[n][0], dp[n][1]);
if(-e.second <= k) {
r = mid - 1;
ans = mid * (-e.second) + e.first;
}
else {
l = mid + 1;
}
}
cout << ans;
}
# | 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... |