A delivery fleet's dispatch system logs every driver on the roster along with every delivery leg any driver has completed. You must build the fleet's mileage leaderboard: for every driver on the roster — even ones who never completed a single leg — report the total distance they have covered across all their logged legs, then rank all drivers from most total distance to least. Break ties between equal totals by the driver's name in ascending alphabetical order.
Line 1: an integer n — the number of drivers on the roster.
Each of the next n lines: an integer driverId and a string name — one roster entry.
Next line: an integer m — the number of logged delivery legs.
Each of the next m lines: an integer driverId and an integer distance — one delivery leg, giving the distance covered by that driver.
Print exactly n lines, one per roster driver: name totalDistance, ordered by totalDistance descending, then by name ascending for ties.
driverId on the roster is a distinct integer in [1, 10^9].name consists of 1 to 20 English letters (upper or lower case) and is distinct across the roster.driverId appearing among the delivery legs matches some driver on the roster.Example 1
Input
3 1 amara 2 boaz 3 chen 3 1 50 2 30 1 20
Expected
amara 70 boaz 30 chen 0
Explanation
Driver amara (id 1) covers legs of 50 and 20, totaling 70. Driver boaz (id 2) covers a leg of 30. Driver chen (id 3) has no logged legs, so their total is 0. Sorted by total distance descending: amara (70), boaz (30), chen (0).
Example 2
Input
3 1 zoe 2 amir 3 liu 2 1 40 2 40
Expected
amir 40 zoe 40 liu 0
Explanation
zoe (id 1) and amir (id 2) each total 40; liu (id 3) has no legs and totals 0. zoe and amir tie at 40, so alphabetical order breaks the tie: amir comes before zoe. Output: "amir 40", "zoe 40", "liu 0".
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 →