이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |