4 5 2 3 1 2 3 4 2 2
7
#include <ctype.h> #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 105; const int inf = 0x3f3f3f3f; int N, W, summount; int w[maxn], v[maxn]; int dp[maxn*maxn]; inline int input() { int res=0,f=1,c; while(!isdigit(c = getchar()) && c!='-'); c=='-' ? f=0 : res=c-'0'; while(isdigit(c = getchar())) res = res*10 + c-'0'; return f ? res : -res; } int main() { int n,W,sum; while(~scanf("%d%d",&n,&W)) { sum=0; for(int i=1;i<=n;i++) { //scanf("%d%d",&w[i],&v[i]); w[i]=input(); v[i]=input(); sum+=v[i]; } for(int i=0;i<=sum;i++) dp[i]=inf; dp[0]=0; for(int i=1;i<=n;i++) for(int j=sum;j>=v[i];j--) { dp[j]=min(dp[j],dp[j-v[i]]+w[i]); } for(int i=sum;i>=0;i--) if(dp[i]<=W) { printf("%d\n",i); break; } } return 0; }
原文地址:http://blog.csdn.net/u013050857/article/details/43345785