#K46097. Account Balance Calculation
Account Balance Calculation
Account Balance Calculation
You are given a sequence of financial transactions that alter an account balance. The account starts at 0. Each transaction consists of a transaction type and an amount. If the transaction type is credit
, the amount is added to the balance; if it is debit
, the amount is subtracted from the balance. The twist is that if at any point the running balance becomes negative, you must immediately stop processing further transactions and output -1
.
The process can be summarized by the following formula:
\( balance = \begin{cases} 0, & \text{if no transactions}\\ \text{if } balance_i < 0 \text{ then } -1, & \text{otherwise}\\ \text{otherwise, } balance = \sum_{i=1}^{n} (\text{credit}_i - \text{debit}_i) \end{cases}\)
Your task is to compute the final output based on the above rules.
inputFormat
The input is read from standard input (stdin). The first line contains an integer N, the number of transactions. The next N lines each contain a transaction. Each transaction is represented by a string (either 'credit' or 'debit') and an integer amount, separated by a space.
outputFormat
Output a single integer to standard output (stdout) that represents the final balance. If at any point the balance becomes negative, output -1 immediately.## sample
0
0