#C1101. Final Inventory Calculation

    ID: 40279 Type: Default 1000ms 256MiB

Final Inventory Calculation

Final Inventory Calculation

You are given a list of products with their initial inventory counts, and a sequence of purchase events. Each purchase reduces the corresponding product's inventory by 1, but only if the product is in stock. Note that the product names in the purchase events are case-insensitive. Your task is to compute the final inventory counts of each product. The order of the output must be the same as the input order.

Formally, let \(P\) be the number of products, and let the product list be \(\{(name_i, count_i)\}_{i=1}^{P}\). Let \(Q\) be the number of purchases, and let the purchase list be \(\{purchase_j\}_{j=1}^{Q}\). For each purchase \(purchase_j\), convert it to lowercase and if it matches a product name in lowercase and its inventory is more than zero, then reduce that product's count by one.

For example, with products: "apple" 10, "banana" 5, "cherry" 2 and purchases: apple, banana, apple, cherry, cherry, cherry, apple, banana, the final inventory will be: "apple" 7, "banana" 3, "cherry" 0.

Ensure that your solution reads input from standard input (stdin) and writes the results to standard output (stdout).

inputFormat

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

P
name1 count1
name2 count2
... (P lines)
Q
purchase1
purchase2
... (Q lines)

Where:

  • P is an integer representing the number of products.
  • Each of the next P lines contains a product name (a string without spaces) and its initial inventory count (an integer) separated by a space.
  • Q is an integer representing the number of purchases.
  • Each of the next Q lines contains a purchased product name (a string).

outputFormat

For each product (in the same order as the input), print a line with the product name and its final inventory count separated by a space.

name1 final_count1
name2 final_count2
... (P lines)
## sample
3
apple 10
banana 5
cherry 2
8
apple
banana
apple
cherry
cherry
cherry
apple
banana
apple 7

banana 3 cherry 0

</p>