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 m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define int long long
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
signed main(){
fast_resp;
int n,k;
cin>>n>>k;
vec<int>a(n);
for(auto &z : a) cin>>z;
vec<ll>pref(n,0);
for(int i=0;i<n;i++) pref[i]=(i?pref[i-1]:0)+a[i];
auto check=[&](ll x){
vec<pll>dp(n+1,m_p(-inf,-1));
pll mx={0,0};
dp[0]={0,0};
// cout<<"CHECK "<<x<<endl;
for(int i=0;i<n;i++){
dp[i+1]=dp[i];
umax(dp[i+1],m_p(dp[i].f-x,dp[i].s+1));
umax(dp[i+1],m_p(mx.f+pref[i]-x,mx.s+1));
umax(mx,m_p(dp[i+1].f-pref[i],dp[i+1].s));
// cout<<"I "<<dp[i+1].f<<endl;
}
// cout<<"W "<<dp[n].f<<' '<<dp[n].s<<endl;
return dp[n];
};
ll l=0,r=1e9;
while(l!=r){
ll tm=(l+r)/2;
if(check(tm).s<k) r=tm;
else l=tm+1;
}
--l;
// cout<<k<<' '<<l<<endl;
cout<<check(l).f+1ll*k*l;
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... |