#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define f first
#define s second
#define mp make_pair
int main() {
cin.tie(0); ios::sync_with_stdio(false);
int n, k; cin >> n >> k;
int A[n+1]; for (int i = 1; i <= n; i++) cin >> A[i];
ii dp[n+1][k+1];
dp[0][0] = mp(0, 0);
for (int i = 1; i <= n; i++) dp[i][0] = mp(1e9, 1e9);
for (int j = 1; j <= k; j++) {
stack<ii> s;
for (int i = j; i <= n; i++) {
dp[i][j] = mp(dp[i-1][j-1].f+dp[i-1][j-1].s, A[i]);
while (!s.empty() && s.top().s <= A[i]) {
if (s.top().f+A[i] < dp[i][j].f+dp[i][j].s) {
dp[i][j] = mp(s.top().f, A[i]);
}
s.pop();
}
if (!s.empty()) {
if (s.top().f + s.top().s < dp[i][j].f + dp[i][j].s) {
dp[i][j] = s.top();
}
}
s.push(dp[i][j]);
}
}
cout << dp[n][k].f + dp[n][k].s << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
1536 KB |
Output is correct |
3 |
Correct |
13 ms |
3584 KB |
Output is correct |
4 |
Correct |
95 ms |
27648 KB |
Output is correct |
5 |
Correct |
375 ms |
80632 KB |
Output is correct |
6 |
Correct |
19 ms |
4480 KB |
Output is correct |
7 |
Correct |
164 ms |
37368 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
26 ms |
8320 KB |
Output is correct |
15 |
Correct |
2 ms |
768 KB |
Output is correct |
16 |
Correct |
7 ms |
2560 KB |
Output is correct |
17 |
Correct |
7 ms |
1664 KB |
Output is correct |
18 |
Correct |
13 ms |
3968 KB |
Output is correct |
19 |
Correct |
119 ms |
27904 KB |
Output is correct |
20 |
Correct |
380 ms |
80820 KB |
Output is correct |
21 |
Correct |
18 ms |
4864 KB |
Output is correct |
22 |
Correct |
149 ms |
37880 KB |
Output is correct |
23 |
Correct |
15 ms |
3200 KB |
Output is correct |
24 |
Correct |
35 ms |
10280 KB |
Output is correct |
25 |
Correct |
366 ms |
80792 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
25 ms |
8448 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
7 ms |
2560 KB |
Output is correct |
31 |
Correct |
7 ms |
1920 KB |
Output is correct |
32 |
Correct |
12 ms |
3968 KB |
Output is correct |
33 |
Correct |
89 ms |
28032 KB |
Output is correct |
34 |
Correct |
397 ms |
81272 KB |
Output is correct |
35 |
Correct |
20 ms |
5376 KB |
Output is correct |
36 |
Correct |
164 ms |
38264 KB |
Output is correct |
37 |
Correct |
2 ms |
640 KB |
Output is correct |
38 |
Correct |
24 ms |
8320 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
0 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
0 ms |
384 KB |
Output is correct |