이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |