A traveling fairground wants to close out the weekend by ranking its visitors on a spending leaderboard. Every game booth charges a fixed number of tickets per play, and the front desk has a log of every time a visitor redeemed one or more plays at a booth. For each visitor, compute the total number of tickets they spent across every redemption in the log, then rank the visitors from biggest spender to smallest.
Line 1 contains two integers B and K — the number of booths and the number of redemption records.
Each of the next B lines contains two integers booth_id and cost — the number of tickets it costs to play that booth once. All booth_id values are distinct.
Each of the next K lines contains three integers visitor_id, booth_id, and plays — meaning that visitor redeemed plays turns at that booth. The same visitor may appear on multiple lines (at the same booth or different booths), and every booth_id referenced in a redemption line is guaranteed to appear exactly once among the booth lines.
Print one line per distinct visitor who appears in at least one redemption record, formatted as visitor_id spent, where spent is the sum over all of that visitor's redemption lines of plays * cost (using the cost of the referenced booth). Order the lines by spent descending; break ties by visitor_id ascending. Booths with no redemptions produce no output line.
Example 1
Input
2 4 7 10 9 15 501 7 4 501 9 2 502 9 4 503 7 6
Expected
501 70 502 60 503 60
Explanation
Booth 7 costs 10 tickets/play, booth 9 costs 15. Visitor 501 spends 4*10 + 2*15 = 70. Visitor 502 spends 4*15 = 60. Visitor 503 spends 6*10 = 60. Sorted by spend descending: 501 (70), then the tie between 502 and 503 (both 60) broken by ascending visitor_id, giving 502 before 503.
Example 2
Input
1 1 3 5 200 3 7
Expected
200 35
Explanation
There is one booth (id 3, cost 5) and one redemption: visitor 200 played it 7 times, spending 7*5 = 35 tickets.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →