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>
#define N 300005
using namespace std;
typedef pair<long double, int> pi;
pi DP[N][2];
int n, k, seq[N];
int doDP(long double pen) {
for(auto&i: DP) for(auto&j: i) j = make_pair(-9e18,0);
DP[0][0] = {0,0};
for(int i = 1; i <= n+1; i ++) {
DP[i][0] = max(DP[i-1][0], DP[i-1][1]);
DP[i][1] = max(DP[i-1][1], make_pair(DP[i-1][0].first-pen, DP[i-1][0].second+1));
DP[i][1].first += seq[i];
}
return DP[n+1][0].second;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
int pc = 0;
for(int i = 1; i <= n; i ++) {
cin >> seq[i];
if(seq[i] > 0 && seq[i-1] <= 0) pc ++;
}
k = min(k, pc);
long double l=0,h=1e13;
for(int i = 0; i < 100; i ++) {
long double m = (l+h)/2;
int res = doDP(m);
if(res > k) {
l = m;
} else {
h = m;
}
}
cout << (long long)round(DP[n+1][0].first + k*l);
}
# | 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... |