#C13777. Grocery List Summary
Grocery List Summary
Grocery List Summary
You are given a grocery list where each item is described by its name, quantity, and the categories it belongs to. The input is presented in a specific format, with each item given on a separate line. Your task is to compute the total quantity for each category among the following five fixed categories: Fruits, Vegetables, Dairy, Meat, and Grains.
If the same item name appears more than once, only the information from its last occurrence should be used (i.e. the grocery list behaves like a dictionary with unique keys). The final output should display the totals in a specific format (see Output Description).
In mathematical terms, if an item i has quantity \(q_i\) and belongs to a subset \(C_i \subseteq \{\text{Fruits},\,\text{Vegetables},\,\text{Dairy},\,\text{Meat},\,\text{Grains}\}\), then for each category \(c\) the total is computed as:
\[ Total(c) = \sum_{i:\, c \in C_i} q_i \]Note that if an item appears multiple times, only its last occurrence is considered.
inputFormat
The first line of input contains an integer \(n\) representing the number of grocery items.
Each of the following \(n\) lines contains an item record in the following format:
name quantity k category1 category2 ... category_k
where:
name
is a string without spaces.quantity
is a positive integer.k
is the number of categories for the item.- Each
category
is a string. Only the following five categories are recognized: Fruits, Vegetables, Dairy, Meat, and Grains. (Categories are case-insensitive.)
If an item appears more than once, only the last record should be considered.
outputFormat
Output a summary string that starts with the line "Total quantities by category:" followed by one line per category. Each line must list the category name (with the first letter capitalized) and the corresponding total quantity, in the following order: Fruits, Vegetables, Dairy, Meat, Grains.
The output format should exactly match the example below:
Total quantities by category: Fruits: X Vegetables: Y Dairy: Z Meat: W Grains: V
where X, Y, Z, W, and V are the computed total quantities for the respective categories.
## sample6
apple 4 1 fruits
broccoli 2 1 vegetables
cheese 1 1 dairy
chicken 3 1 meat
bread 5 1 grains
pizza 1 2 dairy grains
Total quantities by category:
Fruits: 4
Vegetables: 2
Dairy: 2
Meat: 3
Grains: 6
</p>