先上题目
Time Limit: 5000/1000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 8404 Accepted
Submission(s): 2861
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define MAX 1001 5 #define max(x,y) (x >= y ? x : y) 6 using namespace std; 7 8 int dp[MAX],c[MAX],n,m; 9 10 11 int main() 12 { 13 //freopen("data.txt","r",stdin); 14 while(scanf("%d",&n),n){ 15 for(int i=1;i<=n;i++) scanf("%d",&c[i]); 16 sort(c+1,c+n+1); 17 scanf("%d",&m); 18 19 if(m<5){ 20 printf("%d\n",m); 21 continue; 22 } 23 24 for(int i=0;i<=m;i++) dp[i]=0; 25 for(int i=1;i<n;i++){ 26 for(int j=m;j>=c[i];j--){ 27 dp[j]=max(dp[j],dp[j-c[i]]+c[i]); 28 //printf("%d ",dp[j]); 29 } 30 //printf("\n"); 31 } 32 printf("%d\n",m-dp[m-5]-c[n]); 33 } 34 return 0; 35 }
原文:http://www.cnblogs.com/sineatos/p/3529064.html