#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll INF = LLONG_MAX / 2;
const int iINF = INT_MAX / 2;
const int MAXN = 3e5 + 5;
int n, k;
ll arr[MAXN], dp[MAXN][2];
int cnt[MAXN][2];
pll eval(ll c)
{
dp[0][1] = -INF;
for (int i = 1; i <= n; i++)
{
if (dp[i - 1][0] >= dp[i - 1][1])
{
dp[i][0] = dp[i - 1][0];
cnt[i][0] = cnt[i - 1][0];
}
else
{
dp[i][0] = dp[i - 1][1];
cnt[i][0] = cnt[i - 1][1];
}
if (dp[i - 1][0] - c >= dp[i - 1][1])
{
dp[i][1] = arr[i] + dp[i - 1][0] - c;
cnt[i][1] = cnt[i - 1][0] + 1;
}
else
{
dp[i][1] = arr[i] + dp[i - 1][1];
cnt[i][1] = cnt[i - 1][1];
}
}
return max(pll(dp[n][0], cnt[n][0]), pll(dp[n][1], cnt[n][1]));
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> arr[i];
ll answer = 0;
ll lo = 0, hi = INF;
while (lo <= hi)
{
ll mi = lo + (hi - lo) / 2;
auto [optv, optc] = eval(mi);
if (optc <= k)
hi = mi - 1, answer = max(answer, optv + optc * mi);
else
lo = mi + 1;
}
cout << answer << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
12124 KB |
Output is correct |
2 |
Correct |
50 ms |
12368 KB |
Output is correct |
3 |
Correct |
51 ms |
12708 KB |
Output is correct |
4 |
Correct |
49 ms |
12620 KB |
Output is correct |
5 |
Correct |
51 ms |
12380 KB |
Output is correct |
6 |
Correct |
51 ms |
12116 KB |
Output is correct |
7 |
Correct |
51 ms |
12120 KB |
Output is correct |
8 |
Correct |
56 ms |
12368 KB |
Output is correct |
9 |
Correct |
49 ms |
12380 KB |
Output is correct |
10 |
Correct |
65 ms |
12496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
10576 KB |
Output is correct |
2 |
Correct |
45 ms |
10840 KB |
Output is correct |
3 |
Correct |
43 ms |
10588 KB |
Output is correct |
4 |
Correct |
44 ms |
10616 KB |
Output is correct |
5 |
Correct |
51 ms |
12368 KB |
Output is correct |
6 |
Correct |
43 ms |
10576 KB |
Output is correct |
7 |
Correct |
44 ms |
10844 KB |
Output is correct |
8 |
Correct |
51 ms |
12628 KB |
Output is correct |
9 |
Correct |
49 ms |
12112 KB |
Output is correct |
10 |
Correct |
42 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12624 KB |
Output is correct |
2 |
Correct |
68 ms |
12416 KB |
Output is correct |
3 |
Correct |
67 ms |
12532 KB |
Output is correct |
4 |
Correct |
67 ms |
12604 KB |
Output is correct |
5 |
Correct |
66 ms |
12708 KB |
Output is correct |
6 |
Correct |
67 ms |
12692 KB |
Output is correct |
7 |
Correct |
70 ms |
12624 KB |
Output is correct |
8 |
Correct |
66 ms |
12712 KB |
Output is correct |
9 |
Correct |
67 ms |
12624 KB |
Output is correct |
10 |
Correct |
70 ms |
12832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
352 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
21 |
Correct |
1 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
360 KB |
Output is correct |
25 |
Correct |
1 ms |
360 KB |
Output is correct |
26 |
Correct |
1 ms |
360 KB |
Output is correct |
27 |
Correct |
1 ms |
360 KB |
Output is correct |
28 |
Correct |
1 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
360 KB |
Output is correct |
30 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
12124 KB |
Output is correct |
2 |
Correct |
50 ms |
12368 KB |
Output is correct |
3 |
Correct |
51 ms |
12708 KB |
Output is correct |
4 |
Correct |
49 ms |
12620 KB |
Output is correct |
5 |
Correct |
51 ms |
12380 KB |
Output is correct |
6 |
Correct |
51 ms |
12116 KB |
Output is correct |
7 |
Correct |
51 ms |
12120 KB |
Output is correct |
8 |
Correct |
56 ms |
12368 KB |
Output is correct |
9 |
Correct |
49 ms |
12380 KB |
Output is correct |
10 |
Correct |
65 ms |
12496 KB |
Output is correct |
11 |
Correct |
42 ms |
10576 KB |
Output is correct |
12 |
Correct |
45 ms |
10840 KB |
Output is correct |
13 |
Correct |
43 ms |
10588 KB |
Output is correct |
14 |
Correct |
44 ms |
10616 KB |
Output is correct |
15 |
Correct |
51 ms |
12368 KB |
Output is correct |
16 |
Correct |
43 ms |
10576 KB |
Output is correct |
17 |
Correct |
44 ms |
10844 KB |
Output is correct |
18 |
Correct |
51 ms |
12628 KB |
Output is correct |
19 |
Correct |
49 ms |
12112 KB |
Output is correct |
20 |
Correct |
42 ms |
10844 KB |
Output is correct |
21 |
Correct |
75 ms |
12624 KB |
Output is correct |
22 |
Correct |
68 ms |
12416 KB |
Output is correct |
23 |
Correct |
67 ms |
12532 KB |
Output is correct |
24 |
Correct |
67 ms |
12604 KB |
Output is correct |
25 |
Correct |
66 ms |
12708 KB |
Output is correct |
26 |
Correct |
67 ms |
12692 KB |
Output is correct |
27 |
Correct |
70 ms |
12624 KB |
Output is correct |
28 |
Correct |
66 ms |
12712 KB |
Output is correct |
29 |
Correct |
67 ms |
12624 KB |
Output is correct |
30 |
Correct |
70 ms |
12832 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
352 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
344 KB |
Output is correct |
46 |
Correct |
1 ms |
352 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
352 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
360 KB |
Output is correct |
51 |
Correct |
1 ms |
360 KB |
Output is correct |
52 |
Correct |
1 ms |
360 KB |
Output is correct |
53 |
Correct |
1 ms |
360 KB |
Output is correct |
54 |
Correct |
1 ms |
360 KB |
Output is correct |
55 |
Correct |
1 ms |
360 KB |
Output is correct |
56 |
Correct |
1 ms |
360 KB |
Output is correct |
57 |
Correct |
1 ms |
360 KB |
Output is correct |
58 |
Correct |
1 ms |
360 KB |
Output is correct |
59 |
Correct |
1 ms |
360 KB |
Output is correct |
60 |
Correct |
1 ms |
360 KB |
Output is correct |
61 |
Correct |
102 ms |
12372 KB |
Output is correct |
62 |
Correct |
101 ms |
12636 KB |
Output is correct |
63 |
Correct |
75 ms |
12376 KB |
Output is correct |
64 |
Correct |
100 ms |
12604 KB |
Output is correct |
65 |
Correct |
100 ms |
12628 KB |
Output is correct |
66 |
Correct |
96 ms |
12628 KB |
Output is correct |
67 |
Correct |
92 ms |
12380 KB |
Output is correct |
68 |
Correct |
70 ms |
12800 KB |
Output is correct |
69 |
Correct |
68 ms |
12368 KB |
Output is correct |
70 |
Correct |
75 ms |
12492 KB |
Output is correct |