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;
const int MAX = 3e5 + 10;
const long long oo = (long long)300000 * 1000000000;
const long double EPS = 1e-3;
int N, K;
int a[MAX];
pair<long double, int> dp[MAX][2];
bool OK(long double lambda) {
dp[0][0] = {0, 0}, dp[0][1] = {-oo, 0};
for (int i = 1; i <= N; ++i) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] =
max(make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second),
make_pair(dp[i - 1][0].first + a[i] - lambda,
dp[i - 1][0].second + 1));
}
return max(dp[N][0], dp[N][1]).second >= K;
};
int main(){
ios_base::sync_with_stdio(0);
cin >> N >> K;
for(int i=1; i<=N; i++){
cin >> a[i];
}
long double lower = 0, upper = oo;
while (lower + EPS < upper) {
long double mid = (lower + upper) / 2;
if (OK(mid)) {
lower = mid;
} else {
upper = mid;
}
}
long double lambda = lower;
OK(lambda);
cout << (long long)round(lambda * K + max(dp[N][0], dp[N][1]).first);
return 0;
}
# | 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... |