#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++) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
11732 KB |
Output is correct |
2 |
Correct |
83 ms |
12000 KB |
Output is correct |
3 |
Correct |
81 ms |
12120 KB |
Output is correct |
4 |
Correct |
80 ms |
12028 KB |
Output is correct |
5 |
Correct |
86 ms |
12004 KB |
Output is correct |
6 |
Correct |
94 ms |
11724 KB |
Output is correct |
7 |
Correct |
90 ms |
11736 KB |
Output is correct |
8 |
Correct |
82 ms |
12064 KB |
Output is correct |
9 |
Correct |
78 ms |
11868 KB |
Output is correct |
10 |
Correct |
83 ms |
11876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
72 ms |
12120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
11856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
11732 KB |
Output is correct |
2 |
Correct |
83 ms |
12000 KB |
Output is correct |
3 |
Correct |
81 ms |
12120 KB |
Output is correct |
4 |
Correct |
80 ms |
12028 KB |
Output is correct |
5 |
Correct |
86 ms |
12004 KB |
Output is correct |
6 |
Correct |
94 ms |
11724 KB |
Output is correct |
7 |
Correct |
90 ms |
11736 KB |
Output is correct |
8 |
Correct |
82 ms |
12064 KB |
Output is correct |
9 |
Correct |
78 ms |
11868 KB |
Output is correct |
10 |
Correct |
83 ms |
11876 KB |
Output is correct |
11 |
Incorrect |
72 ms |
12120 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |