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>
using namespace std;
#define int long long
typedef long double ld;
const int INF = 1e9;
int n,k;
int a[300005];
int dp[300005];
int cnt[300005];
int pref[300005];
ld coef;
void solve()
{
int mxm=0,cate=0;
for(int i=1;i<=n;i++)
{
pref[i] = pref[i-1] + a[i];
dp[i] = dp[i-1];
cnt[i] = cnt[i-1];
if(dp[i]-pref[i]>mxm || (dp[i]-pref[i]==mxm && cnt[i]>cate))
{
mxm = dp[i]-pref[i];
cate = cnt[i];
}
int aux = mxm + pref[i] - coef;
if(aux>dp[i] || (aux==dp[i] && cate+1>cnt[i]))
{
dp[i] = aux;
cnt[i] = cate+1;
}
}
}
/**
dp[i] = max(dp[x] - pref[x]) + pref[i] - coef
*/
signed main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
int st,dr,ans=0;
st=0;
dr=n*INF;
while(st<=dr)
{
coef = (st+dr)/2;
solve();
if(cnt[n]<=k)
{
ans=coef;
dr=coef-1;
}
else
st=coef+1;
}
coef=ans;
solve();
cout<<dp[n] + k*coef;
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... |