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<ld, ll>
#define ppi pair<ld, pi>
#define vp vector<ppi>
#define vvp vector<vp>
ll n, k;
vi L;
vvp M;
ppi oink(ld c, ll p = 0, bool q = 0) {
if (p == n) return {0, {0, 0}};
if (M[p][q].first != -1) return M[p][q];
if (q) {
if (L[p] >= 0) {
ppi a = oink(c, p+1, 1);
a.first += L[p];
a.second.second += L[p];
return M[p][q] = a;
}
ppi a = oink(c, p+1, 1);
a.first += L[p];
a.second.second += L[p];
ppi b = oink(c, p+1, 0);
return M[p][q] = max(a, b);
}
ppi a = oink(c, p+1, 1);
a.first += L[p] - c;
a.second.second += L[p];
a.second.first++;
ppi b = oink(c, p+1, 0);
return M[p][q] = max(a, b);
}
/*pi oinkoink(ld c, ll p = 0, bool q = 0) {
if (p == n) return {0, 0};
if (M[p][q].first != -1) return M[p][q];
if (q) {
if (L[p] >= 0) {
pi a = oink(c, p+1, 1);
a.first += L[p];
a.second += L[p];
return M[p][q] = a;
}
pi a = oink(c, p+1, 1);
a.first += L[p];
a.second += L[p];
pi b = oink(c, p+1, 0);
return M[p][q] = max(a, b);
}
pi a = oink(c, p+1, 1);
a.first += L[p] - c;
a.second += L[p];
pi b = oink(c, p+1, 0);
return M[p][q] = max(a, b);
}
*/
int main() {
cin >> n >> k;
L = vi(n);
for (auto &i : L) cin >> i;
ld l = 0, r = 1000000000000000;
ll ans;
while (l < r) {
//cerr << l << " " << r << endl;
M = vvp(n, vp(2, {-1, {-1, -1}}));
ld m = (l+r)/2;
ppi a = oink(m);
if (a.second.first == k) {
ans = a.second.second;
break;
}
if (a.second.first > k) {
l = m;
continue;
}
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... |