#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f[100009],i,j,dp[100009][109],msh[100009],la[100009];
deque <pair <int, int> > de[109];
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(i=1; i<=a; i++) cin>>f[i];
for(i=0; i<=a+1; i++){
for(j=0; j<=b+1; j++) dp[i][j]=999999999;
}
dp[0][1]=0;
for(i=1; i<=a; i++) dp[i][1]=max(dp[i-1][1],f[i]);
for(i=1; i<=a; i++){
while(de[0].size()!=0){
if(de[0][de[0].size()-1].second<f[i]){
de[0].pop_back();
}else{
msh[i]=de[0][de[0].size()-1].first;
break;
}
}
de[0].push_back(make_pair(i,f[i]));
}
if(b==1){
cout<<dp[a][1];
return 0;
}
for(j=2; j<=b; j++){
for(i=1; i<=a; i++) la[i]=dp[i][j-1];
for(i=2; i<=a; i++) if(la[i]>la[i-1]) la[i]=la[i-1];
for(i=j; i<=a; i++){
if(msh[i]!=0){
dp[i][j]=dp[msh[i]][j];
int zx=0,xc=0;
while(de[j].size()!=0){
if(de[j][de[j].size()-1].first>=msh[j]){
zx=de[j][de[j].size()-1].first;
xc=de[j][de[j].size()-1].second;
de[j].pop_back();
}else{
break;
}
}
if(zx!=0){
if(dp[i][j]>xc+f[i]) dp[i][j]=xc+f[i];
}
while(de[j].size()!=0){
if(de[j][de[j].size()-1].second>dp[i][j]){
de[j].pop_back();
}else{
break;
}
}
de[j].push_back(make_pair(i,dp[i][j]));
}else{
dp[i][j]=la[i-1]+f[i];
while(de[j].size()!=0){
if(de[j][de[j].size()-1].second>dp[i][j]){
de[j].pop_back();
}else{
break;
}
}
de[j].push_back(make_pair(i,dp[i][j]));
}
}
}
cout<<dp[a][b];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
4856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |