Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Expense Solver


5 friends share accommodation. Expenses between them are shared. They have a rule that expenses are shared equally among all friends those who are a party to that expense. Their problem is they are poor in math. Hence they end up fighting over expenses. Your job is to help them in splitting the expenses and prevent in-fighting. 

Write a program to help divide the expenses. The program should be generic enough such that number of members sharing accommodation can vary. Your program should take the following inputs and give the following output.



Assumptions:
The Person paying for a transaction is himself a party in that particular expense

Input Format:

Line 1 contains the name of the friends sharing the accomodation. Line 2 provides the number of trancations(T) to be accounted. Line 3 to line T+2 will give the details of the transactions and will contain the spender's name,the amount spent and the friends involved in the transactions

Line 1


N1 N2…Nn 
where Ni is the name of all the friends sharing the accomodation,separated by white space character

Line 2



where T is the number of transactions

Line 3 to Line T+2


Ns1 A Np1 Np2 … 
where Ns is the spender, A is the amount spent in a particular transaction and Npi are the other friends involved in the transaction



Output Format: 

Line 1


For Valid Input, print
N1 A1
N2 -A2

Nn -An

where Ni is the name of the friend and Ai is the amount he has to recieve/pay. Negative(-) number represents payable.


Sample Inputs and Outputs:

SNo.

Input

Output

1



Pankaj Pranav Mayank Nihit Mukesh
5
Pankaj 600 Pranav Mayank
Pranav 450 Mayank Nihit
Mayank 300 Mukesh Pranav Nihit
Nihit 1200 Pranav Pankaj
Mukesh 3000 Pranav Pankaj Mayank


Pankaj -750.0
Pranav -1125.0
Mayank -875.0
Nihit 575.0
Mukesh 2175.0

2


Sagar Sumit Suresh
4
Sagar Sumit


Invalid Input

 


Post a Comment

0 Comments