#include <cstdio>
#include <cstring>
#include <deque>
#include <algorithm>
using namespace std;
int dp[105][100005];
int a[100005];
int n,k;
int main(){
scanf("%d %d",&n,&k);
int maxv = 0;
memset(dp,0x3f,sizeof(dp));
for (int i=1; i<=n; i++) {
scanf("%d",&a[i]);
maxv = max(maxv,a[i]);
dp[1][i] = maxv;
}
for (int i=2; i<=k; i++) {
deque<int> dq,dq2;
for (int j=1; j<=n; j++) {
int my_dp = dp[i-1][j-1];
while (!dq.empty() && dq.back() < a[j]) {
my_dp = min(my_dp,dq2.back());
dq.pop_back();
dq2.pop_back();
}
if(dq.empty() || my_dp + a[j] < dq.back() + dq2.back()){
dq.push_back(a[j]);
dq2.push_back(my_dp);
}
if(j >= i) dp[i][j] = dq.back() + dq2.back();
}
}
printf("%d",dp[k][n]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
42988 KB |
Output is correct |
2 |
Correct |
0 ms |
42988 KB |
Output is correct |
3 |
Correct |
0 ms |
42988 KB |
Output is correct |
4 |
Correct |
0 ms |
42988 KB |
Output is correct |
5 |
Correct |
4 ms |
42988 KB |
Output is correct |
6 |
Correct |
4 ms |
42988 KB |
Output is correct |
7 |
Correct |
4 ms |
42988 KB |
Output is correct |
8 |
Correct |
0 ms |
42988 KB |
Output is correct |
9 |
Correct |
0 ms |
42988 KB |
Output is correct |
10 |
Correct |
5 ms |
42988 KB |
Output is correct |
11 |
Correct |
3 ms |
42988 KB |
Output is correct |
12 |
Correct |
0 ms |
42988 KB |
Output is correct |
13 |
Correct |
4 ms |
42988 KB |
Output is correct |
14 |
Correct |
4 ms |
42988 KB |
Output is correct |
15 |
Correct |
0 ms |
42988 KB |
Output is correct |
16 |
Correct |
0 ms |
42988 KB |
Output is correct |
17 |
Correct |
5 ms |
42988 KB |
Output is correct |
18 |
Correct |
0 ms |
42988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
42988 KB |
Output is correct |
2 |
Correct |
0 ms |
42988 KB |
Output is correct |
3 |
Correct |
3 ms |
42988 KB |
Output is correct |
4 |
Correct |
4 ms |
42988 KB |
Output is correct |
5 |
Correct |
3 ms |
42988 KB |
Output is correct |
6 |
Correct |
4 ms |
42988 KB |
Output is correct |
7 |
Correct |
4 ms |
42988 KB |
Output is correct |
8 |
Correct |
0 ms |
42988 KB |
Output is correct |
9 |
Correct |
0 ms |
42988 KB |
Output is correct |
10 |
Correct |
0 ms |
42988 KB |
Output is correct |
11 |
Correct |
4 ms |
42988 KB |
Output is correct |
12 |
Correct |
4 ms |
42988 KB |
Output is correct |
13 |
Correct |
4 ms |
42988 KB |
Output is correct |
14 |
Correct |
4 ms |
42988 KB |
Output is correct |
15 |
Correct |
0 ms |
42988 KB |
Output is correct |
16 |
Correct |
4 ms |
42988 KB |
Output is correct |
17 |
Correct |
5 ms |
42988 KB |
Output is correct |
18 |
Correct |
0 ms |
42988 KB |
Output is correct |
19 |
Correct |
0 ms |
42988 KB |
Output is correct |
20 |
Correct |
0 ms |
42988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
42988 KB |
Output is correct |
2 |
Correct |
0 ms |
42988 KB |
Output is correct |
3 |
Correct |
4 ms |
42988 KB |
Output is correct |
4 |
Correct |
4 ms |
42988 KB |
Output is correct |
5 |
Correct |
4 ms |
42988 KB |
Output is correct |
6 |
Correct |
4 ms |
42988 KB |
Output is correct |
7 |
Correct |
0 ms |
42988 KB |
Output is correct |
8 |
Correct |
5 ms |
42988 KB |
Output is correct |
9 |
Correct |
0 ms |
42988 KB |
Output is correct |
10 |
Correct |
4 ms |
42988 KB |
Output is correct |
11 |
Correct |
6 ms |
42988 KB |
Output is correct |
12 |
Correct |
0 ms |
42988 KB |
Output is correct |
13 |
Correct |
4 ms |
42988 KB |
Output is correct |
14 |
Correct |
0 ms |
42988 KB |
Output is correct |
15 |
Correct |
0 ms |
42988 KB |
Output is correct |
16 |
Correct |
0 ms |
42988 KB |
Output is correct |
17 |
Correct |
3 ms |
42988 KB |
Output is correct |
18 |
Correct |
5 ms |
42988 KB |
Output is correct |
19 |
Correct |
0 ms |
42988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
42988 KB |
Output is correct |
2 |
Correct |
11 ms |
42988 KB |
Output is correct |
3 |
Correct |
7 ms |
42988 KB |
Output is correct |
4 |
Correct |
51 ms |
42988 KB |
Output is correct |
5 |
Correct |
149 ms |
42988 KB |
Output is correct |
6 |
Correct |
21 ms |
42988 KB |
Output is correct |
7 |
Correct |
72 ms |
42988 KB |
Output is correct |
8 |
Correct |
4 ms |
42988 KB |
Output is correct |
9 |
Correct |
0 ms |
42988 KB |
Output is correct |
10 |
Correct |
0 ms |
42988 KB |
Output is correct |
11 |
Correct |
0 ms |
42988 KB |
Output is correct |
12 |
Correct |
3 ms |
42988 KB |
Output is correct |
13 |
Correct |
5 ms |
42988 KB |
Output is correct |
14 |
Correct |
20 ms |
42988 KB |
Output is correct |
15 |
Correct |
3 ms |
42988 KB |
Output is correct |
16 |
Correct |
7 ms |
42988 KB |
Output is correct |
17 |
Correct |
8 ms |
42988 KB |
Output is correct |
18 |
Correct |
17 ms |
42988 KB |
Output is correct |
19 |
Correct |
56 ms |
42988 KB |
Output is correct |
20 |
Correct |
143 ms |
42988 KB |
Output is correct |
21 |
Correct |
17 ms |
42988 KB |
Output is correct |
22 |
Correct |
72 ms |
42988 KB |
Output is correct |
23 |
Correct |
14 ms |
42988 KB |
Output is correct |
24 |
Correct |
31 ms |
42988 KB |
Output is correct |
25 |
Correct |
147 ms |
42988 KB |
Output is correct |
26 |
Correct |
6 ms |
42988 KB |
Output is correct |
27 |
Correct |
5 ms |
42988 KB |
Output is correct |
28 |
Correct |
22 ms |
42988 KB |
Output is correct |
29 |
Correct |
3 ms |
42988 KB |
Output is correct |
30 |
Correct |
12 ms |
42988 KB |
Output is correct |
31 |
Correct |
3 ms |
42988 KB |
Output is correct |
32 |
Correct |
7 ms |
43116 KB |
Output is correct |
33 |
Correct |
76 ms |
43116 KB |
Output is correct |
34 |
Correct |
256 ms |
43248 KB |
Output is correct |
35 |
Correct |
21 ms |
43248 KB |
Output is correct |
36 |
Correct |
112 ms |
43248 KB |
Output is correct |
37 |
Correct |
3 ms |
42988 KB |
Output is correct |
38 |
Correct |
18 ms |
42988 KB |
Output is correct |
39 |
Correct |
3 ms |
42988 KB |
Output is correct |
40 |
Correct |
3 ms |
42988 KB |
Output is correct |
41 |
Correct |
0 ms |
42988 KB |
Output is correct |
42 |
Correct |
5 ms |
42988 KB |
Output is correct |