#QHD0000065. Thanh toán và trả tiền thừa (Making Change)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thanh toán và trả tiền thừa (Making Change)
Making Change
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
At a shop, you do not have to pay the exact purchase price. You may hand over a larger amount, receiving the difference as change. Your objective is to minimize the total number of coins passing between the buyer and the cashier: the coins you pay plus the coins returned to you.
Your wallet contains six denominations of New Zealand coins: , , , , , and cents, equivalent to , , , , , and dollars. You have a limited number of each type and cannot pay with more coins of a type than you possess. However, the shopkeeper has an unlimited supply of all six denominations for making change.
Given the counts in your wallet and the purchase price (in dollars, always written to exactly two decimal digits), choose coins from your wallet with total value at least . The cashier returns the exact difference. Find the minimum total number of coins you pay and receive. The price is a multiple of and the total wallet value is guaranteed sufficient for the purchase. Coins of equal denominations are interchangeable, and no wallet coin may be paid twice.
For instance, paying for an item costing may require fewer coins overall than giving the exact price because the cashier returns just one coin.
Input
One line contains six integers , the numbers of available -, -, -, -, -, and -cent coins, in that order. The last value is the amount , written as d.cc with two fractional digits. There is no terminating all-zero coin-count record in this version.
Output
Print one line containing the minimum number of coins transferred in the transaction, right-aligned in a field of width . Count both the coins tendered and the change returned.
Subtasks
-
Subtask 1 (20 points): for every , at least one ; , is a multiple of , and for coin values .
-
Subtask 2 (30 points): for every , at least one ; , is a multiple of , and .
-
Subtask 3 (50 points): for every , at least one ; , is a multiple of , and .
Examples
Input
2 4 2 2 1 0 0.95
Output
2
Explanation
The wallet contains two coins, four coins, two coins, two coins, and one coin. The purchase price is . Paying with the single coin requires the cashier to return one coin, for two coins transferred in total. One coin alone cannot complete this purchase, so the optimum is 2, displayed in a three-character right-aligned field.