#C5172. Bank Account Operation Simulation
Bank Account Operation Simulation
Bank Account Operation Simulation
This problem simulates a simple bank account system featuring four operations: deposit
, withdraw
, undo
, and balance
. Initially, the bank account has a balance of 0. When the operation deposit X
is executed, X is added to the account and the previous balance is saved. For the withdraw X
operation, if the current balance is at least X, then X is subtracted (after storing the current balance) – otherwise, the operation is ignored. The undo
operation reverts the account to its last recorded state (i.e. before the most recent deposit or withdraw). The balance
operation outputs the current account balance.
Note: The undo
command only undoes the effect of deposit or withdraw operations; it does not reverse previous balance queries or other undo operations.
You are given a sequence of operations to be processed sequentially. Your task is to simulate these operations and output the bank account balance each time a balance
command is encountered.
inputFormat
The first line contains an integer n (1 ≤ n ≤ 10^5), representing the number of operations. Each of the next n lines contains a single operation that can be one of the following:
• deposit X • withdraw X • undo • balance
For operations with an amount, X is an integer (1 ≤ X ≤ 10^9).
outputFormat
For each balance
operation in the input, output the current bank account balance on a new line.## sample
8
deposit 100
balance
withdraw 50
balance
undo
balance
undo
balance
100
50
100
0
</p>