http://acm.hdu.edu.cn/showproblem.php?pid=2546
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12665 Accepted Submission(s): 4389
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int f[1005]; int main() { int n,v[1005],m,i,j,mp,flag; while(~scanf("%d",&n)) { if(n==0) break; mp=0; memset(f,0,sizeof(f)); for(i=1;i<=n;i++) { scanf("%d",&v[i]); if(mp<v[i]) { mp=v[i]; flag=i; } } scanf("%d",&m); if(m<5) { printf("%d\n",m); continue; } for(i=1;i<=n;i++) { if(i==flag) continue; for(j=m-5;j>=v[i];j--) { f[j]=max(f[j],f[j-v[i]]+v[i]); // printf("i=%d,j=%d\n",i,j); //printf("f=%d\n",f[j]); } } printf("%d\n",m-f[m-5]-mp); } return 0; }
原文:http://www.cnblogs.com/cancangood/p/4168136.html