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 ll long long int
#define pii pair<ll,ll>
#define f first
#define s second
const int mxN = 3e5;
int n,k;
int a[mxN];
pii test(ll v){
pii dp[2];
pii dp2[2];
dp[0] = {0,0};
dp[1] = {0,0};
for (int i = 0; i < n; i++){
dp2[0] = max(dp[0],(pii){dp[1].f-v,dp[1].s+1});
dp2[1] = max((pii) {dp[0].f+a[i],dp[0].s}, (pii) {dp[1].f+a[i],dp[1].s});
swap(dp2,dp);
}
dp[1] = (pii) {dp[1].f-v,dp[1].s+1};
return max(dp[0],dp[1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
ll l = 0;
ll r = 1e15;
ll c;
while (r-l>1){
c = (l+r)/2;
if(test(c).s<k) r = c;
else l = c;
}
pii t = test(l);
// cout << l << " -= " << t.f << " " << t.s << '\n';
cout << t.f + k*l << "\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... |