Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18620 Accepted Submission(s): 6500
/* Accepted 2546 31MS 1420K 525 B G++ 1340502116 */ #include"cstdio" #include"cstring" #include"algorithm" using namespace std; const int MAXN=1005; int n,W; int w[MAXN]; int dp[MAXN]; int main() { while(scanf("%d",&n)!=EOF&&n!=0) { memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) scanf("%d",&w[i]); scanf("%d",&W); if(W<5) { printf("%d\n",W); continue; } sort(w,w+n); int V=W-5; for(int i=0;i<n-1;i++) { for(int j=V;j>=w[i];j--) dp[j]=max(dp[j],dp[j-w[i]]+w[i]); } printf("%d\n",W-dp[V]-w[n-1]); } return 0; }
原文:http://www.cnblogs.com/program-ccc/p/5186506.html