Transfers
InterviewTime limit1sMemory limit1024 MB
Given a debt matrix for N friends (N up to 16), find the fewest transfers that bring every friend's net balance to zero.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
friends went on a trip. During the trip they often had to pay for various activities, such as a taxi, a hotel tip, or a restaurant meal. After the trip, the friends counted who owed whom, and the result is a matrix . Here is the total amount in bajtalar that friend owes to friend . The friends are numbered from 1. It is guaranteed that for all .
It is time to settle up. The friends want every debt paid so that everyone ends at zero. A direct transfer between each debtor and creditor is the simplest way, but banks charge a fee for every transfer. So the friends decided to settle in a smarter way. Each person only cares that the total they receive or pay is correct. It does not matter who pays whom, as long as the balance of every account matches at the end.
What is the minimum number of transfers needed?
Input
The first line contains one natural number (), the number of friends. Each of the next lines contains integers ().
Output
Print one integer on the first and only line: the minimum number of transfers needed to settle all debts.