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;
typedef long long ll;
ll n, k;
vector<ll> a;
pair<ll, ll> solve(ll p) {
ll dp = 0;
ll dpSeg = a[0] - p;
ll ps = 0;
ll psSeg = 1;
for (int i = 1; i < n; i++) {
ll tdp = dp, tdpSeg = dpSeg, tps = ps, tpsSeg = psSeg;
if (tdpSeg > tdp) {
dp = tdpSeg;
ps = tpsSeg;
}
if (tdp - p > tdpSeg) {
dpSeg = tdp - p;
psSeg = tps + 1;
}
dpSeg += a[i];
}
if (dpSeg > dp) {
dp = dpSeg;
ps = psSeg;
}
return {dp, ps};
}
int main() {
cin >> n >> k;
a = vector<ll>(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll l = 0, r = 1ll << 50;
ll bestK = 1ll << 50;
ll bestAns = 0;
ll pen = 0;
while (l <= r) {
ll mid = pen = (l + r) / 2;
bestK = solve(mid).second;
bestAns = solve(mid).first;
if (bestK > k) {
l = mid + 1;
}
else {
if (l == r) break;
r = mid;
}
}
ll val = bestAns + k * pen;
cout << val;
}
# | 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... |