Under construction

Still being built. Things may change

Greedy Generosity

Time limit2sMemory limit512 MB

Summary
Simulate a vending machine's coin stock over several purchases, adding a greedy overpayment when exact greedy change is impossible, and report the total excess paid.
Level

Medium5 of 10

Topics
Simulation, Greedy, Implementation, Array
Solved
No attempts yet

Problem

Many currencies are designed so that, when you have enough coins of each denomination, making change with a greedy method uses the fewest coins possible. The greedy method repeatedly picks the largest coin that does not exceed the amount left. The currency in this problem has 1 cent, 2 cent, 5 cent, 10 cent, 20 cent and 50 cent coins. To make change for 77 cents, the greedy method picks 50 cents, then 20 cents, then 5 cents, then 2 cents.

Some shops have installed self-service checkout machines. A customer puts money in and the machine gives back any change required. The shops want customers to use these machines. Consider one way to encourage that, borrowed from gambling machines, which people also put money into and out of which they tend to get less than they put in.

Every self-service machine holds a stock of each type of coin and starts the day at its standard level. The machine always makes change with the greedy method described above. If the largest coin that does not exceed the amount left is out of stock, the machine does not substitute smaller coins. It rewards the shopper with greedy generosity instead.

The generous part works like this. If the machine cannot pay in the greedy fashion, the amount of change is increased by as little as needed for the greedy method to pay it. Take a machine that holds two 5 cent coins and one 20 cent coin, and no 10 cent coin, and that has to give 10 cents in change: it gives the 20 cent coin. After such a payout there are buzzers and flashing lights, in the spirit of a gambling machine, and the staff immediately adjust the machine's coin stock back to its standard level.

Simulate the machine and work out how much extra money it gives the customers. The machine processes several transactions in order and starts stocked at its standard level. The money the customer inserts is added to the machine's stock before the change is worked out. There is no limit on how many coins the machine can hold. Coins paid out to customers are removed from the stock. Each customer pays at least the value of the sale, and if they pay more, the machine makes change for the difference. The largest coin a customer puts in is guaranteed to exceed the value of the correct change.

Input

The first line contains six integers C1C_1, C2C_2, C5C_5, C10C_{10}, C20C_{20}, C50C_{50} (0≤Ci≤1000 \le C_i \le 100), the number of coins of each denomination when the machine is stocked at its standard level.

The second line contains one integer TT (1≤T≤10001 \le T \le 1000), the number of transactions to process.

Each of the next TT lines describes one transaction, in the order the transactions happen. Such a line contains seven integers: the value of the sale VV (1≤V≤991 \le V \le 99), then d1d_1, d2d_2, d5d_5, d10d_{10}, d20d_{20}, d50d_{50} (0≤di≤1000 \le d_i \le 100), the number of coins of each type that the customer puts into the machine.

Output

Print the total amount, in cents, that the machine pays beyond the correct change over the course of the transactions.

Examples2

  1. Example 1

    Input
    0 0 2 0 1 0
    1
    60 0 0 0 0 1 1
    
    Expected output
    10
    
  2. Example 2

    Input
    2 2 2 2 2 2
    3
    5 0 0 0 1 0 0
    5 0 0 0 1 0 0
    5 0 0 0 1 0 0
    
    Expected output
    5