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 <cstdio>
#include <cmath>
#include <climits>
#include <functional>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;
#define N 300005
#define ALL(x) x.begin(), x.end()
int n, k, a[N];
long long p[N];
pair<long double, int> dp[N][2];
const long double EPS = 1e-3;
int check(long double lambda)
{
dp[0][1] = make_pair(-1e18, 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][0].first + a[i] - lambda, dp[i-1][0].second + 1),
make_pair(dp[i-1][1].first + a[i], dp[i-1][1].second));
}
return max(dp[n][0], dp[n][1]).second >= k;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
ll l = 0, r = 1e18;
while (l <= r)
{
ll y = (l+r)/2;
if (check(y)) l = y + 1;
else r = y - 1;
}
check(l-1);
cout << (long long)round(k * (l-1) + 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... |