The organizers of the Riverside Night Bazaar keep one ledger sheet that lists, for every item on sale, the price charged by each of three fixed booths — Booth A, Booth B and Booth C — along the lane. Not every booth stocks every item: when a booth does not carry an item, its column on the sheet holds -1 instead of a price. Before the public price board can be printed, the sheet needs to be flattened into one line per (item, booth) pair for every booth that actually stocks that item, skipping every -1 entry.
Produce the flattened ledger sorted by item id in increasing order; within the same item, list the booths in the fixed order A, then B, then C.
n — the number of items on the sheet.n lines: four space-separated integers id price_a price_b price_c — the item's unique id and the price charged at Booth A, Booth B and Booth C respectively, where a value of -1 means that booth does not stock the item.Print one line per (item, booth) pair whose price is not -1, formatted as id booth price (booth is one of the single letters A, B, or C). Lines must be ordered by id ascending, and for equal id by booth in the order A, B, C. If no booth stocks any item, print nothing.
id values are distinctprice_a, price_b, price_c is either -1 or an integer in [1, 1000000]Example 1
Input
3 1 10 -1 15 2 -1 20 -1 3 5 5 5
Expected
1 A 10 1 C 15 2 B 20 3 A 5 3 B 5 3 C 5
Explanation
Item 1 is stocked by Booth A (10) and Booth C (15) but not B (-1), so it contributes "1 A 10" then "1 C 15". Item 2 is only stocked by Booth B (20), contributing "2 B 20". Item 3 is stocked by all three booths at price 5, contributing "3 A 5", "3 B 5", "3 C 5". The items are already in ascending id order, so the six lines are printed in that order.
Example 2
Input
2 50 -1 -1 7 10 3 -1 -1
Expected
10 A 3 50 C 7
Explanation
Item 50 appears first in the input but only Booth C stocks it (price 7); item 10 appears second but only Booth A stocks it (price 3). Because the ledger must be sorted by id ascending, item 10's line "10 A 3" is printed before item 50's line "50 C 7", even though item 50 came first in the input.
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 →