Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = 0 and M = 0 ends the input.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[105][105],dp[105];
int main()
{
int n,m,i,j,k;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
for(j=m;j>0;j--)
{
for(k=1;k<=m;k++)
{
if(j>=k)
dp[j]=max(dp[j],dp[j-k]+a[i][k]);
}
}
}
printf("%d\n",dp[m]);
}
}