#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> ii;
const int N = 1e5 + 7, M = 107;
int n, k, a[N], L[N], dp[N][M];
stack<int> st;
int T[N][M];
void upd(int i, int j, int v) {
for (; i <= n; i += i & -i) T[i][j] = min(T[i][j], v);
}
int get(int i, int l, int r) {
int res = 2e9;
while (l <= r) {
if (r - (r & -r) >= l) {
res = min(res, T[r][i]);
r -= r & -r;
}
else {
res = min(res, dp[r][i]);
--r;
}
}
return res;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
memset(T, 0x3f, sizeof T);
dp[0][1] = 0;
for (int i = 1; i <= n; i++) {
dp[i][1] = max(dp[i - 1][1], a[i]);
upd(i, 1, dp[i][1]);
}
for (int i = 1; i <= n; i++) {
while (st.size() && a[st.top()] <= a[i]) st.pop();
L[i] = (st.empty() ? 1 : st.top());
if (st.empty()) {
for (int j = 2; j <= min(i, k); j++) {
dp[i][j] = get(j - 1, 1, i - 1) + a[i];
upd(i, j, dp[i][j]);
}
}
else {
for (int j = 2; j <= min(i, k); j++) {
dp[i][j] = min(dp[L[i]][j], get(j - 1, L[i], i - 1) + a[i]);
upd(i, j, dp[i][j]);
}
}
st.push(i);
// f(i, j) = min(f(k - 1, j - 1) + max(a[k]..a[i]))
}
cout << dp[n][k];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
84052 KB |
Output is correct |
2 |
Correct |
32 ms |
84048 KB |
Output is correct |
3 |
Correct |
35 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84016 KB |
Output is correct |
5 |
Correct |
33 ms |
84060 KB |
Output is correct |
6 |
Correct |
34 ms |
84052 KB |
Output is correct |
7 |
Correct |
33 ms |
84052 KB |
Output is correct |
8 |
Correct |
32 ms |
84020 KB |
Output is correct |
9 |
Correct |
33 ms |
84000 KB |
Output is correct |
10 |
Correct |
33 ms |
84040 KB |
Output is correct |
11 |
Correct |
32 ms |
84072 KB |
Output is correct |
12 |
Correct |
32 ms |
84044 KB |
Output is correct |
13 |
Correct |
34 ms |
84048 KB |
Output is correct |
14 |
Correct |
32 ms |
83996 KB |
Output is correct |
15 |
Correct |
34 ms |
84076 KB |
Output is correct |
16 |
Correct |
34 ms |
83996 KB |
Output is correct |
17 |
Correct |
36 ms |
84044 KB |
Output is correct |
18 |
Correct |
35 ms |
83984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
84172 KB |
Output is correct |
2 |
Correct |
34 ms |
84012 KB |
Output is correct |
3 |
Correct |
32 ms |
84020 KB |
Output is correct |
4 |
Correct |
33 ms |
84064 KB |
Output is correct |
5 |
Correct |
35 ms |
83988 KB |
Output is correct |
6 |
Correct |
35 ms |
84040 KB |
Output is correct |
7 |
Correct |
32 ms |
83964 KB |
Output is correct |
8 |
Correct |
33 ms |
84044 KB |
Output is correct |
9 |
Correct |
37 ms |
84028 KB |
Output is correct |
10 |
Correct |
33 ms |
84044 KB |
Output is correct |
11 |
Correct |
37 ms |
84068 KB |
Output is correct |
12 |
Correct |
33 ms |
84004 KB |
Output is correct |
13 |
Correct |
33 ms |
84044 KB |
Output is correct |
14 |
Correct |
33 ms |
84044 KB |
Output is correct |
15 |
Correct |
32 ms |
83964 KB |
Output is correct |
16 |
Correct |
34 ms |
84060 KB |
Output is correct |
17 |
Correct |
32 ms |
83988 KB |
Output is correct |
18 |
Correct |
33 ms |
84068 KB |
Output is correct |
19 |
Correct |
36 ms |
84096 KB |
Output is correct |
20 |
Correct |
34 ms |
84040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
84052 KB |
Output is correct |
2 |
Correct |
32 ms |
84048 KB |
Output is correct |
3 |
Correct |
35 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84016 KB |
Output is correct |
5 |
Correct |
33 ms |
84060 KB |
Output is correct |
6 |
Correct |
34 ms |
84052 KB |
Output is correct |
7 |
Correct |
33 ms |
84052 KB |
Output is correct |
8 |
Correct |
32 ms |
84020 KB |
Output is correct |
9 |
Correct |
33 ms |
84000 KB |
Output is correct |
10 |
Correct |
33 ms |
84040 KB |
Output is correct |
11 |
Correct |
32 ms |
84072 KB |
Output is correct |
12 |
Correct |
32 ms |
84044 KB |
Output is correct |
13 |
Correct |
34 ms |
84048 KB |
Output is correct |
14 |
Correct |
32 ms |
83996 KB |
Output is correct |
15 |
Correct |
34 ms |
84076 KB |
Output is correct |
16 |
Correct |
34 ms |
83996 KB |
Output is correct |
17 |
Correct |
36 ms |
84044 KB |
Output is correct |
18 |
Correct |
35 ms |
83984 KB |
Output is correct |
19 |
Correct |
34 ms |
84172 KB |
Output is correct |
20 |
Correct |
34 ms |
84012 KB |
Output is correct |
21 |
Correct |
32 ms |
84020 KB |
Output is correct |
22 |
Correct |
33 ms |
84064 KB |
Output is correct |
23 |
Correct |
35 ms |
83988 KB |
Output is correct |
24 |
Correct |
35 ms |
84040 KB |
Output is correct |
25 |
Correct |
32 ms |
83964 KB |
Output is correct |
26 |
Correct |
33 ms |
84044 KB |
Output is correct |
27 |
Correct |
37 ms |
84028 KB |
Output is correct |
28 |
Correct |
33 ms |
84044 KB |
Output is correct |
29 |
Correct |
37 ms |
84068 KB |
Output is correct |
30 |
Correct |
33 ms |
84004 KB |
Output is correct |
31 |
Correct |
33 ms |
84044 KB |
Output is correct |
32 |
Correct |
33 ms |
84044 KB |
Output is correct |
33 |
Correct |
32 ms |
83964 KB |
Output is correct |
34 |
Correct |
34 ms |
84060 KB |
Output is correct |
35 |
Correct |
32 ms |
83988 KB |
Output is correct |
36 |
Correct |
33 ms |
84068 KB |
Output is correct |
37 |
Correct |
36 ms |
84096 KB |
Output is correct |
38 |
Correct |
34 ms |
84040 KB |
Output is correct |
39 |
Correct |
33 ms |
84020 KB |
Output is correct |
40 |
Correct |
32 ms |
84016 KB |
Output is correct |
41 |
Correct |
33 ms |
83996 KB |
Output is correct |
42 |
Correct |
35 ms |
84076 KB |
Output is correct |
43 |
Correct |
33 ms |
84040 KB |
Output is correct |
44 |
Correct |
37 ms |
84048 KB |
Output is correct |
45 |
Correct |
32 ms |
84052 KB |
Output is correct |
46 |
Correct |
36 ms |
83996 KB |
Output is correct |
47 |
Correct |
33 ms |
84040 KB |
Output is correct |
48 |
Correct |
33 ms |
84044 KB |
Output is correct |
49 |
Correct |
33 ms |
84020 KB |
Output is correct |
50 |
Correct |
34 ms |
84052 KB |
Output is correct |
51 |
Correct |
33 ms |
84072 KB |
Output is correct |
52 |
Correct |
32 ms |
84040 KB |
Output is correct |
53 |
Correct |
35 ms |
84032 KB |
Output is correct |
54 |
Correct |
33 ms |
84044 KB |
Output is correct |
55 |
Correct |
34 ms |
83952 KB |
Output is correct |
56 |
Correct |
32 ms |
84052 KB |
Output is correct |
57 |
Correct |
32 ms |
84008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
84052 KB |
Output is correct |
2 |
Correct |
32 ms |
84048 KB |
Output is correct |
3 |
Correct |
35 ms |
84044 KB |
Output is correct |
4 |
Correct |
33 ms |
84016 KB |
Output is correct |
5 |
Correct |
33 ms |
84060 KB |
Output is correct |
6 |
Correct |
34 ms |
84052 KB |
Output is correct |
7 |
Correct |
33 ms |
84052 KB |
Output is correct |
8 |
Correct |
32 ms |
84020 KB |
Output is correct |
9 |
Correct |
33 ms |
84000 KB |
Output is correct |
10 |
Correct |
33 ms |
84040 KB |
Output is correct |
11 |
Correct |
32 ms |
84072 KB |
Output is correct |
12 |
Correct |
32 ms |
84044 KB |
Output is correct |
13 |
Correct |
34 ms |
84048 KB |
Output is correct |
14 |
Correct |
32 ms |
83996 KB |
Output is correct |
15 |
Correct |
34 ms |
84076 KB |
Output is correct |
16 |
Correct |
34 ms |
83996 KB |
Output is correct |
17 |
Correct |
36 ms |
84044 KB |
Output is correct |
18 |
Correct |
35 ms |
83984 KB |
Output is correct |
19 |
Correct |
34 ms |
84172 KB |
Output is correct |
20 |
Correct |
34 ms |
84012 KB |
Output is correct |
21 |
Correct |
32 ms |
84020 KB |
Output is correct |
22 |
Correct |
33 ms |
84064 KB |
Output is correct |
23 |
Correct |
35 ms |
83988 KB |
Output is correct |
24 |
Correct |
35 ms |
84040 KB |
Output is correct |
25 |
Correct |
32 ms |
83964 KB |
Output is correct |
26 |
Correct |
33 ms |
84044 KB |
Output is correct |
27 |
Correct |
37 ms |
84028 KB |
Output is correct |
28 |
Correct |
33 ms |
84044 KB |
Output is correct |
29 |
Correct |
37 ms |
84068 KB |
Output is correct |
30 |
Correct |
33 ms |
84004 KB |
Output is correct |
31 |
Correct |
33 ms |
84044 KB |
Output is correct |
32 |
Correct |
33 ms |
84044 KB |
Output is correct |
33 |
Correct |
32 ms |
83964 KB |
Output is correct |
34 |
Correct |
34 ms |
84060 KB |
Output is correct |
35 |
Correct |
32 ms |
83988 KB |
Output is correct |
36 |
Correct |
33 ms |
84068 KB |
Output is correct |
37 |
Correct |
36 ms |
84096 KB |
Output is correct |
38 |
Correct |
34 ms |
84040 KB |
Output is correct |
39 |
Correct |
33 ms |
84020 KB |
Output is correct |
40 |
Correct |
32 ms |
84016 KB |
Output is correct |
41 |
Correct |
33 ms |
83996 KB |
Output is correct |
42 |
Correct |
35 ms |
84076 KB |
Output is correct |
43 |
Correct |
33 ms |
84040 KB |
Output is correct |
44 |
Correct |
37 ms |
84048 KB |
Output is correct |
45 |
Correct |
32 ms |
84052 KB |
Output is correct |
46 |
Correct |
36 ms |
83996 KB |
Output is correct |
47 |
Correct |
33 ms |
84040 KB |
Output is correct |
48 |
Correct |
33 ms |
84044 KB |
Output is correct |
49 |
Correct |
33 ms |
84020 KB |
Output is correct |
50 |
Correct |
34 ms |
84052 KB |
Output is correct |
51 |
Correct |
33 ms |
84072 KB |
Output is correct |
52 |
Correct |
32 ms |
84040 KB |
Output is correct |
53 |
Correct |
35 ms |
84032 KB |
Output is correct |
54 |
Correct |
33 ms |
84044 KB |
Output is correct |
55 |
Correct |
34 ms |
83952 KB |
Output is correct |
56 |
Correct |
32 ms |
84052 KB |
Output is correct |
57 |
Correct |
32 ms |
84008 KB |
Output is correct |
58 |
Correct |
48 ms |
84220 KB |
Output is correct |
59 |
Correct |
38 ms |
84668 KB |
Output is correct |
60 |
Correct |
62 ms |
84724 KB |
Output is correct |
61 |
Correct |
320 ms |
84664 KB |
Output is correct |
62 |
Correct |
959 ms |
85556 KB |
Output is correct |
63 |
Correct |
71 ms |
85596 KB |
Output is correct |
64 |
Correct |
461 ms |
85620 KB |
Output is correct |
65 |
Correct |
34 ms |
84052 KB |
Output is correct |
66 |
Correct |
33 ms |
84040 KB |
Output is correct |
67 |
Correct |
34 ms |
84044 KB |
Output is correct |
68 |
Correct |
32 ms |
84056 KB |
Output is correct |
69 |
Correct |
36 ms |
84048 KB |
Output is correct |
70 |
Correct |
33 ms |
84036 KB |
Output is correct |
71 |
Correct |
51 ms |
84160 KB |
Output is correct |
72 |
Correct |
36 ms |
84104 KB |
Output is correct |
73 |
Correct |
41 ms |
84108 KB |
Output is correct |
74 |
Correct |
39 ms |
84724 KB |
Output is correct |
75 |
Correct |
49 ms |
84664 KB |
Output is correct |
76 |
Correct |
107 ms |
84700 KB |
Output is correct |
77 |
Correct |
261 ms |
85512 KB |
Output is correct |
78 |
Correct |
56 ms |
85456 KB |
Output is correct |
79 |
Correct |
146 ms |
85476 KB |
Output is correct |
80 |
Correct |
48 ms |
85460 KB |
Output is correct |
81 |
Correct |
75 ms |
85500 KB |
Output is correct |
82 |
Correct |
260 ms |
85472 KB |
Output is correct |
83 |
Correct |
31 ms |
84044 KB |
Output is correct |
84 |
Correct |
34 ms |
84024 KB |
Output is correct |
85 |
Correct |
55 ms |
84296 KB |
Output is correct |
86 |
Correct |
36 ms |
84180 KB |
Output is correct |
87 |
Correct |
37 ms |
84232 KB |
Output is correct |
88 |
Correct |
37 ms |
84696 KB |
Output is correct |
89 |
Correct |
46 ms |
84684 KB |
Output is correct |
90 |
Correct |
100 ms |
84792 KB |
Output is correct |
91 |
Correct |
243 ms |
85800 KB |
Output is correct |
92 |
Correct |
57 ms |
85580 KB |
Output is correct |
93 |
Correct |
131 ms |
85568 KB |
Output is correct |
94 |
Correct |
34 ms |
84204 KB |
Output is correct |
95 |
Correct |
87 ms |
84176 KB |
Output is correct |
96 |
Correct |
32 ms |
84044 KB |
Output is correct |
97 |
Correct |
32 ms |
84044 KB |
Output is correct |
98 |
Correct |
31 ms |
83968 KB |
Output is correct |
99 |
Correct |
34 ms |
83952 KB |
Output is correct |