http://acm.hdu.edu.cn/showproblem.php?pid=2546
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> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 1800 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define met(a, b) memset (a, b, sizeof (a)) #define Lson rt<<1, l, tree[rt].mid() #define Rson rt<<1|1, tree[rt].mid()+1, r typedef long long LL; int dp[N]; int main () { int n, m, p[N]; while (scanf ("%d", &n), n) { for (int i=0; i<n; i++) scanf ("%d", &p[i]); scanf ("%d", &m); met (dp, 0); sort (p, p+n); if (m < 5) { printf ("%d\n", m); continue; } for (int i=0; i<n-1; i++)//循环到次大的一个 for (int j=m-5; j>=p[i]; j--)//计算出m-5块钱在n-1个物品内最多买多少钱的菜 dp[j] = max (dp[j], dp[ j-p[i] ]+p[i]); printf ("%d\n", m-dp[m-5]-p[n-1]); } return 0; }