HDU_2546_饭卡(动态规划_01背包)

#include
using namespace std;
const int maxn = 1000 + 5;
const int maxm = 1000 + 5;
int c[maxn];
int F[maxn][maxm];
int cmp(int i , int j) {
	return i > j;
}
int main() {
	int n; 
	while(cin >> n && n) {
		for(int i = 0; i < n; i++) {
			cin >> c[i];
		}
		int m;
		cin >> m; 
		sort(c, c+n, cmp);
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= m-5; j++) {
				F[i][j] = (i == 1? 0 : F[i-1][j]);
				if(j >= c[i]) F[i][j] = max(F[i][j], F[i-1][j-c[i]]+c[i]);
			}
		}
		if(m < 5) cout << m << endl;  //不细心啊   如果少于5元   就买不了东西!!! 
		else  cout << m - F[n-1][m-5] - c[0] << endl; 
	}
	return 0;
}

你可能感兴趣的:(HDU_2546_饭卡(动态规划_01背包))