#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
#define pi3 pair<pii, int>
#define MP3(a,b,c) MP(MP(a, b), c)
using namespace std;
const int oo = 1e9;
const int N = 100100;
const int K = 110;
const int PW = 20;
stack<pi3> st;
int f[N][K], a[N], n, k;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
a[0] = oo;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++)
f[i][j] = oo;
f[0][0] = 0;
for (int j = 1; j <= k; j++) {
while (sz(st)) st.pop();
st.push(MP3(0, oo, oo));
for (int i = 1; i <= n; i++){
pi3 nw = MP3(i, f[i - 1][j - 1], 0);
while (a[st.top().ft.ft] <= a[i]) {
nw.ft.sd = min(nw.ft.sd, st.top().ft.sd);
st.pop();
}
nw.sd = min(st.top().sd, nw.ft.sd + a[i]);
st.push(nw);
f[i][j] = nw.sd;
}
}
cout << f[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
420 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
380 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
404 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4728 KB |
Output is correct |
2 |
Correct |
22 ms |
19576 KB |
Output is correct |
3 |
Correct |
27 ms |
19832 KB |
Output is correct |
4 |
Correct |
78 ms |
19692 KB |
Output is correct |
5 |
Correct |
217 ms |
44024 KB |
Output is correct |
6 |
Correct |
54 ms |
44536 KB |
Output is correct |
7 |
Correct |
127 ms |
44636 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
760 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
23 ms |
4728 KB |
Output is correct |
15 |
Correct |
7 ms |
4728 KB |
Output is correct |
16 |
Correct |
10 ms |
4728 KB |
Output is correct |
17 |
Correct |
23 ms |
19932 KB |
Output is correct |
18 |
Correct |
27 ms |
19960 KB |
Output is correct |
19 |
Correct |
83 ms |
20132 KB |
Output is correct |
20 |
Correct |
247 ms |
44664 KB |
Output is correct |
21 |
Correct |
53 ms |
44536 KB |
Output is correct |
22 |
Correct |
133 ms |
44592 KB |
Output is correct |
23 |
Correct |
50 ms |
44408 KB |
Output is correct |
24 |
Correct |
70 ms |
44408 KB |
Output is correct |
25 |
Correct |
239 ms |
44536 KB |
Output is correct |
26 |
Correct |
2 ms |
760 KB |
Output is correct |
27 |
Correct |
2 ms |
760 KB |
Output is correct |
28 |
Correct |
22 ms |
4856 KB |
Output is correct |
29 |
Correct |
7 ms |
4828 KB |
Output is correct |
30 |
Correct |
11 ms |
4748 KB |
Output is correct |
31 |
Correct |
24 ms |
20088 KB |
Output is correct |
32 |
Correct |
28 ms |
20216 KB |
Output is correct |
33 |
Correct |
87 ms |
20216 KB |
Output is correct |
34 |
Correct |
261 ms |
44592 KB |
Output is correct |
35 |
Correct |
56 ms |
45036 KB |
Output is correct |
36 |
Correct |
147 ms |
45136 KB |
Output is correct |
37 |
Correct |
7 ms |
4728 KB |
Output is correct |
38 |
Correct |
19 ms |
4728 KB |
Output is correct |
39 |
Correct |
2 ms |
760 KB |
Output is correct |
40 |
Correct |
2 ms |
760 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |