Q: The card has m yuan, and n kinds of vegetables can be bought (only once for each dish ), as long as the card amount is greater than or equal to 5 yuan, you can buy any food (brush to negative ). Calculate the minimum amount of balance on the card.
Idea: the most expensive of a dish must be the last to buy, and then use 01 backpack (M-5) yuan money can buy the largest amount of food, then (m-the maximum amount-the price of the most expensive dish) is required.
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Define maxn1111
Int val [maxn], f [maxn];
Main ()
{
Int n, m, I, j, max, pos;
While (scanf ("% d", & n)
{
For (I = 1; I <= n; I ++)
Scanf ("% d", & val [I]);
Scanf ("% d", & m );
If (m <5)
{
Printf ("% d \ n", m );
Continue;
}
For (max =-1, pos = 0, I = 1; I <= n; I ++)
If (max <val [I])
{
Max = val [I];
Pos = I;
}
Memset (f, 0, sizeof (f ));
For (I = 1; I <= n; I ++)
{
If (pos = I)
Continue; www.2cto.com
For (j = M-5; j> = val [I]; j --)
If (f [j] <f [j-val [I] + val [I])
F [j] = f [j-val [I] + val [I];
}
Printf ("% d \ n", m-f [M-5]-max );
}
Return 0;
}
Author: sdc1992