Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Constraints A person's name ranges from A to Z. 1

Constraints A person's name ranges from A to Z.

1 <= Amount <= 10^4

1 <= Transaction and Expenses <= 10^4

Input The first line contains an integer N, representing the number of transactions and expenses.

The next N lines contain the expenses and transactions.

Output Print who owes whom how much in lexicographical order of the payer. In the format Ignore printing zero balance transactions. If there are no pending dues, print "NO DUES."

Time Limit (secs) 1

Examples Example 1

Input

3

A/240/A/B/C

B/120/A/C

C/100/C/A

Output

C/A/50

C/B/40

Explanation

On the first day A spends 240 that is shared equally between A, B and C. Then B spends 120 that is shared between equally A and C and finally C spends 100 shared equally between C and A.

So, the final expenditure would be:

A: +50

B: +40

C: -90

Therefore, C will give 50 to A and 40 to B.

Example 2

Input

5

A/240/A/B/C

B/120/A/C

C/A/100

C/B/40

C/100/C/A

Output

A/C/50

Explanation

On the first day of input A spends 240 that is shared equally between A, B and C. On Second day, B spends 120 that is shared equally between A and C. By end of Day 2 C owed a total of 140 (-80 -60) and A had an outstanding of 100 to receive and B had a total of 40 to receive. Then on third and the fourth-day C settled his balance with A and B with amount 100 and 40. On the fifth day C spent 100 that is shared equally between C and A.

Therefore, the final expenditure would be:

A: -50

C: +50

Therefore, A will give C 50.

Example 3

Input

8

C/B/L100

A/240/A/B/C

B/120/A/C

C/A/100

C/100/C/A

B/A/L200

C/B/L300

B/C/100

Output

A/C/250

B/C/60

Explanation

The first day B took a loan of 100 from C and the next two days' expenses are normal spending of 240 and 120 done by A and B which is split between A, B, C and A, C respectively. The 4th day is a normal settlement of 100 done by C to A. On the 5th day C spends 100 which is split between C and A. Then the next two expenses are money lent by B and C to A and B respectively, of the amount of 200 and 300. And finally on the eight-day C pays a settlement of 100 which is the settlement of the money lent by C to B on the 1st day, so he has settled the amount of 100.

After reconciling, it is found that A owed an amount of 40 and amount of 10 to B and C respectively. But there is money that has been lent by B to A and C to B and C. But as a week has not passed by, no interest is incurred upon it.

Therefore, reconciliation will be done after the summation of the pending settlement and the interest. The final reconciliation will be that A will pay an additional 200 to B and B will pay additional 300 to C therefore after settlement the balance will be:

A/C/250

B/C/60 Note: Observe that expense settlement and the loan repayment are handled and reconciled separately.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

ISBN: 978-0764535376

More Books

Students also viewed these Databases questions