#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int K = 105;
const int INF = 0x3f3f3f3f;
int a[N];
int 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 = 2; i <= k; ++i) {
stack<pair<int, int> > S;
for (int j = i; j <= n; ++j) {
int minF = dp[i - 1][j - 1];
while (!S.empty() && a[S.top().second] <= a[j]) {
minF = min(minF, S.top().first);
S.pop();
}
dp[i][j] = min(dp[i][S.empty() ? 0 : S.top().second], minF + a[j]);
S.push(make_pair(minF, j));
}
}
cout << dp[k][n] ;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
41464 KB |
Output is correct |
2 |
Correct |
34 ms |
41464 KB |
Output is correct |
3 |
Correct |
34 ms |
41464 KB |
Output is correct |
4 |
Correct |
34 ms |
41464 KB |
Output is correct |
5 |
Correct |
34 ms |
41436 KB |
Output is correct |
6 |
Correct |
35 ms |
41464 KB |
Output is correct |
7 |
Correct |
34 ms |
41436 KB |
Output is correct |
8 |
Correct |
34 ms |
41436 KB |
Output is correct |
9 |
Correct |
35 ms |
41452 KB |
Output is correct |
10 |
Correct |
34 ms |
41464 KB |
Output is correct |
11 |
Correct |
35 ms |
41464 KB |
Output is correct |
12 |
Correct |
34 ms |
41384 KB |
Output is correct |
13 |
Correct |
34 ms |
41464 KB |
Output is correct |
14 |
Correct |
35 ms |
41464 KB |
Output is correct |
15 |
Correct |
34 ms |
41464 KB |
Output is correct |
16 |
Correct |
34 ms |
41464 KB |
Output is correct |
17 |
Correct |
35 ms |
41464 KB |
Output is correct |
18 |
Correct |
34 ms |
41372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
41464 KB |
Output is correct |
2 |
Correct |
35 ms |
41404 KB |
Output is correct |
3 |
Correct |
35 ms |
41464 KB |
Output is correct |
4 |
Correct |
35 ms |
41464 KB |
Output is correct |
5 |
Correct |
35 ms |
41464 KB |
Output is correct |
6 |
Correct |
34 ms |
41464 KB |
Output is correct |
7 |
Correct |
34 ms |
41468 KB |
Output is correct |
8 |
Correct |
34 ms |
41500 KB |
Output is correct |
9 |
Correct |
34 ms |
41464 KB |
Output is correct |
10 |
Correct |
34 ms |
41440 KB |
Output is correct |
11 |
Correct |
35 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 |
35 ms |
41464 KB |
Output is correct |
15 |
Correct |
34 ms |
41464 KB |
Output is correct |
16 |
Correct |
35 ms |
41464 KB |
Output is correct |
17 |
Correct |
35 ms |
41464 KB |
Output is correct |
18 |
Correct |
34 ms |
41436 KB |
Output is correct |
19 |
Correct |
34 ms |
41464 KB |
Output is correct |
20 |
Correct |
34 ms |
41464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
41464 KB |
Output is correct |
2 |
Correct |
34 ms |
41464 KB |
Output is correct |
3 |
Correct |
34 ms |
41464 KB |
Output is correct |
4 |
Correct |
35 ms |
41436 KB |
Output is correct |
5 |
Correct |
35 ms |
41436 KB |
Output is correct |
6 |
Correct |
35 ms |
41464 KB |
Output is correct |
7 |
Correct |
34 ms |
41464 KB |
Output is correct |
8 |
Correct |
35 ms |
41464 KB |
Output is correct |
9 |
Correct |
35 ms |
41464 KB |
Output is correct |
10 |
Correct |
35 ms |
41464 KB |
Output is correct |
11 |
Correct |
35 ms |
41464 KB |
Output is correct |
12 |
Correct |
35 ms |
41464 KB |
Output is correct |
13 |
Correct |
35 ms |
41464 KB |
Output is correct |
14 |
Correct |
35 ms |
41464 KB |
Output is correct |
15 |
Correct |
35 ms |
41464 KB |
Output is correct |
16 |
Correct |
35 ms |
41468 KB |
Output is correct |
17 |
Correct |
35 ms |
41464 KB |
Output is correct |
18 |
Correct |
35 ms |
41464 KB |
Output is correct |
19 |
Correct |
35 ms |
41464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
41464 KB |
Output is correct |
2 |
Correct |
60 ms |
41948 KB |
Output is correct |
3 |
Correct |
63 ms |
41948 KB |
Output is correct |
4 |
Correct |
100 ms |
42020 KB |
Output is correct |
5 |
Correct |
211 ms |
42616 KB |
Output is correct |
6 |
Correct |
93 ms |
42488 KB |
Output is correct |
7 |
Correct |
145 ms |
42656 KB |
Output is correct |
8 |
Correct |
35 ms |
41464 KB |
Output is correct |
9 |
Correct |
35 ms |
41464 KB |
Output is correct |
10 |
Correct |
35 ms |
41592 KB |
Output is correct |
11 |
Correct |
34 ms |
41464 KB |
Output is correct |
12 |
Correct |
35 ms |
41464 KB |
Output is correct |
13 |
Correct |
36 ms |
41464 KB |
Output is correct |
14 |
Correct |
55 ms |
41464 KB |
Output is correct |
15 |
Correct |
40 ms |
41464 KB |
Output is correct |
16 |
Correct |
44 ms |
41516 KB |
Output is correct |
17 |
Correct |
59 ms |
41848 KB |
Output is correct |
18 |
Correct |
64 ms |
41976 KB |
Output is correct |
19 |
Correct |
112 ms |
41976 KB |
Output is correct |
20 |
Correct |
248 ms |
42488 KB |
Output is correct |
21 |
Correct |
92 ms |
42488 KB |
Output is correct |
22 |
Correct |
160 ms |
42616 KB |
Output is correct |
23 |
Correct |
95 ms |
42488 KB |
Output is correct |
24 |
Correct |
104 ms |
42488 KB |
Output is correct |
25 |
Correct |
249 ms |
42488 KB |
Output is correct |
26 |
Correct |
35 ms |
41464 KB |
Output is correct |
27 |
Correct |
35 ms |
41592 KB |
Output is correct |
28 |
Correct |
53 ms |
41644 KB |
Output is correct |
29 |
Correct |
40 ms |
41592 KB |
Output is correct |
30 |
Correct |
44 ms |
41592 KB |
Output is correct |
31 |
Correct |
59 ms |
41864 KB |
Output is correct |
32 |
Correct |
63 ms |
42104 KB |
Output is correct |
33 |
Correct |
106 ms |
42176 KB |
Output is correct |
34 |
Correct |
237 ms |
42824 KB |
Output is correct |
35 |
Correct |
94 ms |
42832 KB |
Output is correct |
36 |
Correct |
156 ms |
42824 KB |
Output is correct |
37 |
Correct |
40 ms |
41592 KB |
Output is correct |
38 |
Correct |
52 ms |
41592 KB |
Output is correct |
39 |
Correct |
35 ms |
41508 KB |
Output is correct |
40 |
Correct |
36 ms |
41464 KB |
Output is correct |
41 |
Correct |
35 ms |
41464 KB |
Output is correct |
42 |
Correct |
35 ms |
41492 KB |
Output is correct |