#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
/*
instead of enforcing K, we find the lambda that produces K, then just do cost + lambda * K
*/
const int N = 3e5+5;
int a[N];
int32_t main() {
int n,k;
cin >> n >> k;
for(int i = 0;i<n;i++)cin>>a[i];
auto fc = [&](int lmb) {
pair<int,int> dp[n][2];
dp[0][0] = {0,0};
dp[0][1] = {a[0] - lmb,1};
for(int i = 1;i<n;i++) {
dp[i][0] = max(dp[i-1][0],dp[i-1][1]);
dp[i][1] = max(
make_pair(dp[i-1][1].f + a[i],dp[i-1][1].s),
make_pair(dp[i-1][0].f + a[i] - lmb,dp[i-1][0].s + 1)
);
}
return max(dp[n-1][0],dp[n-1][1]);
};
int lo = 0, hi = 1e17;
while(lo<hi){
int mid = (lo + 1 + hi)/2;
fc(mid).s >= k ? lo = mid : hi = mid-1;
}
cout << fc(lo).f + lo * k;
}
/*
cost UP, k down
if k too high, then cost up (lo = mid)
if k too low, then cost down (hi = mid-1)
*/
# | 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... |