Mutual debt offset
In light of the economic crisis, many businesses are struggling to collect debts from buyers and settle their own debts with sellers. To address this, the bank plans to minimize the total debt of its clients by implementing mutual debt offsetting. The goal is to adjust the debts of clients in such a way that each client's balance remains unchanged. This balance is defined as the difference between the total debts owed to them and the total debts they owe.
Your task is to write a program that converts the provided list of debts into one with the smallest possible total debt.
Input
Your program should read input data for multiple tests from a single ASCII text file. Each test is separated by an empty line. Each line within the file represents a debt obligation and consists of 3 natural numbers: the debtor's number, the creditor's number, and the amount of debt. Numbers are separated by spaces.
The number of enterprises will not exceed 100, and the debt amounts will not exceed 30,000 units.
Output
Your program should output the results for all tests into one ASCII text file, with results for different tests separated by an empty line. For each test, the output should include a list of debts remaining after mutual offsetting, formatted in the same way as the input. Following this, provide a list of balances for all clients who were either debtors or creditors. Each line of this list should contain the enterprise number and its balance, separated by a space. Finally, at the end of each test's results, output the total sum of debts on a separate line.