#include <bits/stdc++.h>
#define int long long
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
int32_t main() {
int n, K;
cin >> n >> K;
vector<int> v(n+1);
for(int i=1; i<=n; i++) cin >> v[i];
ll l=0, r=1e15, ans=0;
vector<vector<pll> > dp(n+1, vector<pll>(2));
auto check = [&](int mid) {
for(int i=0; i<=n; i++)
for(int j=0; j<2; j++) dp[i][j] = {0, 0};
dp[0][1] = {-1e18, 0};
for(int i=1; i<=n; i++) {
dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
dp[i][1] = max(pll{ dp[i-1][1].first + v[i], dp[i-1][1].second }, pll{ dp[i-1][0].first + v[i] - mid, dp[i-1][0].second + 1 });
}
return max(dp[n][0], dp[n][1]).second;
};
while(l <= r) {
ll mid = (l + r) / 2;
if(check(mid) >= K) ans = mid, l = mid + 1;
else r = mid - 1;
}
check(ans);
cout << max(dp[n][0], dp[n][1]).first + K * ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
23164 KB |
Output is correct |
2 |
Correct |
229 ms |
23632 KB |
Output is correct |
3 |
Correct |
222 ms |
23780 KB |
Output is correct |
4 |
Correct |
226 ms |
23632 KB |
Output is correct |
5 |
Correct |
232 ms |
23636 KB |
Output is correct |
6 |
Correct |
226 ms |
23120 KB |
Output is correct |
7 |
Correct |
211 ms |
23124 KB |
Output is correct |
8 |
Correct |
215 ms |
23476 KB |
Output is correct |
9 |
Correct |
213 ms |
23204 KB |
Output is correct |
10 |
Correct |
235 ms |
23636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
23192 KB |
Output is correct |
2 |
Correct |
193 ms |
23636 KB |
Output is correct |
3 |
Correct |
190 ms |
23128 KB |
Output is correct |
4 |
Correct |
183 ms |
23500 KB |
Output is correct |
5 |
Correct |
224 ms |
23184 KB |
Output is correct |
6 |
Correct |
189 ms |
23380 KB |
Output is correct |
7 |
Correct |
185 ms |
23904 KB |
Output is correct |
8 |
Correct |
236 ms |
23740 KB |
Output is correct |
9 |
Correct |
228 ms |
23128 KB |
Output is correct |
10 |
Correct |
186 ms |
23728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
23484 KB |
Output is correct |
2 |
Correct |
235 ms |
23344 KB |
Output is correct |
3 |
Correct |
244 ms |
23488 KB |
Output is correct |
4 |
Correct |
245 ms |
23340 KB |
Output is correct |
5 |
Correct |
253 ms |
23484 KB |
Output is correct |
6 |
Correct |
247 ms |
23700 KB |
Output is correct |
7 |
Correct |
243 ms |
23756 KB |
Output is correct |
8 |
Correct |
241 ms |
23476 KB |
Output is correct |
9 |
Correct |
243 ms |
23860 KB |
Output is correct |
10 |
Correct |
241 ms |
23796 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
408 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
408 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
408 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
584 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
23164 KB |
Output is correct |
2 |
Correct |
229 ms |
23632 KB |
Output is correct |
3 |
Correct |
222 ms |
23780 KB |
Output is correct |
4 |
Correct |
226 ms |
23632 KB |
Output is correct |
5 |
Correct |
232 ms |
23636 KB |
Output is correct |
6 |
Correct |
226 ms |
23120 KB |
Output is correct |
7 |
Correct |
211 ms |
23124 KB |
Output is correct |
8 |
Correct |
215 ms |
23476 KB |
Output is correct |
9 |
Correct |
213 ms |
23204 KB |
Output is correct |
10 |
Correct |
235 ms |
23636 KB |
Output is correct |
11 |
Correct |
193 ms |
23192 KB |
Output is correct |
12 |
Correct |
193 ms |
23636 KB |
Output is correct |
13 |
Correct |
190 ms |
23128 KB |
Output is correct |
14 |
Correct |
183 ms |
23500 KB |
Output is correct |
15 |
Correct |
224 ms |
23184 KB |
Output is correct |
16 |
Correct |
189 ms |
23380 KB |
Output is correct |
17 |
Correct |
185 ms |
23904 KB |
Output is correct |
18 |
Correct |
236 ms |
23740 KB |
Output is correct |
19 |
Correct |
228 ms |
23128 KB |
Output is correct |
20 |
Correct |
186 ms |
23728 KB |
Output is correct |
21 |
Correct |
265 ms |
23484 KB |
Output is correct |
22 |
Correct |
235 ms |
23344 KB |
Output is correct |
23 |
Correct |
244 ms |
23488 KB |
Output is correct |
24 |
Correct |
245 ms |
23340 KB |
Output is correct |
25 |
Correct |
253 ms |
23484 KB |
Output is correct |
26 |
Correct |
247 ms |
23700 KB |
Output is correct |
27 |
Correct |
243 ms |
23756 KB |
Output is correct |
28 |
Correct |
241 ms |
23476 KB |
Output is correct |
29 |
Correct |
243 ms |
23860 KB |
Output is correct |
30 |
Correct |
241 ms |
23796 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 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 |
1 ms |
408 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
2 ms |
584 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
2 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
257 ms |
23192 KB |
Output is correct |
62 |
Correct |
265 ms |
23892 KB |
Output is correct |
63 |
Correct |
241 ms |
23200 KB |
Output is correct |
64 |
Correct |
282 ms |
23636 KB |
Output is correct |
65 |
Correct |
297 ms |
23696 KB |
Output is correct |
66 |
Correct |
257 ms |
23480 KB |
Output is correct |
67 |
Correct |
270 ms |
23448 KB |
Output is correct |
68 |
Correct |
239 ms |
23632 KB |
Output is correct |
69 |
Correct |
232 ms |
23380 KB |
Output is correct |
70 |
Correct |
236 ms |
23196 KB |
Output is correct |