Under construction

Still being built. Things may change

Transfers

Interview

Time limit1sMemory limit1024 MB

Summary
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

NN 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 Ai,jA_{i,j}. Here Ai,jA_{i,j} is the total amount in bajtalar that friend ii owes to friend jj. The friends are numbered from 1. It is guaranteed that Ai,j=−Aj,iA_{i,j} = -A_{j,i} for all i,ji, j.

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 NN (1≤N≤161 \le N \le 16), the number of friends. Each of the next NN lines contains NN integers Ai,jA_{i,j} (−109≤Ai,j≤109-10^9 \le A_{i,j} \le 10^9).

Output

Print one integer on the first and only line: the minimum number of transfers needed to settle all debts.

Examples2

  1. Example 1

    Input
    5
    0 -1 -1 0 0
    1 0 -1 0 0
    1 1 0 0 0
    0 0 0 0 5
    0 0 0 -5 0
    
    Expected output
    2
    
  2. Example 2

    Input
    4
    0 2 -1 2
    -2 0 -3 3
    1 3 0 -4
    -2 -3 4 0
    
    Expected output
    2