HDOJ 2546 meal card (01 backpack)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.