The meaning of problems
N layer of bricks is placed in a recess in the uppermost layer of oil N brick, a brick decrease from top to bottom each time. Has a score of each brick, this brick can get knocked corresponding value, as shown in FIG.
If you want to knock the first brick layer i j, then, if i = 1, you can just knock it out; if i> 1, then you will need to knock down the i-1 th layer and the j-j + 1 brick.
You can now knock up to M brick, seeking to have scored the most number.
Dp a problem, began to think of a line by line dp is found, however, choose [i, j] would choose [i-1, j + 1 ] and [i, j] all the boxes above, it seems that no after-effects are not satisfied sex, how to do it?
When we find such a file input
4 5
2 2 3 4
8 2 7
2 3
49
We can go to think is not possible an a dp, from n columns to a dp so no aftereffect, we can define the state of f [i] [j] [k] represents the current in the i-th column is selected from the j-th a total of k election, state transition equation
f[i][j][k]=max(f[i+1][t][k-j]+s[i][j],f[i][j][k])
t>=j-1&&t<=n-i
S [i] [j] represents a j-th column before the i-th and
Code
#include<bits/stdc++.h>
using namespace std;
int n,m,ans,f[55][55][3000],a[55][55],s[55][55];
int main(){
scanf("%d %d",&n,&m);
memset(f,-0x3f,sizeof(f));
f[n+1][0][0]=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n-i+1;++j){
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=n-i+1;++j){
s[j][i]=s[j][i-1]+a[i][j];
}
}
for(int i=n;i>=1;--i){
for(int j=0;j<=n-i+1;++j){
for(int k=j;k<=m;++k){
for(int t=max(j-1,0);t<=n-i;++t){
f[i][j][k]=max(f[i+1][t][k-j]+s[i][j],f[i][j][k]);
}
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=n-i+1;++j){
ans=max(ans,f[i][j][m]);
}
}
printf("%d",ans);
return 0;
}