0-1背包+动态规划
关键是了解状态方程。用图来推导,
自己弄不出来的话可以参考地址:http://blog.sina.com.cn/s/blog_611253210100dw6b.html
#include<iostream>
#include<stdio.h>
using namespace std;
int t,v;
int result[1002] = {0};
int main()
{
int time,n;
while(scanf("%d%d",&time,&n) != EOF)
{
for(int i=0;i<n;++i)
{
scanf("%d%d",&t,&v);
for(int j=time;j>=t;--j)
{
result[j] = ((result[j] > (result[j-t] + v) ) ? result[j] :(result[j-t] + v));
}
}
printf("%d\n",result[time]);
}
return 0;
}