//fdsasdfdf
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define ist pair<int,string>
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pb push_back
#define MASK(i) (1LL<<(i))
#define BIT(x,i) ((x>>(i))&1)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define FORJ(i,a,b,j) for(int i=(a);i<=(b);i+=j)
#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define matrix vector<vector<ll>>
typedef long long ll;
const int N=1e6+7;
const int base=259;
const int mod=1e9+7;
const ll MOD=1e9+804;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
template<class A,class B>
int add(A& x,B y)
{
if ((x+=y)>=mod) x-=mod;
return x;
}
template<class A,class B>
int deladd(A& x,B y)
{
if ((x-=y)<0) x+=mod;
return x;
}
template<class A,class B>
int nhan(A& x,B y)
{
if ((x*=y)>mod) x-=mod;
return x;
}
ii dp[N][2];
int n,k;
int a[N];
ii better(ii x,ii y)
{
if (x.F>y.F) return x;
if (x.F<y.F) return y;
if (x.S>y.S) return y;
return x;
}
ii solve(int pen)
{
FOR(i,1,n) dp[i][0]=dp[i][1]={-1e12,0};
dp[0][0]={0,0};
dp[0][1]={-1e12,0};
FOR(i,1,n)
{
dp[i][0]=better(dp[i][0],dp[i-1][0]);
dp[i][1]=better(dp[i][1],{dp[i-1][0].F+a[i]-pen,dp[i-1][0].S+1});
dp[i][1]=better(dp[i][1],{dp[i-1][1].F+a[i],dp[i-1][1].S});
dp[i][0]=better(dp[i][0],dp[i-1][1]);
}
return better(dp[n][0],dp[n][1]);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k;
FOR(i,1,n) cin>>a[i];
int l=0,r=1e12,ans=0;
while(l<=r)
{
int g=(l+r)/2;
ii res=solve(g);
if (res.S<=k)
{
ans=res.F+k*g;
r=g-1;
}
else l=g+1;
}
cout<<ans;
return 0;
}
//Mùa này là mùa của chúng ta
| # | 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... |