Algorithm title: UVA 10626 Buying Coke (DP + memory Search)

Source: Internet
Author: User

I often buy Coca-Cola from the vending machine at work. Usually I buy several cokes in once, since my working mates also likes. A Coke in the vending machine costs 8 Swedish crowns, and the machine accept with the values 1, 5 and 10. As soon as I press the Coke button (after has inserted sufficient amount of), I receive a Coke followed by the ex Change (if any). The exchange is always given into as few coins as possible (this are uniquely determined by the coin set used). This procedure are repeated until I ' ve bought all Cokes I want. Note This I can pick up the coin exchange and use those coins when buying further.

Now, what are the least number of coins I must insert, given the number of Cokes I want to buy and the number of coins I ha ve of each value? Please help me solve this problem while I create some harder problems for you. You may assume this machine won ' t run out of coins and that I always have enough I coins.

Input

The ' the ' the ' the ' the ' the ' number of test cases (at most contains). Each case is then given in a line by itself. A test case consists of four integers: C (the number of Cokes I want to Buy), n1, n 5( the number of coins of value 1, 5 and Respecti vely). The input limits are 1 <= C <=, 0 <= n1 <=, 0 <= n5 <= and 0 <= n <= .

Output

For each test case, output a line containing a single integer:the minimum number of coins needed to insert into the Vendi Ng Machine.

Sample input Output for sample input

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.