#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, K = 105, oo = 1e9;
int n, k, a[N], dp[N][K];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < N; ++i) {
for(int j = 0; j < K; ++j) {
dp[i][j] = oo;
}
}
stack < int > st;
dp[0][0] = 0;
vector < int > v[k + 1];
v[0].push_back(0);
// for(int i = 0; i <= k; ++i) v[i].push_back(0);
for(int i = 1; i <= n; ++i) {
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
int x = 0;
if(!st.empty()) x = st.top();
for(int j = min(i, k); j >= 1; --j) {
dp[i][j] = dp[x][j];
int l = 0, r = (int)v[j - 1].size() - 1, ans = -1;
while(r >= l) {
int m = l + r >> 1;
if(v[j - 1][m] >= x) {
ans = v[j - 1][m];
r = m - 1;
} else {
l = m + 1;
}
}
if(~ans)dp[i][j] = min(dp[ans][j - 1] + a[i], dp[i][j]);
while(!v[j].empty() && dp[v[j].back()][j] >= dp[i][j]) v[j].pop_back();
v[j].push_back(i);
}
st.push(i);
}
cout << dp[n][k] << '\n';
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:29:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
41472 KB |
Output is correct |
2 |
Correct |
28 ms |
41464 KB |
Output is correct |
3 |
Correct |
28 ms |
41464 KB |
Output is correct |
4 |
Correct |
28 ms |
41472 KB |
Output is correct |
5 |
Correct |
28 ms |
41472 KB |
Output is correct |
6 |
Correct |
29 ms |
41472 KB |
Output is correct |
7 |
Correct |
28 ms |
41600 KB |
Output is correct |
8 |
Correct |
29 ms |
41472 KB |
Output is correct |
9 |
Correct |
28 ms |
41464 KB |
Output is correct |
10 |
Correct |
28 ms |
41472 KB |
Output is correct |
11 |
Correct |
28 ms |
41464 KB |
Output is correct |
12 |
Correct |
28 ms |
41464 KB |
Output is correct |
13 |
Correct |
28 ms |
41464 KB |
Output is correct |
14 |
Correct |
29 ms |
41464 KB |
Output is correct |
15 |
Correct |
29 ms |
41464 KB |
Output is correct |
16 |
Correct |
28 ms |
41472 KB |
Output is correct |
17 |
Correct |
28 ms |
41464 KB |
Output is correct |
18 |
Correct |
28 ms |
41464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
41464 KB |
Output is correct |
2 |
Correct |
28 ms |
41472 KB |
Output is correct |
3 |
Correct |
29 ms |
41472 KB |
Output is correct |
4 |
Correct |
28 ms |
41464 KB |
Output is correct |
5 |
Correct |
28 ms |
41472 KB |
Output is correct |
6 |
Correct |
28 ms |
41464 KB |
Output is correct |
7 |
Correct |
28 ms |
41416 KB |
Output is correct |
8 |
Correct |
28 ms |
41464 KB |
Output is correct |
9 |
Correct |
28 ms |
41472 KB |
Output is correct |
10 |
Correct |
28 ms |
41472 KB |
Output is correct |
11 |
Correct |
29 ms |
41472 KB |
Output is correct |
12 |
Correct |
28 ms |
41464 KB |
Output is correct |
13 |
Correct |
28 ms |
41472 KB |
Output is correct |
14 |
Correct |
28 ms |
41472 KB |
Output is correct |
15 |
Correct |
29 ms |
41472 KB |
Output is correct |
16 |
Correct |
29 ms |
41464 KB |
Output is correct |
17 |
Correct |
28 ms |
41488 KB |
Output is correct |
18 |
Correct |
28 ms |
41472 KB |
Output is correct |
19 |
Correct |
28 ms |
41464 KB |
Output is correct |
20 |
Correct |
29 ms |
41472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
41472 KB |
Output is correct |
2 |
Correct |
28 ms |
41464 KB |
Output is correct |
3 |
Correct |
28 ms |
41464 KB |
Output is correct |
4 |
Correct |
28 ms |
41472 KB |
Output is correct |
5 |
Correct |
28 ms |
41472 KB |
Output is correct |
6 |
Correct |
29 ms |
41472 KB |
Output is correct |
7 |
Correct |
28 ms |
41600 KB |
Output is correct |
8 |
Correct |
29 ms |
41472 KB |
Output is correct |
9 |
Correct |
28 ms |
41464 KB |
Output is correct |
10 |
Correct |
28 ms |
41472 KB |
Output is correct |
11 |
Correct |
28 ms |
41464 KB |
Output is correct |
12 |
Correct |
28 ms |
41464 KB |
Output is correct |
13 |
Correct |
28 ms |
41464 KB |
Output is correct |
14 |
Correct |
29 ms |
41464 KB |
Output is correct |
15 |
Correct |
29 ms |
41464 KB |
Output is correct |
16 |
Correct |
28 ms |
41472 KB |
Output is correct |
17 |
Correct |
28 ms |
41464 KB |
Output is correct |
18 |
Correct |
28 ms |
41464 KB |
Output is correct |
19 |
Correct |
29 ms |
41464 KB |
Output is correct |
20 |
Correct |
28 ms |
41472 KB |
Output is correct |
21 |
Correct |
29 ms |
41472 KB |
Output is correct |
22 |
Correct |
28 ms |
41464 KB |
Output is correct |
23 |
Correct |
28 ms |
41472 KB |
Output is correct |
24 |
Correct |
28 ms |
41464 KB |
Output is correct |
25 |
Correct |
28 ms |
41416 KB |
Output is correct |
26 |
Correct |
28 ms |
41464 KB |
Output is correct |
27 |
Correct |
28 ms |
41472 KB |
Output is correct |
28 |
Correct |
28 ms |
41472 KB |
Output is correct |
29 |
Correct |
29 ms |
41472 KB |
Output is correct |
30 |
Correct |
28 ms |
41464 KB |
Output is correct |
31 |
Correct |
28 ms |
41472 KB |
Output is correct |
32 |
Correct |
28 ms |
41472 KB |
Output is correct |
33 |
Correct |
29 ms |
41472 KB |
Output is correct |
34 |
Correct |
29 ms |
41464 KB |
Output is correct |
35 |
Correct |
28 ms |
41488 KB |
Output is correct |
36 |
Correct |
28 ms |
41472 KB |
Output is correct |
37 |
Correct |
28 ms |
41464 KB |
Output is correct |
38 |
Correct |
29 ms |
41472 KB |
Output is correct |
39 |
Correct |
28 ms |
41464 KB |
Output is correct |
40 |
Correct |
29 ms |
41472 KB |
Output is correct |
41 |
Correct |
28 ms |
41464 KB |
Output is correct |
42 |
Correct |
29 ms |
41472 KB |
Output is correct |
43 |
Correct |
29 ms |
41464 KB |
Output is correct |
44 |
Correct |
30 ms |
41472 KB |
Output is correct |
45 |
Correct |
28 ms |
41464 KB |
Output is correct |
46 |
Correct |
28 ms |
41472 KB |
Output is correct |
47 |
Correct |
29 ms |
41592 KB |
Output is correct |
48 |
Correct |
28 ms |
41600 KB |
Output is correct |
49 |
Correct |
29 ms |
41464 KB |
Output is correct |
50 |
Correct |
28 ms |
41464 KB |
Output is correct |
51 |
Correct |
29 ms |
41464 KB |
Output is correct |
52 |
Correct |
28 ms |
41472 KB |
Output is correct |
53 |
Correct |
28 ms |
41464 KB |
Output is correct |
54 |
Correct |
28 ms |
41464 KB |
Output is correct |
55 |
Correct |
29 ms |
41464 KB |
Output is correct |
56 |
Correct |
28 ms |
41472 KB |
Output is correct |
57 |
Correct |
28 ms |
41592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
41472 KB |
Output is correct |
2 |
Correct |
28 ms |
41464 KB |
Output is correct |
3 |
Correct |
28 ms |
41464 KB |
Output is correct |
4 |
Correct |
28 ms |
41472 KB |
Output is correct |
5 |
Correct |
28 ms |
41472 KB |
Output is correct |
6 |
Correct |
29 ms |
41472 KB |
Output is correct |
7 |
Correct |
28 ms |
41600 KB |
Output is correct |
8 |
Correct |
29 ms |
41472 KB |
Output is correct |
9 |
Correct |
28 ms |
41464 KB |
Output is correct |
10 |
Correct |
28 ms |
41472 KB |
Output is correct |
11 |
Correct |
28 ms |
41464 KB |
Output is correct |
12 |
Correct |
28 ms |
41464 KB |
Output is correct |
13 |
Correct |
28 ms |
41464 KB |
Output is correct |
14 |
Correct |
29 ms |
41464 KB |
Output is correct |
15 |
Correct |
29 ms |
41464 KB |
Output is correct |
16 |
Correct |
28 ms |
41472 KB |
Output is correct |
17 |
Correct |
28 ms |
41464 KB |
Output is correct |
18 |
Correct |
28 ms |
41464 KB |
Output is correct |
19 |
Correct |
29 ms |
41464 KB |
Output is correct |
20 |
Correct |
28 ms |
41472 KB |
Output is correct |
21 |
Correct |
29 ms |
41472 KB |
Output is correct |
22 |
Correct |
28 ms |
41464 KB |
Output is correct |
23 |
Correct |
28 ms |
41472 KB |
Output is correct |
24 |
Correct |
28 ms |
41464 KB |
Output is correct |
25 |
Correct |
28 ms |
41416 KB |
Output is correct |
26 |
Correct |
28 ms |
41464 KB |
Output is correct |
27 |
Correct |
28 ms |
41472 KB |
Output is correct |
28 |
Correct |
28 ms |
41472 KB |
Output is correct |
29 |
Correct |
29 ms |
41472 KB |
Output is correct |
30 |
Correct |
28 ms |
41464 KB |
Output is correct |
31 |
Correct |
28 ms |
41472 KB |
Output is correct |
32 |
Correct |
28 ms |
41472 KB |
Output is correct |
33 |
Correct |
29 ms |
41472 KB |
Output is correct |
34 |
Correct |
29 ms |
41464 KB |
Output is correct |
35 |
Correct |
28 ms |
41488 KB |
Output is correct |
36 |
Correct |
28 ms |
41472 KB |
Output is correct |
37 |
Correct |
28 ms |
41464 KB |
Output is correct |
38 |
Correct |
29 ms |
41472 KB |
Output is correct |
39 |
Correct |
28 ms |
41464 KB |
Output is correct |
40 |
Correct |
29 ms |
41472 KB |
Output is correct |
41 |
Correct |
28 ms |
41464 KB |
Output is correct |
42 |
Correct |
29 ms |
41472 KB |
Output is correct |
43 |
Correct |
29 ms |
41464 KB |
Output is correct |
44 |
Correct |
30 ms |
41472 KB |
Output is correct |
45 |
Correct |
28 ms |
41464 KB |
Output is correct |
46 |
Correct |
28 ms |
41472 KB |
Output is correct |
47 |
Correct |
29 ms |
41592 KB |
Output is correct |
48 |
Correct |
28 ms |
41600 KB |
Output is correct |
49 |
Correct |
29 ms |
41464 KB |
Output is correct |
50 |
Correct |
28 ms |
41464 KB |
Output is correct |
51 |
Correct |
29 ms |
41464 KB |
Output is correct |
52 |
Correct |
28 ms |
41472 KB |
Output is correct |
53 |
Correct |
28 ms |
41464 KB |
Output is correct |
54 |
Correct |
28 ms |
41464 KB |
Output is correct |
55 |
Correct |
29 ms |
41464 KB |
Output is correct |
56 |
Correct |
28 ms |
41472 KB |
Output is correct |
57 |
Correct |
28 ms |
41592 KB |
Output is correct |
58 |
Correct |
37 ms |
41948 KB |
Output is correct |
59 |
Correct |
35 ms |
41976 KB |
Output is correct |
60 |
Correct |
43 ms |
42364 KB |
Output is correct |
61 |
Correct |
143 ms |
47096 KB |
Output is correct |
62 |
Correct |
484 ms |
59808 KB |
Output is correct |
63 |
Correct |
50 ms |
43128 KB |
Output is correct |
64 |
Correct |
201 ms |
50452 KB |
Output is correct |
65 |
Correct |
28 ms |
41464 KB |
Output is correct |
66 |
Correct |
28 ms |
41464 KB |
Output is correct |
67 |
Correct |
28 ms |
41464 KB |
Output is correct |
68 |
Correct |
30 ms |
41464 KB |
Output is correct |
69 |
Correct |
29 ms |
41464 KB |
Output is correct |
70 |
Correct |
29 ms |
41464 KB |
Output is correct |
71 |
Correct |
49 ms |
41592 KB |
Output is correct |
72 |
Correct |
30 ms |
41600 KB |
Output is correct |
73 |
Correct |
35 ms |
41600 KB |
Output is correct |
74 |
Correct |
36 ms |
41848 KB |
Output is correct |
75 |
Correct |
43 ms |
41888 KB |
Output is correct |
76 |
Correct |
100 ms |
41976 KB |
Output is correct |
77 |
Correct |
243 ms |
42708 KB |
Output is correct |
78 |
Correct |
49 ms |
42492 KB |
Output is correct |
79 |
Correct |
139 ms |
42488 KB |
Output is correct |
80 |
Correct |
42 ms |
42488 KB |
Output is correct |
81 |
Correct |
65 ms |
42488 KB |
Output is correct |
82 |
Correct |
229 ms |
42588 KB |
Output is correct |
83 |
Correct |
28 ms |
41464 KB |
Output is correct |
84 |
Correct |
29 ms |
41472 KB |
Output is correct |
85 |
Correct |
41 ms |
41612 KB |
Output is correct |
86 |
Correct |
32 ms |
41592 KB |
Output is correct |
87 |
Correct |
33 ms |
41600 KB |
Output is correct |
88 |
Correct |
34 ms |
41984 KB |
Output is correct |
89 |
Correct |
41 ms |
41980 KB |
Output is correct |
90 |
Correct |
75 ms |
42104 KB |
Output is correct |
91 |
Correct |
152 ms |
42744 KB |
Output is correct |
92 |
Correct |
48 ms |
42616 KB |
Output is correct |
93 |
Correct |
97 ms |
42616 KB |
Output is correct |
94 |
Correct |
30 ms |
41600 KB |
Output is correct |
95 |
Correct |
59 ms |
43260 KB |
Output is correct |
96 |
Correct |
29 ms |
41472 KB |
Output is correct |
97 |
Correct |
28 ms |
41464 KB |
Output is correct |
98 |
Correct |
29 ms |
41464 KB |
Output is correct |
99 |
Correct |
28 ms |
41440 KB |
Output is correct |