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;
const int N = (int)3e5, K = (int)3e5;
int n, k;
int a[N];
int64_t dp[N][2];
int cnt[N][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 0;i < n;i ++)
cin >> a[i];
auto check = [&](int64_t penalty) -> pair<int, int64_t> {
for (int i = 0;i < n;i ++) {
for (int pr = 0;pr < 2;pr ++) {
dp[i][pr] = LLONG_MIN;
cnt[i][pr] = INT_MAX;
}
}
if (0 >= a[0] - penalty) {
dp[0][0] = 0;
cnt[0][0] = 0;
} else {
dp[0][0] = a[0] - penalty;
cnt[0][0] = 1;
}
dp[0][1] = a[0] - penalty;
cnt[0][1] = 1;
for (int i = 1;i < n;i ++) {
if (dp[i - 1][0] + a[i] - penalty > dp[i][0] || (dp[i - 1][0] + a[i] - penalty == dp[i][0] && cnt[i - 1][0] + 1 < cnt[i][0])) {
dp[i][0] = dp[i - 1][0] + a[i] - penalty;
cnt[i][0] = cnt[i - 1][0] + 1;
}
if (dp[i - 1][0] > dp[i][0] || (dp[i - 1][0] == dp[i][0] && cnt[i - 1][0] < cnt[i][0])) {
dp[i][0] = dp[i - 1][0];
cnt[i][0] = cnt[i - 1][0];
}
if (dp[i - 1][1] + a[i] > dp[i][0] || (dp[i - 1][1] + a[i] == dp[i][0] && cnt[i - 1][1] < cnt[i][0])) {
dp[i][0] = dp[i - 1][1] + a[i];
cnt[i][0] = cnt[i - 1][1];
}
if (dp[i - 1][1] + a[i] > dp[i][1] || (dp[i - 1][1] + a[i] == dp[i][1] && cnt[i - 1][1] < cnt[i][1])) {
dp[i][1] = dp[i - 1][1] + a[i];
cnt[i][1] = cnt[i - 1][1];
}
if (dp[i - 1][0] + a[i] - penalty > dp[i][1] || (dp[i - 1][0] + a[i] - penalty == dp[i][1] && cnt[i - 1][0] + 1 < cnt[i][1])) {
dp[i][1] = dp[i - 1][0] + a[i] - penalty;
cnt[i][1] = cnt[i - 1][0] + 1;
}
}
return {cnt[n - 1][0], dp[n - 1][0]};
};
int64_t lx = 0, rx = (int64_t)1e15;
while (lx < rx) {
int64_t g = lx + (rx - lx) / 2;
if (check(g).first <= k)
rx = g;
else
lx = g + 1;
}
pair<int, int64_t> res = check(lx);
int64_t ans = res.second + (k - res.first) * lx + lx * k;
cout << ans << '\n';
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... |