#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f[100009],dp[100009][109],x[100009][109],rm[100009][19],ka[100009],zx,g;
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(c=1; c<=a; c++) cin>>f[c];
for(c=1; c<=a; c++) rm[c][0]=f[c];
for(c=1; c<=a; c++){
for(d=1; d<=18; d++){
if((1<<d)+c-1>a) break;
rm[c][d]=max(rm[c][d-1],rm[c+(1<<(d-1))][d-1]);
}
}
ka[0]=0;
for(c=1; c<=a; c++){
if(ka[c-1]*2<c) ka[c]=ka[c-1]+1; else ka[c]=ka[c-1];
}
for(c=b+1; c<=a; c++) x[c][b+1]=c-1;
for(c=0; c<=a+1; c++){
for(d=0; d<=b+1; d++) dp[c][d]=999999999;
}
dp[0][0]=0;
for(c=1; c<=a; c++){
dp[c][c]=dp[c-1][c-1]+f[c];
x[c][c]=c-1;
}
for(c=2; c<=a; c++){
for(d=min(b,c-1); d>=1; d--){
e=x[c-1][d];g=e-c;
zx=max(rm[e+1][ka[g]],rm[c-(1<<ka[g])+1][ka[g]]);
for( ; e<=x[c][d+1]; e++){
if(dp[c][d]>dp[e][d-1]+zx) dp[c][d]=dp[e][d-1]+zx;
if(zx<f[e]) zx=e;
}
}
}
/*for(c=1; c<=a; c++){
for(d=1; d<=min(c,b); d++){
cout<<dp[c][d]<<","<<x[c][d]<<" ";
}
cout<<endl;
}*/
cout<<dp[a][b];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
388 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
8 ms |
764 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
29 ms |
19704 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |