#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define inf 500000000
int n,k;
int su[100002];
int dp[100002][101];
int dpp[100002][101];
vector<int> st;
int stp;
int main(){
int i,j;
scanf("%d %d",&n,&k);
for(i=1;i<=n;i++){
scanf("%d",&su[i]);
}
su[0]=inf;
for(i=0;i<=n;i++){
for(j=0;j<=k;j++){
dp[i][j]=inf;
dpp[i][j]=inf;
}
}
st.push_back(0);
stp=1;
for(i=1;i<=n;i++){
while(stp>0){
if(su[st[stp-1]]>su[i]) break;
for(j=0;j<=k;j++){
dp[st[stp-2]][j]=min(dp[st[stp-2]][j],dp[st[stp-1]][j]);
if(stp>=3){
dpp[st[stp-2]][j]=min(dpp[st[stp-3]][j],dp[st[stp-3]][j]+su[st[stp-2]]);
}else{
dpp[st[stp-2]][j]=inf;
}
}
st.pop_back();
stp--;
}
st.push_back(i);
stp++;
dp[i][1]=su[st[1]];
for(j=1;j<=k-1;j++){
dpp[i][j]=min(dpp[st[stp-2]][j],dp[st[stp-2]][j]+su[i]);
dp[i][j+1]=dpp[i][j];
}
}
printf("%d",dp[n][k]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
80508 KB |
Output is correct |
2 |
Correct |
0 ms |
80508 KB |
Output is correct |
3 |
Correct |
0 ms |
80508 KB |
Output is correct |
4 |
Correct |
0 ms |
80508 KB |
Output is correct |
5 |
Correct |
0 ms |
80508 KB |
Output is correct |
6 |
Correct |
0 ms |
80508 KB |
Output is correct |
7 |
Correct |
0 ms |
80508 KB |
Output is correct |
8 |
Correct |
0 ms |
80508 KB |
Output is correct |
9 |
Correct |
0 ms |
80508 KB |
Output is correct |
10 |
Correct |
0 ms |
80508 KB |
Output is correct |
11 |
Correct |
0 ms |
80508 KB |
Output is correct |
12 |
Correct |
0 ms |
80508 KB |
Output is correct |
13 |
Correct |
0 ms |
80508 KB |
Output is correct |
14 |
Correct |
0 ms |
80508 KB |
Output is correct |
15 |
Correct |
0 ms |
80508 KB |
Output is correct |
16 |
Correct |
0 ms |
80508 KB |
Output is correct |
17 |
Correct |
0 ms |
80508 KB |
Output is correct |
18 |
Correct |
0 ms |
80508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
80508 KB |
Output is correct |
2 |
Correct |
0 ms |
80508 KB |
Output is correct |
3 |
Correct |
0 ms |
80508 KB |
Output is correct |
4 |
Correct |
0 ms |
80508 KB |
Output is correct |
5 |
Correct |
0 ms |
80508 KB |
Output is correct |
6 |
Correct |
0 ms |
80508 KB |
Output is correct |
7 |
Correct |
0 ms |
80508 KB |
Output is correct |
8 |
Correct |
0 ms |
80508 KB |
Output is correct |
9 |
Correct |
0 ms |
80508 KB |
Output is correct |
10 |
Correct |
0 ms |
80508 KB |
Output is correct |
11 |
Correct |
0 ms |
80508 KB |
Output is correct |
12 |
Correct |
0 ms |
80508 KB |
Output is correct |
13 |
Correct |
0 ms |
80508 KB |
Output is correct |
14 |
Correct |
0 ms |
80508 KB |
Output is correct |
15 |
Correct |
0 ms |
80508 KB |
Output is correct |
16 |
Correct |
0 ms |
80508 KB |
Output is correct |
17 |
Correct |
0 ms |
80508 KB |
Output is correct |
18 |
Correct |
0 ms |
80508 KB |
Output is correct |
19 |
Correct |
0 ms |
80508 KB |
Output is correct |
20 |
Correct |
0 ms |
80508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
80508 KB |
Output is correct |
2 |
Correct |
0 ms |
80508 KB |
Output is correct |
3 |
Correct |
0 ms |
80508 KB |
Output is correct |
4 |
Correct |
0 ms |
80508 KB |
Output is correct |
5 |
Correct |
0 ms |
80508 KB |
Output is correct |
6 |
Correct |
0 ms |
80508 KB |
Output is correct |
7 |
Correct |
0 ms |
80508 KB |
Output is correct |
8 |
Correct |
0 ms |
80508 KB |
Output is correct |
9 |
Correct |
0 ms |
80508 KB |
Output is correct |
10 |
Correct |
0 ms |
80508 KB |
Output is correct |
11 |
Correct |
0 ms |
80508 KB |
Output is correct |
12 |
Correct |
0 ms |
80508 KB |
Output is correct |
13 |
Correct |
0 ms |
80508 KB |
Output is correct |
14 |
Correct |
0 ms |
80508 KB |
Output is correct |
15 |
Correct |
0 ms |
80508 KB |
Output is correct |
16 |
Correct |
0 ms |
80508 KB |
Output is correct |
17 |
Correct |
0 ms |
80508 KB |
Output is correct |
18 |
Correct |
0 ms |
80508 KB |
Output is correct |
19 |
Correct |
0 ms |
80508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
80508 KB |
Output is correct |
2 |
Correct |
7 ms |
80508 KB |
Output is correct |
3 |
Correct |
14 ms |
80508 KB |
Output is correct |
4 |
Correct |
46 ms |
80508 KB |
Output is correct |
5 |
Correct |
123 ms |
80508 KB |
Output is correct |
6 |
Correct |
26 ms |
80508 KB |
Output is correct |
7 |
Correct |
77 ms |
80508 KB |
Output is correct |
8 |
Correct |
0 ms |
80508 KB |
Output is correct |
9 |
Correct |
0 ms |
80508 KB |
Output is correct |
10 |
Correct |
0 ms |
80508 KB |
Output is correct |
11 |
Correct |
0 ms |
80508 KB |
Output is correct |
12 |
Correct |
0 ms |
80508 KB |
Output is correct |
13 |
Correct |
0 ms |
80508 KB |
Output is correct |
14 |
Correct |
9 ms |
80508 KB |
Output is correct |
15 |
Correct |
5 ms |
80508 KB |
Output is correct |
16 |
Correct |
6 ms |
80508 KB |
Output is correct |
17 |
Correct |
13 ms |
80508 KB |
Output is correct |
18 |
Correct |
5 ms |
80508 KB |
Output is correct |
19 |
Correct |
44 ms |
80508 KB |
Output is correct |
20 |
Correct |
133 ms |
80508 KB |
Output is correct |
21 |
Correct |
26 ms |
80508 KB |
Output is correct |
22 |
Correct |
72 ms |
80508 KB |
Output is correct |
23 |
Correct |
24 ms |
80508 KB |
Output is correct |
24 |
Correct |
30 ms |
80508 KB |
Output is correct |
25 |
Correct |
137 ms |
80508 KB |
Output is correct |
26 |
Correct |
0 ms |
80508 KB |
Output is correct |
27 |
Correct |
0 ms |
80508 KB |
Output is correct |
28 |
Correct |
9 ms |
80508 KB |
Output is correct |
29 |
Correct |
0 ms |
80508 KB |
Output is correct |
30 |
Correct |
6 ms |
80508 KB |
Output is correct |
31 |
Correct |
8 ms |
80508 KB |
Output is correct |
32 |
Correct |
10 ms |
80508 KB |
Output is correct |
33 |
Correct |
41 ms |
80508 KB |
Output is correct |
34 |
Correct |
103 ms |
80896 KB |
Output is correct |
35 |
Correct |
7 ms |
80896 KB |
Output is correct |
36 |
Correct |
59 ms |
80896 KB |
Output is correct |
37 |
Correct |
0 ms |
80508 KB |
Output is correct |
38 |
Correct |
11 ms |
80508 KB |
Output is correct |
39 |
Correct |
0 ms |
80508 KB |
Output is correct |
40 |
Correct |
0 ms |
80508 KB |
Output is correct |
41 |
Correct |
0 ms |
80508 KB |
Output is correct |
42 |
Correct |
0 ms |
80508 KB |
Output is correct |