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<iostream>
#include<vector>
#include<iomanip>
#include<algorithm>
using namespace std;
#define ll long long
#define ld long double
#define vi vector<ll>
#define vvi vector<vi>
#define pi pair<ll, ll>
#define ppi pair<ll, pi>
#define vp vector<ppi>
#define vvp vector<vp>
#define pn pair<node, node>
#define vpn vector<pn>
ll n, k;
vi L;
struct node {
ll v = -1, tv = -1, sa = -1;
};
node maks(node a, node b) {
if (a.v >= b.v) return a;
return b;
}
vpn M;
node oink(ll c, ll p = 0, bool q = 0) {
if (p == n) return {0, 0, 0};
if (q) {
if (M[p].first.v != -1) return M[p].first;
if (L[p] >= 0) {
node a = oink(c, p+1, 1);
a.v += L[p];
a.tv += L[p];
return M[p].first = a;
}
node a = oink(c, p+1, 1);
a.v += L[p];
a.tv += L[p];
node b = oink(c, p+1, 0);
return M[p].first = maks(a, b);
}
if (M[p].second.v != -1) return M[p].second;
node a = oink(c, p+1, 1);
a.v += L[p] - c;
a.tv += L[p];
a.sa++;
node b = oink(c, p+1, 0);
return M[p].second = maks(a, b);
}
/*
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
L = vi(n);
for (auto &i : L) cin >> i;
ll l = 0, r = 1000000000000;
ll ans;
while (l < r) {
//cerr << l << " " << r << endl;
M = vpn(n, pn({-1, -1, -1}, {-1, -1, -1}));
ll m = (l+r)/2;
node a = oink(m);
if (a.sa == k) {
ans = a.tv;
break;
}
if (a.sa > k) {
l = m+1;
continue;
}
//ans = a.tv;
r = m;
}
cout << ans << "\n";
}
/*
6 1
1 -2 3 -1 5 -6
6 2
1 2 3 -10 5 6
6 4
-1 -2 -1 0 -5 -1
*/
# | 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... |