#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int K = 105;
const int INF = 0x3f3f3f3f;
int a[N], L[N], minF[N];
int dp[K][N];
int n, k;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[1][0] = 0;
for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]);
for (int i = 1; i <= n; ++i)
for (L[i] = i - 1; L[i] && a[L[i]] <= a[i]; ) L[i] = L[L[i]];
for (int i = 2; i <= k; ++i) {
minF[i - 1] = INF;
for (int j = i; j <= n; ++j) {
minF[j] = dp[i - 1][j - 1];
for (int t = j - 1; t > L[j]; t = L[t])
minF[j] = min(minF[j], minF[t]);
dp[i][j] = min(dp[i][L[j]], minF[j] + a[j]);
}
}
cout << dp[k][n] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
41592 KB |
Output is correct |
2 |
Correct |
34 ms |
41464 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
35 ms |
41464 KB |
Output is correct |
5 |
Correct |
32 ms |
41464 KB |
Output is correct |
6 |
Correct |
35 ms |
41464 KB |
Output is correct |
7 |
Correct |
37 ms |
41356 KB |
Output is correct |
8 |
Correct |
34 ms |
41444 KB |
Output is correct |
9 |
Correct |
33 ms |
41448 KB |
Output is correct |
10 |
Correct |
38 ms |
41464 KB |
Output is correct |
11 |
Correct |
33 ms |
41464 KB |
Output is correct |
12 |
Correct |
34 ms |
41464 KB |
Output is correct |
13 |
Correct |
34 ms |
41464 KB |
Output is correct |
14 |
Correct |
33 ms |
41464 KB |
Output is correct |
15 |
Correct |
35 ms |
41464 KB |
Output is correct |
16 |
Correct |
35 ms |
41472 KB |
Output is correct |
17 |
Correct |
36 ms |
41464 KB |
Output is correct |
18 |
Correct |
32 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41580 KB |
Output is correct |
2 |
Correct |
32 ms |
41600 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
32 ms |
41464 KB |
Output is correct |
5 |
Correct |
32 ms |
41464 KB |
Output is correct |
6 |
Correct |
39 ms |
41444 KB |
Output is correct |
7 |
Correct |
39 ms |
41464 KB |
Output is correct |
8 |
Correct |
26 ms |
41464 KB |
Output is correct |
9 |
Correct |
35 ms |
41464 KB |
Output is correct |
10 |
Correct |
38 ms |
41464 KB |
Output is correct |
11 |
Correct |
34 ms |
41464 KB |
Output is correct |
12 |
Correct |
35 ms |
41464 KB |
Output is correct |
13 |
Correct |
33 ms |
41472 KB |
Output is correct |
14 |
Correct |
33 ms |
41464 KB |
Output is correct |
15 |
Correct |
35 ms |
41464 KB |
Output is correct |
16 |
Correct |
33 ms |
41476 KB |
Output is correct |
17 |
Correct |
33 ms |
41464 KB |
Output is correct |
18 |
Correct |
31 ms |
41472 KB |
Output is correct |
19 |
Correct |
33 ms |
41464 KB |
Output is correct |
20 |
Correct |
31 ms |
41472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
41696 KB |
Output is correct |
2 |
Correct |
34 ms |
41464 KB |
Output is correct |
3 |
Correct |
32 ms |
41464 KB |
Output is correct |
4 |
Correct |
37 ms |
41436 KB |
Output is correct |
5 |
Correct |
31 ms |
41436 KB |
Output is correct |
6 |
Correct |
32 ms |
41336 KB |
Output is correct |
7 |
Correct |
37 ms |
41464 KB |
Output is correct |
8 |
Correct |
33 ms |
41464 KB |
Output is correct |
9 |
Correct |
33 ms |
41468 KB |
Output is correct |
10 |
Correct |
34 ms |
41472 KB |
Output is correct |
11 |
Correct |
33 ms |
41472 KB |
Output is correct |
12 |
Correct |
38 ms |
41420 KB |
Output is correct |
13 |
Correct |
33 ms |
41464 KB |
Output is correct |
14 |
Correct |
32 ms |
41404 KB |
Output is correct |
15 |
Correct |
33 ms |
41464 KB |
Output is correct |
16 |
Correct |
34 ms |
41464 KB |
Output is correct |
17 |
Correct |
33 ms |
41464 KB |
Output is correct |
18 |
Correct |
32 ms |
41464 KB |
Output is correct |
19 |
Correct |
35 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
41592 KB |
Output is correct |
2 |
Correct |
58 ms |
42104 KB |
Output is correct |
3 |
Correct |
56 ms |
42232 KB |
Output is correct |
4 |
Correct |
92 ms |
42360 KB |
Output is correct |
5 |
Correct |
165 ms |
43404 KB |
Output is correct |
6 |
Correct |
83 ms |
43384 KB |
Output is correct |
7 |
Correct |
114 ms |
43256 KB |
Output is correct |
8 |
Correct |
34 ms |
41464 KB |
Output is correct |
9 |
Correct |
34 ms |
41380 KB |
Output is correct |
10 |
Correct |
32 ms |
41464 KB |
Output is correct |
11 |
Correct |
37 ms |
41484 KB |
Output is correct |
12 |
Correct |
35 ms |
41464 KB |
Output is correct |
13 |
Correct |
37 ms |
41456 KB |
Output is correct |
14 |
Correct |
43 ms |
41592 KB |
Output is correct |
15 |
Correct |
42 ms |
41660 KB |
Output is correct |
16 |
Correct |
41 ms |
41624 KB |
Output is correct |
17 |
Correct |
48 ms |
42104 KB |
Output is correct |
18 |
Correct |
55 ms |
42232 KB |
Output is correct |
19 |
Correct |
102 ms |
42360 KB |
Output is correct |
20 |
Correct |
175 ms |
43292 KB |
Output is correct |
21 |
Correct |
99 ms |
43256 KB |
Output is correct |
22 |
Correct |
116 ms |
43356 KB |
Output is correct |
23 |
Correct |
78 ms |
42848 KB |
Output is correct |
24 |
Correct |
102 ms |
43260 KB |
Output is correct |
25 |
Correct |
170 ms |
43360 KB |
Output is correct |
26 |
Correct |
32 ms |
41464 KB |
Output is correct |
27 |
Correct |
38 ms |
41592 KB |
Output is correct |
28 |
Correct |
43 ms |
41588 KB |
Output is correct |
29 |
Correct |
45 ms |
41592 KB |
Output is correct |
30 |
Correct |
38 ms |
41592 KB |
Output is correct |
31 |
Correct |
64 ms |
42104 KB |
Output is correct |
32 |
Correct |
66 ms |
42232 KB |
Output is correct |
33 |
Correct |
84 ms |
42232 KB |
Output is correct |
34 |
Correct |
142 ms |
43256 KB |
Output is correct |
35 |
Correct |
85 ms |
43300 KB |
Output is correct |
36 |
Correct |
136 ms |
43304 KB |
Output is correct |
37 |
Correct |
35 ms |
41592 KB |
Output is correct |
38 |
Correct |
41 ms |
41592 KB |
Output is correct |
39 |
Correct |
33 ms |
41464 KB |
Output is correct |
40 |
Correct |
35 ms |
41436 KB |
Output is correct |
41 |
Correct |
34 ms |
41464 KB |
Output is correct |
42 |
Correct |
33 ms |
41464 KB |
Output is correct |