This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 3e5 + 7;
int n, k, a[N];
pair<int, int> dp[N][2];
pair<int, int> calc(int pen) {
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].fi + a[i], dp[i - 1][1].se),
make_pair(dp[i - 1][0].fi + a[i] - pen, dp[i - 1][0].se + 1));
}
return max(dp[n][0], dp[n][1]);
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
for(int i = 0; i < n; i++) cin >> a[i];
dp[0][0] = dp[0][1] = {0, 0};
auto solve_lambda = [&](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][0].first + a[i] - lmb, dp[i - 1][0].second + 1),
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second));
}
return max(dp[n - 1][0], dp[n - 1][1]);
};
int l = 0, r = 1e18;
int ans = 0;
while(l <= r) {
int m = (l + r) >> 1;
if(solve_lambda(m).se >= k) {
ans = m;
l = m + 1;
} else r = m - 1;
}
cout << solve_lambda(ans).fi + k *ans;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
# | 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... |