模版题,分组背包。
dp[i][j]=max(dp[i][j],cost[i][k]+dp[i-1][j-k])
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn=105;
int cost[maxn][maxn];
int dp[maxn][maxn];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m) break;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
scanf("%d",&cost[i][j]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
for(int k=0;k<=j;++k)
{
dp[i][j]=max(dp[i][j],cost[i][k]+dp[i-1][j-k]);
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}