#K86237. Transaction Balance Calculation
Transaction Balance Calculation
Transaction Balance Calculation
You are given a list of financial transactions. Each transaction consists of a sender account, a receiver account, and an amount transferred.
Your task is to compute the net balance for each account after processing all transactions. The net balance is calculated as the sum of all amounts received minus the sum of all amounts sent. Formally, for each account \(i\), the balance is computed as:
\( \text{balance}[i] = \sum (\text{amount received}) - \sum (\text{amount sent}) \)
Output your results by listing each account and its balance, sorted in ascending order of account numbers. If there are no transactions, output nothing.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains a single integer \(n\) representing the number of transactions.
- Each of the next \(n\) lines contains three space-separated integers: sender, receiver, and amount.
outputFormat
For each account that appears in the transactions, output a line containing two space-separated values: the account number and its net balance. The accounts must be listed in increasing order of the account number. If \(n = 0\), output nothing.
## sample3
1 2 50
2 3 70
3 1 20
1 -30
2 -20
3 50
</p>