#include <cstdio>
#include <cstring>
#include <stack>
#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++) {
stack<int> s1,s2;
for (int j=1; j<=n; j++) {
int my_dp = dp[i-1][j-1];
while (!s1.empty() && s1.top() < a[j]) {
my_dp = min(my_dp,s2.top());
s1.pop();
s2.pop();
}
if(s1.empty() || my_dp + a[j] < s1.top() + s2.top()){
s1.push(a[j]);
s2.push(my_dp);
}
if(j >= i) dp[i][j] = s1.top() + s2.top();
}
}
printf("%d",dp[k][n]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
42988 KB |
Output is correct |
2 |
Correct |
4 ms |
42988 KB |
Output is correct |
3 |
Correct |
3 ms |
42988 KB |
Output is correct |
4 |
Correct |
3 ms |
42988 KB |
Output is correct |
5 |
Correct |
0 ms |
42988 KB |
Output is correct |
6 |
Correct |
4 ms |
42988 KB |
Output is correct |
7 |
Correct |
3 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 |
3 ms |
42988 KB |
Output is correct |
11 |
Correct |
4 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 |
3 ms |
42988 KB |
Output is correct |
16 |
Correct |
9 ms |
42988 KB |
Output is correct |
17 |
Correct |
0 ms |
42988 KB |
Output is correct |
18 |
Correct |
4 ms |
42988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
42988 KB |
Output is correct |
2 |
Correct |
3 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 |
3 ms |
42988 KB |
Output is correct |
6 |
Correct |
0 ms |
42988 KB |
Output is correct |
7 |
Correct |
6 ms |
42988 KB |
Output is correct |
8 |
Correct |
0 ms |
42988 KB |
Output is correct |
9 |
Correct |
3 ms |
42988 KB |
Output is correct |
10 |
Correct |
0 ms |
42988 KB |
Output is correct |
11 |
Correct |
3 ms |
42988 KB |
Output is correct |
12 |
Correct |
9 ms |
42988 KB |
Output is correct |
13 |
Correct |
4 ms |
42988 KB |
Output is correct |
14 |
Correct |
3 ms |
42988 KB |
Output is correct |
15 |
Correct |
3 ms |
42988 KB |
Output is correct |
16 |
Correct |
4 ms |
42988 KB |
Output is correct |
17 |
Correct |
6 ms |
42988 KB |
Output is correct |
18 |
Correct |
3 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 |
0 ms |
42988 KB |
Output is correct |
4 |
Correct |
0 ms |
42988 KB |
Output is correct |
5 |
Correct |
3 ms |
42988 KB |
Output is correct |
6 |
Correct |
0 ms |
42988 KB |
Output is correct |
7 |
Correct |
3 ms |
42988 KB |
Output is correct |
8 |
Correct |
4 ms |
42988 KB |
Output is correct |
9 |
Correct |
4 ms |
42988 KB |
Output is correct |
10 |
Correct |
3 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 |
0 ms |
42988 KB |
Output is correct |
14 |
Correct |
4 ms |
42988 KB |
Output is correct |
15 |
Correct |
5 ms |
42988 KB |
Output is correct |
16 |
Correct |
0 ms |
42988 KB |
Output is correct |
17 |
Correct |
0 ms |
42988 KB |
Output is correct |
18 |
Correct |
0 ms |
42988 KB |
Output is correct |
19 |
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 |
14 ms |
42988 KB |
Output is correct |
4 |
Correct |
50 ms |
42988 KB |
Output is correct |
5 |
Correct |
142 ms |
42988 KB |
Output is correct |
6 |
Correct |
29 ms |
42988 KB |
Output is correct |
7 |
Correct |
88 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 |
0 ms |
42988 KB |
Output is correct |
11 |
Correct |
5 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 |
18 ms |
42988 KB |
Output is correct |
15 |
Correct |
7 ms |
42988 KB |
Output is correct |
16 |
Correct |
9 ms |
42988 KB |
Output is correct |
17 |
Correct |
7 ms |
42988 KB |
Output is correct |
18 |
Correct |
16 ms |
42988 KB |
Output is correct |
19 |
Correct |
44 ms |
42988 KB |
Output is correct |
20 |
Correct |
139 ms |
42988 KB |
Output is correct |
21 |
Correct |
23 ms |
42988 KB |
Output is correct |
22 |
Correct |
81 ms |
42988 KB |
Output is correct |
23 |
Correct |
21 ms |
42988 KB |
Output is correct |
24 |
Correct |
32 ms |
42988 KB |
Output is correct |
25 |
Correct |
152 ms |
42988 KB |
Output is correct |
26 |
Correct |
0 ms |
42988 KB |
Output is correct |
27 |
Correct |
0 ms |
42988 KB |
Output is correct |
28 |
Correct |
22 ms |
42988 KB |
Output is correct |
29 |
Correct |
7 ms |
42988 KB |
Output is correct |
30 |
Correct |
7 ms |
42988 KB |
Output is correct |
31 |
Correct |
6 ms |
42988 KB |
Output is correct |
32 |
Correct |
19 ms |
43116 KB |
Output is correct |
33 |
Correct |
77 ms |
43116 KB |
Output is correct |
34 |
Correct |
227 ms |
43248 KB |
Output is correct |
35 |
Correct |
21 ms |
43248 KB |
Output is correct |
36 |
Correct |
103 ms |
43248 KB |
Output is correct |
37 |
Correct |
7 ms |
42988 KB |
Output is correct |
38 |
Correct |
13 ms |
42988 KB |
Output is correct |
39 |
Correct |
0 ms |
42988 KB |
Output is correct |
40 |
Correct |
5 ms |
42988 KB |
Output is correct |
41 |
Correct |
5 ms |
42988 KB |
Output is correct |
42 |
Correct |
0 ms |
42988 KB |
Output is correct |