Under construction

Still being built. Things may change

Balancing Bank Accounts

Time limit1sMemory limit128 MB

Summary
Given transactions among travelers, compute each person's net balance and print the canonical chain of payments using prefix sums.
Level

Easy3 of 10

Topics
Prefix sum, Hash map, Implementation
Solved
No attempts yet

Problem

Once upon a time, a large team of fifteen travellers was coming home from the ACM World Finals, and they were faced with a big problem.

Over the previous weeks there had been many money transactions between them: sometimes one person paid the theme-park entrance fees for the others, someone else paid for a hotel room, another person paid for the rental car, and so on.

Now the big reckoning begins. Some people had paid more than others, so the individual accounts have to be balanced again. "Who has to pay whom, and how much?" — that is the question to solve.

Because such a calculation is a lot of work, we now need a program that will solve this problem again next year.

Input

The input contains one or more test cases.

Each test case starts with a line containing two integers: the number of travellers nn (n≥2n \ge 2) and the number of transactions tt (t≥1t \ge 1). The next nn lines each contain the name of one traveller. Names consist only of alphabetic characters and contain no whitespace. The following tt lines describe the transactions in the format name1 name2 amount, meaning name1 gave amount dollars to name2. Each amount is a non-negative integer less than 1000010000.

Input is terminated by a line containing 00 for both nn and tt.

Output

For each test case, first print a line Case #i, where ii is the test-case number, starting from 11.

Then print the transactions that settle every traveller's account, using the same name1 name2 amount format as the input (meaning name1 pays amount dollars to name2). Amounts must not be negative: instead of printing a negative amount, swap the two names.

Because many different settlements are valid, print the following canonical one so that the answer is unique. Number the travellers 1,2,…,n1, 2, \dots, n in the order their names are listed in the input, and define the net balance of traveller kk as

bk=(total received by k)−(total given by k)b_k = (\text{total received by } k) - (\text{total given by } k)

over all input transactions. For each ii from 11 to n−1n-1, compute the prefix sum Si=b1+b2+⋯+biS_i = b_1 + b_2 + \dots + b_i and print:

  • if Si>0S_i > 0, traveller ii pays traveller i+1i+1: print the name of traveller ii, the name of traveller i+1i+1, and SiS_i;
  • if Si<0S_i < 0, traveller i+1i+1 pays traveller ii: print the name of traveller i+1i+1, the name of traveller ii, and −Si-S_i;
  • if Si=0S_i = 0, print nothing for this pair.

Print these transactions in increasing order of ii. This uses at most n−1n-1 transactions. Finally, print a blank line after each test case, even after the last one.

Examples2

  1. Example 1

    Input
    2 1
    Donald
    Dagobert
    Donald Dagobert 15
    4 4
    John
    Mary
    Cindy
    Arnold 
    John Mary 100
    John Cindy 200
    Cindy Mary 40
    Cindy Arnold 150
    0 0
    
    Expected output
    Case #1
    Dagobert Donald 15
    
    Case #2
    Mary John 300
    Cindy Mary 160
    Arnold Cindy 150
    
    
  2. Example 2

    Input
    2 1
    P
    Q
    P Q 10
    2 1
    Q
    P
    Q P 10
    0 0
    
    Expected output
    Case #1
    Q P 10
    
    Case #2
    P Q 10