#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int n, k;
int a[100005];
int dp[100005][105];
int main(){
cin >> n >> k;
int sum=0;
for(int i=1;i<=n;i++){
cin >> a[i];
sum+=a[i];
}
memset(dp,1e9+7,sizeof(dp));
dp[0][1]=0;
for(int i=1;i<=n;i++){
dp[i][1]=max(dp[i-1][1],a[i]);
}
dp[0][1]=1e9+7;
for(int i=2;i<=k;i++){
stack<int> pval, val;
for(int j=1;j<=n;j++){
int x = dp[j-1][i-1];
while(!pval.empty()&&a[pval.top()]<a[j]){
x=min(x,val.top());
pval.pop();
val.pop();
}
if(pval.size()){
dp[j][i]=dp[pval.top()][i];
}
pval.push(j);
val.push(min(dp[j][i-1],x));
dp[j][i]=min(dp[j][i],x+a[j]);
}
}
cout << dp[n][k] << endl;
return 0;
}
// 10 3 7 9 21 3 9 10 4
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
41528 KB |
Output is correct |
2 |
Correct |
36 ms |
41464 KB |
Output is correct |
3 |
Correct |
37 ms |
41464 KB |
Output is correct |
4 |
Correct |
37 ms |
41464 KB |
Output is correct |
5 |
Correct |
36 ms |
41464 KB |
Output is correct |
6 |
Correct |
36 ms |
41464 KB |
Output is correct |
7 |
Correct |
36 ms |
41464 KB |
Output is correct |
8 |
Correct |
36 ms |
41464 KB |
Output is correct |
9 |
Correct |
36 ms |
41464 KB |
Output is correct |
10 |
Correct |
36 ms |
41464 KB |
Output is correct |
11 |
Correct |
36 ms |
41468 KB |
Output is correct |
12 |
Correct |
36 ms |
41464 KB |
Output is correct |
13 |
Correct |
36 ms |
41468 KB |
Output is correct |
14 |
Correct |
36 ms |
41464 KB |
Output is correct |
15 |
Correct |
36 ms |
41464 KB |
Output is correct |
16 |
Correct |
37 ms |
41464 KB |
Output is correct |
17 |
Correct |
32 ms |
41464 KB |
Output is correct |
18 |
Correct |
37 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
41464 KB |
Output is correct |
2 |
Correct |
36 ms |
41440 KB |
Output is correct |
3 |
Correct |
37 ms |
41384 KB |
Output is correct |
4 |
Correct |
36 ms |
41464 KB |
Output is correct |
5 |
Correct |
36 ms |
41464 KB |
Output is correct |
6 |
Correct |
36 ms |
41464 KB |
Output is correct |
7 |
Correct |
36 ms |
41464 KB |
Output is correct |
8 |
Correct |
36 ms |
41440 KB |
Output is correct |
9 |
Correct |
36 ms |
41464 KB |
Output is correct |
10 |
Correct |
36 ms |
41464 KB |
Output is correct |
11 |
Correct |
36 ms |
41512 KB |
Output is correct |
12 |
Correct |
36 ms |
41464 KB |
Output is correct |
13 |
Correct |
36 ms |
41464 KB |
Output is correct |
14 |
Correct |
36 ms |
41464 KB |
Output is correct |
15 |
Correct |
41 ms |
41464 KB |
Output is correct |
16 |
Correct |
36 ms |
41480 KB |
Output is correct |
17 |
Correct |
36 ms |
41464 KB |
Output is correct |
18 |
Correct |
36 ms |
41464 KB |
Output is correct |
19 |
Correct |
37 ms |
41456 KB |
Output is correct |
20 |
Correct |
36 ms |
41336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
41464 KB |
Output is correct |
2 |
Correct |
36 ms |
41464 KB |
Output is correct |
3 |
Correct |
41 ms |
41464 KB |
Output is correct |
4 |
Correct |
36 ms |
41464 KB |
Output is correct |
5 |
Correct |
36 ms |
41464 KB |
Output is correct |
6 |
Correct |
36 ms |
41464 KB |
Output is correct |
7 |
Correct |
36 ms |
41464 KB |
Output is correct |
8 |
Correct |
36 ms |
41464 KB |
Output is correct |
9 |
Correct |
36 ms |
41464 KB |
Output is correct |
10 |
Correct |
40 ms |
41464 KB |
Output is correct |
11 |
Correct |
36 ms |
41464 KB |
Output is correct |
12 |
Correct |
36 ms |
41464 KB |
Output is correct |
13 |
Correct |
37 ms |
41464 KB |
Output is correct |
14 |
Correct |
37 ms |
41464 KB |
Output is correct |
15 |
Correct |
37 ms |
41464 KB |
Output is correct |
16 |
Correct |
36 ms |
41360 KB |
Output is correct |
17 |
Correct |
37 ms |
41464 KB |
Output is correct |
18 |
Correct |
36 ms |
41364 KB |
Output is correct |
19 |
Correct |
36 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
41460 KB |
Output is correct |
2 |
Correct |
61 ms |
41848 KB |
Output is correct |
3 |
Correct |
71 ms |
41976 KB |
Output is correct |
4 |
Correct |
152 ms |
41836 KB |
Output is correct |
5 |
Correct |
482 ms |
42616 KB |
Output is correct |
6 |
Correct |
113 ms |
42508 KB |
Output is correct |
7 |
Correct |
280 ms |
42660 KB |
Output is correct |
8 |
Correct |
36 ms |
41464 KB |
Output is correct |
9 |
Correct |
36 ms |
41464 KB |
Output is correct |
10 |
Correct |
36 ms |
41464 KB |
Output is correct |
11 |
Correct |
36 ms |
41436 KB |
Output is correct |
12 |
Correct |
37 ms |
41464 KB |
Output is correct |
13 |
Correct |
37 ms |
41464 KB |
Output is correct |
14 |
Correct |
75 ms |
41636 KB |
Output is correct |
15 |
Correct |
42 ms |
41596 KB |
Output is correct |
16 |
Correct |
50 ms |
41592 KB |
Output is correct |
17 |
Correct |
61 ms |
41820 KB |
Output is correct |
18 |
Correct |
70 ms |
41976 KB |
Output is correct |
19 |
Correct |
175 ms |
41940 KB |
Output is correct |
20 |
Correct |
547 ms |
42616 KB |
Output is correct |
21 |
Correct |
99 ms |
42488 KB |
Output is correct |
22 |
Correct |
314 ms |
42588 KB |
Output is correct |
23 |
Correct |
94 ms |
42616 KB |
Output is correct |
24 |
Correct |
133 ms |
42488 KB |
Output is correct |
25 |
Correct |
631 ms |
42616 KB |
Output is correct |
26 |
Correct |
37 ms |
41464 KB |
Output is correct |
27 |
Correct |
38 ms |
41468 KB |
Output is correct |
28 |
Correct |
68 ms |
41592 KB |
Output is correct |
29 |
Correct |
43 ms |
41592 KB |
Output is correct |
30 |
Correct |
49 ms |
41592 KB |
Output is correct |
31 |
Correct |
62 ms |
41976 KB |
Output is correct |
32 |
Correct |
70 ms |
42124 KB |
Output is correct |
33 |
Correct |
193 ms |
42132 KB |
Output is correct |
34 |
Correct |
440 ms |
43084 KB |
Output is correct |
35 |
Correct |
100 ms |
42888 KB |
Output is correct |
36 |
Correct |
276 ms |
42932 KB |
Output is correct |
37 |
Correct |
42 ms |
41592 KB |
Output is correct |
38 |
Correct |
67 ms |
41464 KB |
Output is correct |
39 |
Correct |
38 ms |
41464 KB |
Output is correct |
40 |
Correct |
37 ms |
41464 KB |
Output is correct |
41 |
Correct |
39 ms |
41464 KB |
Output is correct |
42 |
Correct |
36 ms |
41464 KB |
Output is correct |