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 ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define pb push_back
using namespace std;
const ll N=3e5+9;
const ll mod=1e9+7;
ll t,n,k,i,m,x,a[N],tot[N],q,l,b[N],c[N],mu[N],sl=0,xor_basis[64],len=0,r;
ll dp[N][2];
ii check(ll pen)
{
dp[0][1]=-pen;
dp[0][0]=0;
ll res=0;
ll cnt=0;
for(i=1;i<=n;i++)
{
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=max(dp[i-1][0]-pen,dp[i-1][1])+a[i];
res=max(res,dp[i][1]);
}
bool op=0;
ll tot=res;
if(res==0) return {0,0};
for(i=n;i>0;i--)
if(res==dp[i][1])
{
res-=a[i];
if(!op){ cnt++;}
//if(res==0) break;
op=1;
}
else{if(op)res+=pen; op=0;}
return {tot+pen*cnt,cnt};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>a[i];
ll l=0,r=1e18,kq=0;
while(l<=r)
{
ll mid=(l+r)/2;
ii ans=check(mid);
// cout<<mid<<" "<<ans.se<<'\n';
if(ans.se<=k){
if(ans.se>0)
kq=max(kq,ans.fi);
r=mid-1;
}
else l=mid+1;
}
cout<<kq;
}
/*
6 2
1 2 3 -10 5 6
*/
# | 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... |