A vending-machine operator runs a fleet of machines spread across a city, each identified by a numeric id. Every time a route technician restocks a machine, the visit is logged as one line recording which machine was restocked and how many units were loaded into it during that single visit. A machine may be restocked many times over the log's history, and the log may record visits to many different machines in any order. Given the full restock log, compute the total number of units loaded into each machine across every visit recorded for it.
For every machine id that appears at least once in the log, print one line "machine_id total_units", where total_units is the sum of units loaded into that machine across all of its visits. Print the lines in ascending order of machine_id.
Example 1
Input
5 310 7 101 20 205 15 101 10 205 5
Expected
101 30 205 20 310 7
Explanation
Machine 101 receives 20 + 10 = 30 units across its two visits, machine 205 receives 15 + 5 = 20 units, and machine 310 receives 7 units from its single visit. The output lists them sorted by ascending machine id (101, 205, 310) regardless of the order they appeared in the log.
Example 2
Input
3 42 100 42 250 42 5
Expected
42 355
Explanation
All three log lines refer to the same machine, id 42, so its total is 100 + 250 + 5 = 355. Since only one machine ever appears in the log, the output has a single line.
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 →