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
#define f first
#define s second
int n,k;
vector<int> vect1;
pair<int,int> find(int val){
vector<int> dp(n,0);
vector<int> take(n,0);
int currmin = 0;
int currtake = 0;
for(int i=0;i<n;i++){
if(i!=0){
dp[i] = dp[i-1];
take[i] = take[i-1];
}
if(vect1[i]-currmin-val>dp[i]){
dp[i] = vect1[i]-currmin-val;
take[i] = currtake+1;
}
if(vect1[i]-dp[i]<currmin){
currmin = vect1[i]-dp[i];
currtake = take[i];
}
}
return {dp[n-1],take[n-1]};
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> k;
int input;
for(int i=0;i<n;i++){
cin >> input;
vect1.push_back(input);
}
for(int i=1;i<n;i++){
vect1[i] += vect1[i-1];
}
int lo = 0,hi = 10e15;
while(lo+1!=hi){
int mid = (lo+hi)/2;
if(find(mid).s>=k){
lo = mid;
}
else{
hi = mid;
}
}
//cout << lo << " " << hi << endl;
cout << find(lo).f+lo*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... |