The Dank Memes Casino is one of the best casinos of the world. They have a rule that if you win or lose money, you will be paid at the beginning of the next month.

But, catastrophe struck and there was an earthquake, which demolished the casino.

Now, all the poker players have gathered and realised that the casino is not the person that owes them money or they do not owe the casino money, but as a matter of fact, they owe each other!

Your mission, should you choose to accept, will be to find out who owes who money.

Also, do keep in mind that the shortest code wins.


The first line contains the names of the people separated by spaces (Let there be n names).

The second line contains n integers, the ith of which denotes the ith person's winnings or losings at poker. A positive number means that he is owed money from someone, whereas a negative number entails that he owes someone money.

Flexible input methods are accepted.


Print out as many lines as required. Each line should contain the name of the person who owes the money, the name of the person who is owed money, the amount of money that is owed separated by a space.

Input Constraints

2 <= n <= 6

-100000 <= amount of money <= 100000

Guaranteed Constraints

Sum of all integers in the array = 0

Sample Test Cases

Test Case 1

Sample Input

Messi Ronaldo Hazard Ibrahimovich
1000 1000 -2000 0

Sample Output

Hazard Messi 1000
Hazard Ronaldo 1000

Test Case 2

Sample Input

Maradona Pele 
20000 -20000

Sample Output

Pele Maradona 20000

Test Case 3

Sample Input

1 1 1 1 -4

Sample Output

E A 1
E B 1
E C 1
E D 1

Test Case 4

Sample Input

3 3 1 2 -4 -5

Sample Output

F D 2
F C 1
F B 2
E B 3
E B 1
E A 3


There may be multiple answers, anyone of them will be accepted. For example:

1 1 -2

can yield both of the following:

C A 1
C B 1


C B 1
C A 1

both are correct.

  • 2
    \$\begingroup\$ What if multiple people have debts? Do the payments need to be equally distributed? \$\endgroup\$
    – Neil A.
    Commented Jun 24, 2017 at 9:01
  • 1
    \$\begingroup\$ Typiclly, we allow for much more relaxed input; in this case, normally we would allow to take input as key-value pairs in the language's implementation; normally, functions are allowed in place of programs. Ultimately it's the challenger's decision but that's typically what happens. \$\endgroup\$
    – hyper-neutrino
    Commented Jun 24, 2017 at 9:09
  • 3
    \$\begingroup\$ No negotiation on the output format. -1, most competitive entries will want to return a list of strings from a function \$\endgroup\$
    – Stephen
    Commented Jun 24, 2017 at 12:13
  • 3
    \$\begingroup\$ "readability" ...This is Code Golf. "Readability" is meaningless here. \$\endgroup\$ Commented Jun 24, 2017 at 19:18
  • 2
    \$\begingroup\$ I hope the strictness in outputs are just discouraged not completely boycotted, otherwise there would be no meaning of specifiying output requirements. \$\endgroup\$
    – 0xffcourse
    Commented Jun 25, 2017 at 6:31

2 Answers 2


Python 3, 173 bytes

m={x:int(y)for x,y in zip(a(),a())}
l=lambda k:m[k]
while any(map(l,m)):d=min(m,key=l);s=max(m,key=l);t=min(m[s],-m[d]);m[d]+=t;m[s]-=t;print(d,s,t)

Try it online!

-6 bytes thanks to ppperry using dictcomps instead of dict of a listcomp

  • 1
    \$\begingroup\$ You can save five bytes by converting the second line to a dictcomp: m={x:int(y)for x,y in zip(a(),a()))} \$\endgroup\$ Commented Jul 21, 2017 at 1:26
  • \$\begingroup\$ @ppperry I did not know that works :o thanks! \$\endgroup\$
    – hyper-neutrino
    Commented Jul 21, 2017 at 1:28
  • \$\begingroup\$ @HyperNeutrino has anyone ever told you how awesome you are? \$\endgroup\$ Commented Jul 21, 2017 at 17:27
  • \$\begingroup\$ @KoishoreRoy hm? \$\endgroup\$
    – hyper-neutrino
    Commented Jul 21, 2017 at 17:37

Python 2, 146 bytes

def f(p,b):
	while a:
		if d:print D,C,min(-d,c)

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.