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
#define ar array
const int mxN=3e5;
int n, k, a[mxN], cnt[mxN];
ll dp[mxN];
int solve(ll x) {
ll s=0;
ar<ll, 2> cur={0, 0};
for (int i=0; i<n; ++i) {
s+=a[i];
dp[i]=i?dp[i-1]:0;
cnt[i]=i?cnt[i-1]:0;
if (cur[0]+s-x>dp[i])
dp[i]=cur[0]+s-x, cnt[i]=cur[1]+1;
if (dp[i]-s>cur[0])
cur={dp[i]-s, cnt[i]};
}
return cnt[n-1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i=0; i<n; ++i)
cin >> a[i];
ll lb=0, rb=1e15;
while(lb<rb) {
ll m=(lb+rb)/2;
if (solve(m)<=k)
rb=m;
else
lb=m+1;
}
solve(lb);
//cout << lb << " " << dp[n-1] << " " << cnt[n-1] << "\n";
cout << dp[n-1]+round(cnt[n-1]*lb);
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... |