题目连接 http://poj.org/problem?id=3624
View Code
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int n,i,j,w[3500],val[3500],v,f[20000]; 6 while(~scanf("%d %d",&n,&v)) 7 { 8 9 for(i = 1;i <= n;i++) 10 { 11 scanf("%d %d",&w[i],&val[i]); 12 } 13 memset(f,0,sizeof(f)); 14 for(i = 1;i<= n;i++) 15 for(j = v;j>=w[i];j--) 16 if(f[j] < f[j-w[i]]+val[i]) 17 f[j] = f[j-w[i]]+val[i]; 18 19 printf("%d\n",f[v]); 20 21 } 22 return 0; 23 }