Product Order Totals
InterviewTime limit1sMemory limit128 MB
Sum the order quantities for each distinct product name, then print each product with its total sorted by name length, then alphabetically.
- Level
Medium4 of 10
- Topics
- Hash map, Sorting, String, Implementation
- Solved
- No attempts yet
Problem
A factory receives product-production orders from each of its sales offices. You want to aggregate the previous day's orders and compute the total production quantity for each product.
The first line of the input contains the number of order records . Each of the next lines contains a product name and an order quantity, separated by a space. A product name consists of at most 5 uppercase English letters. The same product may appear in more than one order record, and the records are given in no particular order.
Add up the order quantities for each distinct product, then output each product name together with its total, separated by a space. Print the products in the following order.
Ordering: sort by the length of the name in ascending order; when two names have the same length, sort them alphabetically, comparing character by character from the front and using the first position at which they differ.
Input
In the input data, the number of distinct products, each order quantity, and each product's total are all at most .
Output
Print each product name and its total on its own line, following the ordering described above. Make sure the final line also ends with a newline character.