代码实现:
# include <iostream>
using namespace std;
const int N = 1010; //由于f[i][j]中j为时间所以N为1010
int f[N];
int w[N],v[N];
int n,m;
int main()
{
scanf("%d %d",&m,&n);
for(int i = 1 ; i <= n ; i++)
{
scanf("%d %d",&w[i],&v[i]);
}
for(int i = 1 ; i <= n ; i++)
{
for(int j = m ; j >= w[i] ; j--)
{
f[j] = max(f[j] , f[j - w[i]] + v[i]);
}
}
printf("%d\n",f[m]);
return 0;
}