#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16;
const int N = 300300;
int n, k, a[N];
pair<long long, int> dp[N][2];
pair<long long, int> cost (long long lmb){
dp[0][0] = {0ll, 0};
dp[0][1] = {-inf, 0};
for (int i = 1; i <= n; ++i){
auto [x, y] = dp[i - 1][0];
auto [u, v] = dp[i - 1][1];
dp[i][0] = max(make_pair(x, y), make_pair(u, v));
dp[i][1] = max(make_pair(x + 1ll * a[i] - lmb, y + 1), make_pair(u + 1ll * a[i], v));
}
return max(dp[n][0], dp[n][1]);
}
int32_t main (){
ios::sync_with_stdio(false); cin.tie(nullptr);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
long long l = 0, r = 1e16;
while (l < r){
long long m = l + r + 1 >> 1;
auto [x, y] = cost(m);
if (y >= k){
l = m;
} else {
r = m - 1;
}
}
cout << cost(l).first + 1ll * k * l;
}
# | 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... |