PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000065. Thanh toán và trả tiền thừa (Making Change)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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: 55, 1010, 2020, 5050, 100100, and 200200 cents, equivalent to 0.050.05, 0.100.10, 0.200.20, 0.500.50, 1.001.00, and 2.002.00 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 AA (in dollars, always written to exactly two decimal digits), choose coins from your wallet with total value at least AA. The cashier returns the exact difference. Find the minimum total number of coins you pay and receive. The price is a multiple of 0.050.05 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 1.051.05 for an item costing 0.550.55 may require fewer coins overall than giving the exact price because the cashier returns just one 0.500.50 coin.

Input

One line contains six integers q1,…,q6q_1,\ldots,q_6, the numbers of available 55-, 1010-, 2020-, 5050-, 100100-, and 200200-cent coins, in that order. The last value is the amount AA, 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 33. Count both the coins tendered and the change returned.

Subtasks

  • Subtask 1 (20 points): 0≤qi≤40\le q_i\le4 for every ii, at least one qi>0q_i>0; 0.05≤A≤1.000.05\le A\le1.00, AA is a multiple of 0.050.05, and ∑iqivi≥A\sum_i q_iv_i\ge A for coin values viv_i.

  • Subtask 2 (30 points): 0≤qi≤150\le q_i\le15 for every ii, at least one qi>0q_i>0; 0.05≤A≤2.500.05\le A\le2.50, AA is a multiple of 0.050.05, and ∑iqivi≥A\sum_i q_iv_i\ge A.

  • Subtask 3 (50 points): 0≤qi≤500\le q_i\le50 for every ii, at least one qi>0q_i>0; 0.05≤A≤4.950.05\le A\le4.95, AA is a multiple of 0.050.05, and ∑iqivi≥A\sum_i q_iv_i\ge A.

Examples

Input

2 4 2 2 1 0 0.95

Output

  2

Explanation

The wallet contains two 0.050.05 coins, four 0.100.10 coins, two 0.200.20 coins, two 0.500.50 coins, and one 1.001.00 coin. The purchase price is 0.950.95. Paying with the single 1.001.00 coin requires the cashier to return one 0.050.05 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.