#K14426. Final Balance Calculator
Final Balance Calculator
Final Balance Calculator
You are given a sequence of banking operations. Each operation consists of a type and an amount. There are two valid types:
- deposit: Add the specified amount to the current balance.
- withdrawal: Subtract the specified amount from the current balance if the balance is sufficient. If the withdrawal amount exceeds the current balance, the operation is ignored.
Any operation with a type other than deposit or withdrawal (for example, "balance" or an invalid type) must be ignored.
The final balance is computed starting from an initial balance of 0.
Formally, if you denote the current balance by \( b \) and the operation by \( (t, a) \), the update rule is:
\[ b \leftarrow \begin{cases} b + a, & \text{if } t = \texttt{deposit}, \\ b - a, & \text{if } t = \texttt{withdrawal} \text{ and } a \leq b, \\ b, & \text{otherwise.} \end{cases} \]Your task is to process the given operations in order and print the final balance.
inputFormat
The input is read from standard input (stdin) and has the following format:
<N> <operation1> <operation2> ... <operationN>
Where:
N
is an integer representing the number of operations.- Each operation is given on a separate line and consists of a string
type
and a numberamount
separated by a space.
Valid operation types are "deposit" and "withdrawal" (other types should be ignored).
outputFormat
Output the final balance on standard output (stdout). If the balance is a whole number, print it as an integer; otherwise, print it in floating point format.
## sample4
deposit 100
withdrawal 50
deposit 20
withdrawal 10
60