#K5086. ATM Machine Simulation

    ID: 28958 Type: Default 1000ms 256MiB

ATM Machine Simulation

ATM Machine Simulation

You are required to simulate an Automated Teller Machine (ATM) that supports three operations:

  • deposit a specified amount: The amount must be a multiple of 5. When depositing, the machine dispenses bills in the denominations $50, $20, $10, and $5 using a greedy strategy (i.e. use the largest bill possible first). It then prints the details of the bills deposited.
  • withdraw a specified amount: Withdraw the requested amount using the minimum number of bills (using the same denominations and greedy strategy). If the ATM does not have sufficient funds, an error is produced.
  • balance: Print the current total amount of money available in the ATM.

The following example illustrates the operations:

Example Input:
4
deposit 205
balance
withdraw 35
balance

Example Output: Bills deposited: $50x4 $5x1 205 Bills used: $20x1 $10x1 $5x1 170

</p>

If an operation encounters an error, such as depositing an amount not a multiple of 5 or attempting to withdraw more than available, print Error (and terminate further processing).

Note: All amounts and operations follow the greedy algorithm. In mathematical terms, for any amount \(A\) (with \(A\equiv 0\pmod{5}\)), the number of bills of each denomination \(d\) is determined by iteratively subtracting the largest possible denomination. That is, if \(n_d\) is the number of bills of denomination \(d\), then:

\[ A = 50\,n_{50} + 20\,n_{20} + 10\,n_{10} + 5\,n_{5} \]

inputFormat

The first line of input contains an integer \(N\) representing the number of operations. Each of the next \(N\) lines contains a command in one of the following formats:

  • deposit X where \(X\) is an integer.
  • withdraw X where \(X\) is an integer.
  • balance

Input is provided via stdin.

outputFormat

For each operation, output the result on a new line. For a deposit, print the deposited bills as: Bills deposited: $50xA $20xB $10xC $5xD (only include bills where the count is positive). For a withdrawal, print the bills used as: Bills used: $50xA $20xB $10xC $5xD (again, only listing non-zero counts). For a balance inquiry, simply print the current balance as an integer. If an operation results in an error (for example, depositing an amount that is not a multiple of 5 or attempting to withdraw more than the current balance), print Error and terminate processing without executing subsequent operations. Output must be printed to stdout.

## sample
4
deposit 205
balance
withdraw 35
balance
Bills deposited: $50x4 $5x1

205 Bills used: $20x1 $10x1 $5x1 170

</p>