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 ll long long
const int nx=3e5+5;
ll n, k, qs[nx];
pair<ll, ll> solve(ll x)
{
pair<ll, ll> mx, dp;
for (int i=1; i<=n; i++)
{
dp=max(dp, {mx.first+qs[i]-x, mx.second-1});
mx=max(mx, {dp.first-qs[i], dp.second});
}
return dp;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>qs[i], qs[i]+=qs[i-1];
ll l=0, r=3e14+5;
while (l<r)
{
ll md=(l+r)/2;
if (-solve(md).second>k) l=md+1;
else r=md;
}
cout<<solve(l).first-solve(l).second*l;
}
# | 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... |