#P1201. Gift Exchange Balance
Gift Exchange Balance
Gift Exchange Balance
There is a group of n friends who exchange gifts with each other. Each friend has prepared some money for the gifts. The money prepared by a friend will be divided equally among the list of friends who will receive his gifts. For each friend, determine how much more money he receives than he spends on giving gifts. Note that if a friend has no recipients then he does not spend any money.
Formally, suppose friend i gives an amount A and has k recipients. He gives each recipient \(\lfloor A/k \rfloor\) money and he spends \(\lfloor A/k \rfloor \times k\). His net balance is defined as the total money received from others minus the money he spent. Your task is to compute the net balance for each friend in the order in which their names are given.
inputFormat
The input starts with an integer n (n ≥ 2) which is the number of friends. The next n lines each contain the name of a friend (each name is at most 14 characters long). Following that, there are exactly n blocks, one for each friend, in which each block has the following format:
- A line containing the name of the giver.
- A line with two integers: the amount of money the giver has prepared for gifts and the number of recipients k.
- If k > 0, then k lines follow, each containing the name of one recipient.
Each friend may appear as a giver exactly once, and the order in which the blocks appear can be arbitrary. It is guaranteed that all names given among friends exist in the initial list.
outputFormat
For each friend (in the same order as the initial friend list), output a line containing the friend's name and his net balance (the total money received minus the money spent), separated by a space.
sample
5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
laura
0 2
dave
owen
owen
500 1
vick
vick
0 0
amr
150 2
vick
laura
dave -198
laura 141
owen -434
vick 641
amr -150
</p>