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>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
using namespace std;
const int N = 3e5+1;
int n, k;
ll a[N];
pair<ll, int> dp[N][2];
pair<ll, int> solve(ll lambda) {
dp[0][1] = {-lambda,1};
dp[0][0] = {0,0};
for (int i = 1; i <= n; i++) {
dp[i][1] = max(
mp(dp[i-1][1].first+a[i], dp[i-1][1].second),
mp(dp[i-1][0].first+a[i]-lambda, dp[i-1][0].second+1)
);
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
}
return max(dp[n][0], dp[n][1]);
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; cin >> a[i++]);
ll l = 1, r = 1e18;
while (l < r) {
const ll mid = (l+r)/2;
if (solve(mid).second > k)
l = mid+1;
else
r = mid;
}
cout << solve(l).first + l*solve(l).second;
}
# | 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... |