#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,dp[100009][109],f[100009],zx,xc,cv,ka[100009],rm[100009][20],vb;
int x[100009][109];
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(d=1; d<=17; d++){
for(c=1; c<=a; c++){
if(c+(1<<d)-1>a) break;
rm[c][d]=max(rm[c][d-1],rm[c+(1<<(d-1))][d-1]);
}
}
/*for(c=1; c<=a; c++){
for(d=1; d<=a; d++){
if((1<<d)+c>a) break;
cout<<c<<" "<<(1<<d)<<" "<<rm[c][d]<<endl;
}
}*/
ka[0]=0;
ka[1]=0;
ka[2]=0;
ka[3]=1;
for(d=4; d<=a; d++){
if(ka[d-1]*2<d) ka[d]=ka[d-1]+1; else ka[d]=ka[d-1];
}
for(c=1; c<=a; c++) x[c][b+1]=c-1;
for(c=1; c<=b; c++){
dp[c][c]=c*(c+1)/2;
x[c][c]=c-1;
}
for(c=1; c<=a; c++){
dp[c][1]=c;x[c][1]=0;
}
for(c=2; c<=a; c++){
for(d=min(b,c-1); d>=2; d--){
if(x[c-1][d]>=d) zx=x[c-1][d]; else zx=d-1;
if(x[c][d+1]<=c-1) xc=x[c][d+1]; else xc=c-1;
dp[c][d]=999999999999999999LL;
cv=c-xc;
/* if(c==3&&d==2){
cout<<"FDsfdsf"<<" "<<zx<<" "<<xc<<endl;
}*/
for(e=xc; e>=zx; e--){
vb=max(rm[e+1][ka[cv]],rm[c-(1<<ka[cv])+1][ka[cv]]);
if(dp[c][d]>dp[e][d-1]+vb){
dp[c][d]=dp[e][d-1]+vb;
x[c][d]=e;
}
cv++;
}
}
}
cout<<dp[a][b];
/* for(c=1; c<=a; c++){
for(d=1; d<=a; d++){
cout<<dp[c][d]<<","<<x[c][d]<<" ";
}
cout<<endl;
}*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
1020 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
42 ms |
30088 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |