#C14324. ATM Transaction Simulator

    ID: 43961 Type: Default 1000ms 256MiB

ATM Transaction Simulator

ATM Transaction Simulator

You are required to simulate an ATM machine that processes a sequence of transactions. The machine supports three types of commands:

  1. Deposit x — Deposit an amount x into the account.
  2. Withdraw x — Withdraw an amount x if sufficient funds exist. If the balance is less than x, the withdrawal is ignored.
  3. CheckBalance — This command does not change the balance but is used to mark the end of the transaction list.

The problem is defined mathematically as follows. Let (B_0=0) and for each transaction i with command Ci and amount (a_i) if applicable:

  • If (C_i=\text{Deposit}), then (B_i = B_{i-1} + a_i).
  • If (C_i=\text{Withdraw}) and (B_{i-1} \ge a_i), then (B_i = B_{i-1} - a_i), otherwise (B_i = B_{i-1}).
  • If (C_i=\text{CheckBalance}), then simply report (B_{i-1}).

Your program must read the transactions from standard input and output the final balance to standard output.

inputFormat

Standard input will begin with an integer (n) which represents the number of transactions. This is followed by (n) lines, each containing one transaction command. Each transaction is in one of the following formats:

  • Deposit x
  • Withdraw x
  • CheckBalance

Here, (x) is a non-negative integer.

outputFormat

Output a single integer which is the final balance after processing all the transactions.## sample

3
Deposit 100
Withdraw 50
CheckBalance
50