This page is still under construction.

Parts of this page are still being built. What you see may change.

Make Purse Light

Time limit8sMemory limit512 MB

Summary
Given his coin counts and an amount to pay, find which coins to hand over so that the total number of coins he keeps is minimized, accounting for optimal change.
Level

Medium6 of 10

Topics
Brute force, Dynamic programming, Greedy, Math
Solved
No attempts yet

Problem

Mr. Bill is shopping at a store. His wallet holds some coins (10-yen, 50-yen, 100-yen, and 500-yen coins), and he wants to spend as many of these coins as he can. That is, he wants to pay for the item with an appropriate number of coins so that after he receives his change, the total number of coins in his wallet is as small as possible.

Fortunately, the clerk at this store is very scrupulous and kind, so change is always given in the optimal way. For example, five 100-yen coins are never given instead of one 500-yen coin. Also, for example, he may hand over five 10-yen coins and receive a 50-yen coin as change. However, he must not pay in a way that returns a coin of the same denomination as one he handed over. For example, if he handed over a 10-yen coin as payment but a different 10-yen coin comes back as change, that would be a completely meaningless exchange.

Mr. Bill is poor at arithmetic, however, so he could not work out by himself how many coins he should actually use. He has therefore asked you for help. Your job is to write a program that, given the numbers of coins in his wallet and the amount to pay, determines the denominations and numbers of coins he should use. Note that the clerk does not use banknotes for change.

Input

The input contains several test cases.

Each test case consists of two lines. The first line contains a single integer giving the amount Mr. Bill must pay, in yen. The second line contains four integers, which in order give the numbers of 10-yen, 50-yen, 100-yen, and 500-yen coins in his wallet.

The amount to pay is always a multiple of 10 yen. That is, the ones digit of the amount is always 0. You may also assume that the wallet contains at most 20 coins of each denomination. No case in which payment is impossible is given in the input.

The end of the input is indicated by a line containing a single 0.

Output

For each test case, output the denominations and numbers of coins Mr. Bill should use.

Each line of the output contains two integers ci and ki, meaning that ki coins of denomination ci yen are used for the payment. When several denominations are used, output as many lines as needed in increasing order of ci. See the output example below.

The output must not contain extra spaces. Consecutive test cases are separated by a blank line.

Examples1

  1. Example 1

    Input
    160
    1 1 2 0
    160
    1 0 2 10
    0
    
    Expected output
    10 1
    50 1
    100 1
    
    10 1
    100 2