#C9578. Exact Ingredient Purchase Problem
Exact Ingredient Purchase Problem
Exact Ingredient Purchase Problem
You are given a set of purchase requirements for ingredients. For each requirement, you are provided with a required ingredient and its quantity, as well as an available ingredient with its quantity and price. The task is to check if the available ingredient exactly matches the required one in both name and quantity. If they match for every requirement, you must sum the prices to obtain the total cost. Otherwise, output -1
.
Formally, you are given an integer \(N\) (the number of ingredient groups) followed by \(N\) groups. Each group consists of five values:
required_item, required_quantity, available_item, available_quantity, price
.
For each group \(i\), if:
[ \text{required_item}_i = \text{available_item}_i \quad \text{and} \quad \text{required_quantity}_i = \text{available_quantity}_i, ]
then the price for this group is added to the total cost. Otherwise, the answer is -1
.
Your solution should use \(\LaTeX\) for any mathematical formulae.
inputFormat
The input is given in a single line via stdin as space-separated tokens in the following order:
- An integer \(N\): the number of ingredient groups.
- For each group, five tokens:
required_item required_quantity available_item available_quantity price
.
For example: 2 burger 3 burger 3 10 meat 4 meat 4 50
outputFormat
Output a single integer via stdout: the total cost if all available ingredients match the requirements exactly, or -1
if any group does not match.
2 burger 3 burger 3 10 meat 4 meat 4 50
60