#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= (_b); ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= (_b); --i)
#define getBit(mask,i) (((mask) >> (i)) & (1LL))
#define allof(x) begin(x), end(x)
#define el cout << '\n';
//--Compare------------------------------------------------------------------------------------
template<class X, class Y>
inline bool maximize(X &x, const Y &y){ return (x < y) ? x = y, 1 : 0; }
template<class X, class Y>
inline bool minimize(X &x, const Y &y){ return (x > y) ? x = y, 1 : 0; }
//--Process------------------------------------------------------------------------------------
#define int long long
#define fi first
#define se second
typedef pair <int, int> ii;
constexpr int MAXN = 3e5 + 10;
int n, k;
int a[MAXN];
ii dp[MAXN][2];
signed main(void)
{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
function <ii(int)> calc = [&](int lmb)
{
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - lmb, 1};
FOR(i, 2, n)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
ii val1 = make_pair(dp[i - 1][0].fi + a[i] - lmb, dp[i - 1][0].se + 1);
ii val2 = make_pair(dp[i - 1][1].fi + a[i], dp[i - 1][1].se);
dp[i][1] = max(val1, val2);
}
return max(dp[n][0], dp[n][1]);
};
int low = 0, high = 1e18;
while (low < high)
{
int mid = (low + high + 1) / 2;
calc(mid).se >= k ? low = mid : high = mid - 1;
}
cout << calc(low).fi + low * k << '\n';
cerr << (1.0 * clock() / CLOCKS_PER_SEC);
return 0;
}
# | 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... |