/*
* 首先用5元买价值最大的,然后就是zero one knaspsack
* First 5 yuan to buy the largest value, then zero one knaspsack
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXCNT 1001
int dp[MAXCNT], price[MAXCNT];
int zero_one_knapsack(int n, int max_price, int max_v)
{
if( max_v < 5 ) {
return max_v;
}
int flag(0); max_v -= 5;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i ++) {
if( max_price == price[i] && !flag ) {
flag = 1; continue;
}
for(int v = max_v; v >= price[i]; v --) {
dp[v] = max(dp[v], dp[v-price[i]]+price[i]);
}
}
return (max_v+5)-max_price-dp[max_v];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, surplus, max_price;
while( scanf("%d", &n) ) {
if( !n ) {
break;
}
max_price = 0;
for(int i = 0; i < n; i ++) {
scanf("%d", &price[i]);
max_price = max(max_price, price[i]);
}
scanf("%d", &surplus);
printf("%d\n", zero_one_knapsack(n, max_price, surplus));
}
return 0;
}
hdu_2546
最新推荐文章于 2019-07-13 09:27:23 发布