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<iostream>
#include<vector>
#include<iomanip>
#include<algorithm>
using namespace std;
#define ll long long
#define ld long double
#define vi vector<ll>
#define vvi vector<vi>
#define pi pair<ll, ll>
#define ppi pair<ll, pi>
#define vp vector<ppi>
#define vvp vector<vp>
#define MP make_pair
ll n, k;
vi L;
pi oink(ll c) {
pi dp[n][2];
dp[0][0] = {0, 0};
dp[0][1] = {L[0] - c, 1};
for (int i = 1; i < n; i++) {
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(MP(dp[i-1][1].first + L[i], dp[i-1][1].second), MP(dp[i-1][0].first + L[i] - c, dp[i-1][0].second + 1));
}
return max(dp[n-1][0], dp[n-1][1]);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
L = vi(n);
for (auto &i : L) cin >> i;
ll l = 0, r = 1e18;
ll ans = 0;
while (l < r) {
//cerr << l << " " << r << endl;
ll m = (l+r)/2;
pi a = oink(m);
if (a.second == k) {
ans = a.first + m*k;
break;
}
if (a.second > k) {
l = m+1;
continue;
}
ans = a.first + a.second*m;
r = m;
}
cout << ans << "\n";
}
/*
6 1
1 -2 3 -1 5 -6
6 2
1 2 3 -10 5 6
6 4
-1 -2 -1 0 -5 -1
*/
# | 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... |