#include <bits/stdc++.h>
using namespace std;
const int N = (int)3e5, K = (int)3e5;
int n, k;
int a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 0;i < n;i ++)
cin >> a[i];
auto check = [&](int64_t penalty) -> pair<int, int64_t> {
vector<vector<int64_t>> dp(n, vector<int64_t>(2, LLONG_MIN));
vector<vector<int64_t>> count(n, vector<int64_t>(2, INT_MAX));
if (0 >= a[0] - penalty) {
dp[0][0] = 0;
count[0][0] = 0;
} else {
dp[0][0] = a[0] - penalty;
count[0][0] = 1;
}
dp[0][1] = a[0] - penalty;
count[0][1] = 1;
for (int i = 1;i < n;i ++) {
if (dp[i - 1][0] + a[i] - penalty > dp[i][0] || (dp[i - 1][0] + a[i] - penalty == dp[i][0] && count[i - 1][0] + 1 < count[i][0])) {
dp[i][0] = dp[i - 1][0] + a[i] - penalty;
count[i][0] = count[i - 1][0] + 1;
}
if (dp[i - 1][0] > dp[i][0] || (dp[i - 1][0] == dp[i][0] && count[i - 1][0] < count[i][0])) {
dp[i][0] = dp[i - 1][0];
count[i][0] = count[i - 1][0];
}
if (dp[i - 1][1] + a[i] > dp[i][0] || (dp[i - 1][1] + a[i] == dp[i][0] && count[i - 1][1] < count[i][0])) {
dp[i][0] = dp[i - 1][1] + a[i];
count[i][0] = count[i - 1][1];
}
if (dp[i - 1][1] + a[i] > dp[i][1] || (dp[i - 1][1] + a[i] == dp[i][1] && count[i - 1][1] < count[i][1])) {
dp[i][1] = dp[i - 1][1] + a[i];
count[i][1] = count[i - 1][1];
}
if (dp[i - 1][0] + a[i] - penalty > dp[i][1] || (dp[i - 1][0] + a[i] - penalty == dp[i][1] && count[i - 1][0] + 1 < count[i][1])) {
dp[i][1] = dp[i - 1][0] + a[i] - penalty;
count[i][1] = count[i - 1][0] + 1;
}
}
return {count[n - 1][0], dp[n - 1][0]};
};
int64_t lx = 0, rx = (int64_t)1e15;
while (lx < rx) {
int64_t g = lx + (rx - lx) / 2;
if (check(g).first <= k)
rx = g;
else
lx = g + 1;
}
pair<int, int64_t> res = check(lx);
int64_t ans = res.second + (k - res.first) * lx + lx * k;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1097 ms |
36084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
34692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1100 ms |
36952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
21 |
Correct |
12 ms |
460 KB |
Output is correct |
22 |
Correct |
13 ms |
456 KB |
Output is correct |
23 |
Correct |
12 ms |
464 KB |
Output is correct |
24 |
Correct |
13 ms |
556 KB |
Output is correct |
25 |
Correct |
13 ms |
572 KB |
Output is correct |
26 |
Correct |
13 ms |
476 KB |
Output is correct |
27 |
Correct |
12 ms |
460 KB |
Output is correct |
28 |
Correct |
12 ms |
568 KB |
Output is correct |
29 |
Correct |
13 ms |
460 KB |
Output is correct |
30 |
Correct |
12 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1097 ms |
36084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |