#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5 + 25;
ll a[MAXN], n, k;
pair <ll, ll> eval (ll mid) {
pair <ll, ll> dp[n + 1][2];
dp[1][0] = {0, 0};
dp[1][1] = {a[1] - mid, 1};
for (int i = 2; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
pair <ll, ll> g = {dp[i - 1][0].first + a[i] - mid, dp[i - 1][0].second + 1};
pair <ll, ll> h = {dp[i - 1][1].first + a[i], dp[i - 1][1].second};
dp[i][1] = max(g, h);
dp[i][0] = max(dp[i][0], {0ll, 0ll});
dp[i][1] = max(dp[i][1], {a[i] - mid, 1ll});
}
return max({dp[n][0], dp[n][1], {0ll, 0ll}});
}
void solve () {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll l = 0, r = 1e18, ans = 0;
while (l <= r) {
ll mid = (l + r) / 2;
if (eval(mid).second >= k) {
l = mid + 1; ans = mid;
} else {
r = mid - 1;
}
}
cout << eval(ans).first + ans * k << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
11604 KB |
Output is correct |
2 |
Correct |
101 ms |
14884 KB |
Output is correct |
3 |
Correct |
102 ms |
15044 KB |
Output is correct |
4 |
Correct |
101 ms |
14936 KB |
Output is correct |
5 |
Correct |
100 ms |
14676 KB |
Output is correct |
6 |
Correct |
100 ms |
14420 KB |
Output is correct |
7 |
Correct |
103 ms |
14420 KB |
Output is correct |
8 |
Correct |
103 ms |
14828 KB |
Output is correct |
9 |
Correct |
98 ms |
14656 KB |
Output is correct |
10 |
Correct |
105 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
11864 KB |
Output is correct |
2 |
Correct |
90 ms |
12148 KB |
Output is correct |
3 |
Correct |
89 ms |
11648 KB |
Output is correct |
4 |
Correct |
97 ms |
11868 KB |
Output is correct |
5 |
Correct |
96 ms |
14464 KB |
Output is correct |
6 |
Correct |
90 ms |
12888 KB |
Output is correct |
7 |
Correct |
91 ms |
13148 KB |
Output is correct |
8 |
Correct |
107 ms |
14828 KB |
Output is correct |
9 |
Correct |
102 ms |
14428 KB |
Output is correct |
10 |
Correct |
96 ms |
13232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
11896 KB |
Output is correct |
2 |
Correct |
123 ms |
11868 KB |
Output is correct |
3 |
Correct |
148 ms |
11860 KB |
Output is correct |
4 |
Correct |
147 ms |
11892 KB |
Output is correct |
5 |
Correct |
117 ms |
11868 KB |
Output is correct |
6 |
Correct |
154 ms |
11856 KB |
Output is correct |
7 |
Correct |
151 ms |
12112 KB |
Output is correct |
8 |
Correct |
122 ms |
12012 KB |
Output is correct |
9 |
Correct |
125 ms |
12148 KB |
Output is correct |
10 |
Correct |
135 ms |
12120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Correct |
0 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
464 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Correct |
0 ms |
476 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
2 ms |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
356 KB |
Output is correct |
26 |
Correct |
2 ms |
496 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
2 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
356 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
11604 KB |
Output is correct |
2 |
Correct |
101 ms |
14884 KB |
Output is correct |
3 |
Correct |
102 ms |
15044 KB |
Output is correct |
4 |
Correct |
101 ms |
14936 KB |
Output is correct |
5 |
Correct |
100 ms |
14676 KB |
Output is correct |
6 |
Correct |
100 ms |
14420 KB |
Output is correct |
7 |
Correct |
103 ms |
14420 KB |
Output is correct |
8 |
Correct |
103 ms |
14828 KB |
Output is correct |
9 |
Correct |
98 ms |
14656 KB |
Output is correct |
10 |
Correct |
105 ms |
14676 KB |
Output is correct |
11 |
Correct |
91 ms |
11864 KB |
Output is correct |
12 |
Correct |
90 ms |
12148 KB |
Output is correct |
13 |
Correct |
89 ms |
11648 KB |
Output is correct |
14 |
Correct |
97 ms |
11868 KB |
Output is correct |
15 |
Correct |
96 ms |
14464 KB |
Output is correct |
16 |
Correct |
90 ms |
12888 KB |
Output is correct |
17 |
Correct |
91 ms |
13148 KB |
Output is correct |
18 |
Correct |
107 ms |
14828 KB |
Output is correct |
19 |
Correct |
102 ms |
14428 KB |
Output is correct |
20 |
Correct |
96 ms |
13232 KB |
Output is correct |
21 |
Correct |
153 ms |
11896 KB |
Output is correct |
22 |
Correct |
123 ms |
11868 KB |
Output is correct |
23 |
Correct |
148 ms |
11860 KB |
Output is correct |
24 |
Correct |
147 ms |
11892 KB |
Output is correct |
25 |
Correct |
117 ms |
11868 KB |
Output is correct |
26 |
Correct |
154 ms |
11856 KB |
Output is correct |
27 |
Correct |
151 ms |
12112 KB |
Output is correct |
28 |
Correct |
122 ms |
12012 KB |
Output is correct |
29 |
Correct |
125 ms |
12148 KB |
Output is correct |
30 |
Correct |
135 ms |
12120 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 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 |
1 ms |
464 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
464 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
460 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
408 KB |
Output is correct |
49 |
Correct |
0 ms |
476 KB |
Output is correct |
50 |
Correct |
1 ms |
356 KB |
Output is correct |
51 |
Correct |
2 ms |
356 KB |
Output is correct |
52 |
Correct |
1 ms |
356 KB |
Output is correct |
53 |
Correct |
1 ms |
556 KB |
Output is correct |
54 |
Correct |
1 ms |
352 KB |
Output is correct |
55 |
Correct |
1 ms |
356 KB |
Output is correct |
56 |
Correct |
2 ms |
496 KB |
Output is correct |
57 |
Correct |
1 ms |
356 KB |
Output is correct |
58 |
Correct |
2 ms |
356 KB |
Output is correct |
59 |
Correct |
1 ms |
356 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
141 ms |
14836 KB |
Output is correct |
62 |
Correct |
139 ms |
15236 KB |
Output is correct |
63 |
Correct |
130 ms |
14676 KB |
Output is correct |
64 |
Correct |
142 ms |
15116 KB |
Output is correct |
65 |
Correct |
145 ms |
14940 KB |
Output is correct |
66 |
Correct |
152 ms |
15036 KB |
Output is correct |
67 |
Correct |
150 ms |
14676 KB |
Output is correct |
68 |
Correct |
122 ms |
14932 KB |
Output is correct |
69 |
Correct |
115 ms |
14672 KB |
Output is correct |
70 |
Correct |
115 ms |
14672 KB |
Output is correct |