A vinyl-record reseller keeps a single running ledger of every transaction made across all the titles they trade. Each ledger entry names one record title, states whether that entry was a purchase (BUY, the reseller acquired a copy) or a sale (SELL, the reseller sold a copy), records the day number the transaction happened, and gives the price involved. For every distinct record title that appears anywhere in the ledger, compute its net position: the total of all its SELL prices minus the total of all its BUY prices. The day number is recorded for completeness but has no bearing on the answer -- only the total amount bought and the total amount sold for each title matter.
n, the number of ledger entries.n lines describes one entry as four space-separated values: title op day price, where title is the record title, op is either BUY or SELL, day is the day number, and price is the price of that transaction.Print one line for every distinct record title that appears in the ledger, sorted in ascending lexicographic (dictionary) order by title. Each line must contain the title, a single space, and its net position (total SELL price minus total BUY price), as an integer.
1 <= n <= 2000title consists of 1 to 20 lowercase English letters, digits, and underscores.op is exactly BUY or SELL.1 <= day <= 3651 <= price <= 10^6BUY and SELL, in any order.Example 1
Input
5 vinylA BUY 1 80 vinylA SELL 3 150 vinylB BUY 2 40 vinylA BUY 5 20 vinylB SELL 6 100
Expected
vinylA 50 vinylB 60
Explanation
vinylA has two BUY entries (80 + 20 = 100) and one SELL entry (150), for a net position of 150 - 100 = 50. vinylB has one BUY entry (40) and one SELL entry (100), for a net position of 100 - 40 = 60. Sorted alphabetically, vinylA is printed before vinylB.
Example 2
Input
3 comet BUY 1 200 comet SELL 2 150 nova BUY 1 30
Expected
comet -50 nova -30
Explanation
comet was bought for 200 and sold for 150, for a net position of 150 - 200 = -50. nova was only bought, for 30, and never sold, for a net position of 0 - 30 = -30. Sorted alphabetically, comet is printed before nova.
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 →