#C1510. Merge Inventory Updates

    ID: 44724 Type: Default 1000ms 256MiB

Merge Inventory Updates

Merge Inventory Updates

You are given multiple inventory update batches. Each update batch is represented by a multi‐line input. The first line of an update batch contains an integer n representing the number of items in this update. This is followed by n lines, each containing an item identifier (a non-empty string without spaces) and an integer representing the quantity to add (which may be negative). The update batch is terminated by a line containing the word end. Initially, the inventory is empty. Your task is to merge all updates into a final inventory where the quantity for each item is the sum of all updates. Finally, output each item and its final quantity sorted in lexicographical order by the item identifier.

Mathematically, let \( Q_i \) be the quantity change for item i in a single update. The final inventory for item i is given by:

[ Q_i^{final} = \sum_{j=1}^{k} Q_{i,j} ]

where \( k \) is the total number of update batches. If an item never appears in any update, its quantity is assumed to be 0 and should not be printed. The output should list items in lexicographical order.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. The first line contains an integer U representing the number of update batches.
  2. For each update batch:
    1. The first line contains an integer n, the number of items in the update.
    2. The next n lines contain an item identifier and an integer quantity separated by a space.
    3. The update batch is terminated by a line with the word end.

It is guaranteed that all numeric values are valid integers and the string end appears after reading the specified number of items.

outputFormat

Output to standard output (stdout) the final merged inventory. Each line should contain an item and its final quantity (separated by a single space). The items must be printed in lexicographical order. If the final inventory is empty, do not output anything.

## sample
1
3
apple 50
banana 30
orange -10
end
apple 50

banana 30 orange -10

</p>