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 F first
#define S second
#define ll long long
#define pii pair<ll,ll>
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
using namespace std;
bool operator>(pair<ll,int> a,pair<ll,int>b){
return make_pair(a.F,-1 * a.S) > make_pair(b.F, -1 * b.S);
}
bool operator<(pair<ll,int> a,pair<ll,int>b){
return make_pair(a.F,-1 * a.S) < make_pair(b.F, -1 * b.S);
}
int n,k;
ll a[mxN];
pair<ll,int> dp[2][2];
pair<ll,int> num;
ll ans;
bool solve(ll lam){
// cout<<lam<<'\n';
for(int i = 0;i <= 1;i++){
dp[i][0] = make_pair(0,0);
dp[i][1] = make_pair(0,0);
}
for(int i = 1;i <= n;i++){
// cout<<(i & 1)<<' '<<(~i & 1)<<'\n';
dp[(i & 1)][0] = max(dp[(~i & 1)][0],dp[(~i & 1)][1]);
dp[(i & 1)][1] = make_pair(dp[(~i & 1)][0].F + a[i] - lam,dp[~i & 1][0].S + 1);
if(i > 1) dp[(i & 1)][1] = max(dp[(i & 1)][1],make_pair(dp[~i & 1][1].F + a[i],dp[~i & 1][1].S));
// cout<<dp[(i & 1)][0].F<<' '<<dp[(i & 1)][0].S<<' '<<dp[(i & 1)][1].F<<' '<<dp[(i & 1)][1].S<<'\n';
}
num = max(dp[n & 1][0],dp[n & 1][1]);
// cout<<num.F<<' '<<num.S<<'\n';
return (num.S <= k);
}
signed main(){
// cout<<"TEST";
cin >>n>>k;
for(int i = 1;i <= n;i++) cin >>a[i];
ll l = 0,r = 1e16,md;
while(l < r){
md = (l + r) / 2;
if(solve(md)){
r = md;
ans = num.F + num.S * md;
}else l = md + 1;
}
cout<<ans;
}
# | 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... |