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 int long long
pair<int,int> solve(int l,int a[],int n){
vector<vector<pair<int,int>>> DP(n,vector<pair<int,int>>(2));
DP[0][0]={0,0};
DP[0][1]={a[0]-l,1};
for(int i=1;i<n;i++){
DP[i][0]=max(DP[i-1][0],DP[i-1][1]);
DP[i][1]=max(DP[i-1][1],{DP[i-1][0].first-l,DP[i-1][0].second+1});
DP[i][1].first+=a[i];
}
return max(DP[n-1][0],DP[n-1][1]);
}
signed main() {
int n,k;
cin >> n >> k;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
}
int low=0,high=1e18;
while(low<high){
int mid=(high+low+1)/2;
if(solve(mid,a,n).second>=k){
low=mid;
}
else{
high=mid-1;
}
}
cout << solve(low,a,n).first+low*k;
}
# | 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... |