ANARC08G - Think I will Buy Me a Football Team

Falling Stocks. Bankrupted companies. Banks with no Cash. Seems like the best time to invest: "Think I'll buy me a football team!"

No seriously, I think I have the solution to at least the problem of cash in banks. Banks nowadays are all owing each other great amounts of money and no bank has enough cash to pay other banks' debts even though, on paper at least, they should have enough money to do so. Take for example the inter-bank loans shown in figure (a). The graph shows the amounts owed between four banks (A...D). For example, A owes B 50M while, at the same time, B owes A 150M. (It is quite common for two banks to owe each other at the same time.) A total amount of 380M in cash is needed to settle all debts between the banks.

In an attempt to decrease the need for cash, and after studying the example carefully, I concluded that there's a lot of cash being transferred unnecessarily. Take a look:

  1. C owes D the same amount as D owes A, so we can say that C owes A an amount of 30M and get D out of the picture.
  2. But since A already owes C 100M, we can say that A owes C an amount of 70M.
  3. Similarly, B owes A 100M only, (since A already owes B 50M.) This reduces the above graph to the one shown in figure (b) which reduces the needed cash amount to 190M (A reduction of 200M, or 53%.)
  4. I can still do better. Rather than B paying A 100M and A paying 70M to C, B can pay 70M (out of A's 100M) directly to C. This reduces the graph to the one shown in figure (c). Banks can settle all their debts with only 120M in cash. A total reduction of 260M or 68%. Amazing!

I have data about inter-bank debts but I can't seem to be able to process it to obtain the minimum amount of cash needed to settle all the debts. Could you please write a program to do that?

Input

Your program will be tested on one or more test cases. Each test case is specified on N + 1 lines where N < 1,000 is the number of banks and is specified on the first line. The remaining N lines specifies the inter-bank debts using an N×N adjacency matrix (with zero diagonal) specified in row-major order. The ith row specifies the amounts owed by the ith bank. Amounts are separated by one or more spaces. All amounts are less than 1000. The last line of the input file has a single 0.

Output

For each test case, print the result using the following format:

k. B A

where k is the test case number (starting at 1), B is the amount of cash needed before reduction and A is the amount of cash after reduction.

Example

Input:
4
  0  50 100   0
150   0  20   0
  0   0   0  30
 30   0   0   0
0

Output:
1. 380 120

Added by:Ahmed Aly
Date:2009-07-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ANARC 2008

hide comments
2014-01-04 14:14:31 innovolt
luking like graph problem bt it's not.
2013-12-30 14:40:50 IKKA
Getting NZEC in java,Any idea??
2012-12-29 22:31:20 Harshit Agarwal
just use fast i/o ...even scanf works got AC 0.84(without fast i/o) and 0.15(with fast input)...!!!...:D
2012-06-24 16:50:37 code_red
replacing cin,cout with scanf,printf, respectively got acc. after TLE
O(n^2)
2011-12-08 19:08:51 manish kapoor
yes,i got AC,but in .89 sec.Those who have done it in .05 sec pls suggest me the way or just mail me the code(or hint)@ manish.2313kapoor@gmail.com
Thanks
2010-08-03 18:23:58 Matja¾
@piyushkumar, my program that was AC returns 5 :| it seems that this problem is somewhat ill definied,since there is no exact deffinition of what the output value should be
2010-06-27 20:05:36 shivmitra mishra
i got AC in .95 sec...i want to know hw some people hav got AC in 0.05 sec or so..is it all fast i/o implementations or they had some better algo...

Last edit: 2010-07-27 14:30:23
2009-10-01 16:15:56 piyush kumar
what should be the output of:
3
0 5 0
0 0 9
4 0 0


Last edit: 2009-10-01 16:16:46
2009-08-18 14:39:12 amaroq
0.91 seconds, so it got worse. It's submission 2641048, which I guess you're able to see.
2009-08-06 12:20:49 Abdelrahman Hatem
how do you now time of using scanf(),amaroq?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.