#K83832. Net Balance Calculation
Net Balance Calculation
Net Balance Calculation
You are given a ledger containing a sequence of transactions. Each transaction is either a credit or a debit transaction accompanied by an amount. Your task is to compute the net balance of the ledger by processing all the transactions.
The net balance is calculated using the formula:
\( \text{net_balance} = \sum_{ i=1}^{t } {\text{amount}_i \times (1 \;\text{if } \text{type}_i = \text{'credit'} \;\text{else} \; -1)} \)
Here, \(t\) is the number of transactions. Each transaction is represented by a type and an amount.
Input Constraints:
- 1 ≤ t ≤ 1000
- Each amount is an integer between 1 and 10000.
- The transaction type is either 'credit' or 'debit'.
inputFormat
The input is given via stdin
in the following format:
Line 1: A single integer t, the number of transactions. Next t lines: Each line contains a string and an integer separated by space. The string represents the transaction type ('credit' or 'debit') and the integer represents the transaction amount.
outputFormat
Output a single integer to stdout
which is the net balance after processing all the transactions.
5
credit 100
debit 50
credit 200
debit 100
credit 150
300