1 50 5 10 1 2 3 2 1 1 2 3 2 1 50 0
-45 32
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int dp[40000], w[40000], v[40000]; int n, t,m,mx,p; int main() { while (cin >> n &&n) { mx=0; memset(dp,0,sizeof(dp)); for (int i = 1; i <=n; i++) { { cin >> v[i] ; } } sort(v+1,v+n+1); cin>>m; if(m<5) { cout<<m<<endl; continue; } for (int i = 1; i <n; i++) for (int j = m-5; j >= v[i]; j--) dp[j] = max(dp[j], dp[j - v[i]] + v[i]); // for(int i=1;i<=100;i++) cout<<dp[m-5]<<endl; cout<<m-dp[m-5]-v[n]<<endl; } return 0; }
原文:http://blog.csdn.net/zhangweiacm/article/details/38336341