#C1279. Restaurant Inventory Manager

    ID: 42255 Type: Default 1000ms 256MiB

Restaurant Inventory Manager

Restaurant Inventory Manager

You are required to implement an inventory management system for a restaurant. In this system, each ingredient is identified by a string identifier and has a corresponding quantity.

The system supports three types of operations:

  • add <id> <quantity>: Adds a new ingredient with the given identifier and quantity to the inventory. If the ingredient already exists, increase its quantity by the given amount.
  • update <id> <quantity>: Updates the quantity of an ingredient to the new given quantity. If the ingredient does not exist, it should be added.
  • print: Prints the inventory list in alphabetical order of the ingredient identifiers. Each line should contain the ingredient id followed by a space and the quantity.

Note: The operations are given through standard input. The first line of the input contains an integer T denoting the number of operations, and each of the following T lines contains one operation.

Example:

5
add sugar 5
add flour 2
update sugar 3
update salt 1
print

The expected output for the above example is:

flour 2
salt 1
sugar 3

Formula: The update operation sets the ingredient's quantity: \( Q_{new} = q \), and the add operation increments as: \( Q_{new} = Q_{old} + q \) if the ingredient exists.

inputFormat

The first line contains an integer T, the number of operations. Each of the following T lines contains an operation in one of the following formats:

  • add <ingredient_id> <quantity>
  • update <ingredient_id> <quantity>
  • print

outputFormat

Whenever the "print" operation appears, output the inventory list in alphabetical order, where each line contains the ingredient_id and the corresponding quantity separated by a space.

## sample
5
add sugar 5
add flour 2
update sugar 3
update salt 1
print
flour 2

salt 1 sugar 3

</p>