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;
#define int long long
#define ld long double
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> A(n + 1);
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
int l = 0, r = 1e18, res = 0;
while (l <= r) {
int md = (l + r) / 2;
vector<array<array<int, 2>, 2>> dp(n + 1);
dp[0][1][0] = -md, dp[0][1][1] = 1;
for (int i = 1; i <= n; i++) {
dp[i][1][0] = -md, dp[i][1][1] = 1;
if (dp[i - 1][0][0] - md + A[i] > dp[i][1][0]) {
dp[i][1][0] = dp[i - 1][0][0] - md + A[i];
dp[i][1][1] = dp[i - 1][0][1] + 1;
}
if (dp[i - 1][1][0] + A[i] > dp[i][1][0]) {
dp[i][1][0] = dp[i - 1][1][0] + A[i];
dp[i][1][1] = dp[i - 1][1][1];
}
if (dp[i - 1][0][0] > dp[i][0][0]) {
dp[i][0][0] = dp[i - 1][0][0];
dp[i][0][1] = dp[i - 1][0][1];
}
if (dp[i - 1][1][0] > dp[i][0][0]) {
dp[i][0][0] = dp[i - 1][1][0];
dp[i][0][1] = dp[i - 1][1][1];
}
}
if (dp.back()[0][0] < dp.back()[1][0]) {
swap(dp.back()[0], dp.back()[1]);
}
if (dp.back()[0][1] <= k) {
r = md - 1, res = max(res, dp.back()[0][0] + md * dp.back()[0][1]);
}
else if (dp.back()[0][1] > k){
l = md + 1;
}
}
cout << res;
}
# | 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... |