#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
23172 KB |
Output is correct |
2 |
Correct |
236 ms |
23612 KB |
Output is correct |
3 |
Correct |
250 ms |
23872 KB |
Output is correct |
4 |
Correct |
229 ms |
23636 KB |
Output is correct |
5 |
Correct |
232 ms |
23580 KB |
Output is correct |
6 |
Correct |
217 ms |
23192 KB |
Output is correct |
7 |
Correct |
225 ms |
23180 KB |
Output is correct |
8 |
Correct |
232 ms |
23496 KB |
Output is correct |
9 |
Correct |
227 ms |
23124 KB |
Output is correct |
10 |
Correct |
234 ms |
23736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
23336 KB |
Output is correct |
2 |
Correct |
202 ms |
23848 KB |
Output is correct |
3 |
Correct |
187 ms |
23304 KB |
Output is correct |
4 |
Correct |
186 ms |
23480 KB |
Output is correct |
5 |
Correct |
237 ms |
26008 KB |
Output is correct |
6 |
Correct |
196 ms |
24396 KB |
Output is correct |
7 |
Correct |
194 ms |
25036 KB |
Output is correct |
8 |
Correct |
240 ms |
26560 KB |
Output is correct |
9 |
Correct |
224 ms |
25920 KB |
Output is correct |
10 |
Correct |
181 ms |
24864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
23496 KB |
Output is correct |
2 |
Correct |
240 ms |
23192 KB |
Output is correct |
3 |
Correct |
244 ms |
23568 KB |
Output is correct |
4 |
Correct |
241 ms |
23340 KB |
Output is correct |
5 |
Correct |
275 ms |
23476 KB |
Output is correct |
6 |
Correct |
246 ms |
23500 KB |
Output is correct |
7 |
Correct |
256 ms |
23792 KB |
Output is correct |
8 |
Correct |
245 ms |
23492 KB |
Output is correct |
9 |
Correct |
258 ms |
23632 KB |
Output is correct |
10 |
Correct |
243 ms |
23760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
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 |
1 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 |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
23172 KB |
Output is correct |
2 |
Correct |
236 ms |
23612 KB |
Output is correct |
3 |
Correct |
250 ms |
23872 KB |
Output is correct |
4 |
Correct |
229 ms |
23636 KB |
Output is correct |
5 |
Correct |
232 ms |
23580 KB |
Output is correct |
6 |
Correct |
217 ms |
23192 KB |
Output is correct |
7 |
Correct |
225 ms |
23180 KB |
Output is correct |
8 |
Correct |
232 ms |
23496 KB |
Output is correct |
9 |
Correct |
227 ms |
23124 KB |
Output is correct |
10 |
Correct |
234 ms |
23736 KB |
Output is correct |
11 |
Correct |
190 ms |
23336 KB |
Output is correct |
12 |
Correct |
202 ms |
23848 KB |
Output is correct |
13 |
Correct |
187 ms |
23304 KB |
Output is correct |
14 |
Correct |
186 ms |
23480 KB |
Output is correct |
15 |
Correct |
237 ms |
26008 KB |
Output is correct |
16 |
Correct |
196 ms |
24396 KB |
Output is correct |
17 |
Correct |
194 ms |
25036 KB |
Output is correct |
18 |
Correct |
240 ms |
26560 KB |
Output is correct |
19 |
Correct |
224 ms |
25920 KB |
Output is correct |
20 |
Correct |
181 ms |
24864 KB |
Output is correct |
21 |
Correct |
235 ms |
23496 KB |
Output is correct |
22 |
Correct |
240 ms |
23192 KB |
Output is correct |
23 |
Correct |
244 ms |
23568 KB |
Output is correct |
24 |
Correct |
241 ms |
23340 KB |
Output is correct |
25 |
Correct |
275 ms |
23476 KB |
Output is correct |
26 |
Correct |
246 ms |
23500 KB |
Output is correct |
27 |
Correct |
256 ms |
23792 KB |
Output is correct |
28 |
Correct |
245 ms |
23492 KB |
Output is correct |
29 |
Correct |
258 ms |
23632 KB |
Output is correct |
30 |
Correct |
243 ms |
23760 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 |
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 |
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 |
1 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 |
588 KB |
Output is correct |
54 |
Correct |
2 ms |
348 KB |
Output is correct |
55 |
Correct |
2 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
264 ms |
26196 KB |
Output is correct |
62 |
Correct |
267 ms |
26952 KB |
Output is correct |
63 |
Correct |
268 ms |
26208 KB |
Output is correct |
64 |
Correct |
303 ms |
26748 KB |
Output is correct |
65 |
Correct |
320 ms |
26732 KB |
Output is correct |
66 |
Correct |
321 ms |
26664 KB |
Output is correct |
67 |
Correct |
266 ms |
26256 KB |
Output is correct |
68 |
Correct |
245 ms |
26544 KB |
Output is correct |
69 |
Correct |
241 ms |
26240 KB |
Output is correct |
70 |
Correct |
237 ms |
26120 KB |
Output is correct |