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 int long long
using namespace std;
int sp[300005],v[300005];
pair <int,int> dp[300005];
int n,k;
pair <int,int> calc(int lambda)
{
pair<int,int> Max={0,0};
for(int i=1; i<=n; ++i)
{
dp[i]=dp[i-1];
if(Max.first+sp[i]-lambda>dp[i].first || (Max.first+sp[i]-lambda==dp[i].first && Max.second+1<dp[i].second))
{
dp[i].first=Max.first+sp[i]-lambda;
dp[i].second=Max.second+1;
}
if(dp[i].first>Max.first+sp[i] || (dp[i].first==Max.first+sp[i] && dp[i].second<Max.second))
{
Max.first=dp[i].first-sp[i];
Max.second=dp[i].second;
}
}
return dp[n];
}
int solve()
{
int st=1;
int dr=3e14;
while(st<=dr)
{
int mij=(st+dr)/2;
pair<int,int> val=calc(mij);
if(val.second==k)
{
return val.first+k*mij;
}
if(val.second<k)dr=mij-1;
else st=mij+1;
}
pair<int,int> val=calc(st);
return val.first+val.second*st;
}
signed main()
{
///freopen("feast.in","r",stdin);
///freopen("feast.out","w",stdout);
cin.sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1; i<=n; ++i)cin>>v[i];
for(int i=1; i<=n; ++i)sp[i]=sp[i-1]+v[i];
cout<<solve();
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... |