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;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 300005;
int n, k;
int a[N];
pair<long long,int> dp[N][2];
pair<long long,int> solve (long long cost) {
dp[0][0] = {0LL, 0};
dp[0][1] = {-1e15, -N};
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] - cost, dp[i - 1][0].second + 1),
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second));
}
return max(dp[n][0], dp[n][1]);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
long long l = 0, r = (long long) N * 1e9;
while (l < r) {
long long mid = (l + r) >> 1;
if (solve(mid).second >= k) {
l = mid + 1;
} else {
r = mid;
}
}
if (solve(l - 1).second == k) {
cout << solve(l - 1).first + (l - 1) * k << '\n';
} else {
cout << solve(l).first + l * k << '\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... |