Restaurant
Time limit8sMemory limit512 MB
Simulate a single-cook kitchen that batches identical dishes up to a per-dish limit and reports each customer's serving time.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Sorting, Greedy
- Solved
- No attempts yet
Problem
Steve runs a small restaurant in a city. He is the only cook in his restaurant, so he cooks every dish that customers order.
He handles orders on a first-come-first-served basis. Each dish takes a prescribed time to prepare. A customer can order more than one dish at a time, so Steve starts cooking with the dish that takes the longest to prepare. If two or more dishes take the same amount of time to prepare, he cooks them in the order they appear in the restaurant's menu card. He does not care in which order these dishes were ordered. When he finishes all dishes ordered by a customer, he immediately passes them to the waitress, who serves them to the customer. Serving takes negligible time.
While he is cooking one order, another customer may arrive and order dishes. For efficiency, Steve decided to prepare multiple dishes of the same kind together when possible. When he starts cooking new dishes, he looks over the orders accepted by that time (including an order accepted exactly at that time, if any) and counts how many of the same dish he will cook next. Cooking takes the same amount of time no matter how many dishes he prepares at once. Unfortunately, the kitchen has limited capacity, so it is sometimes impossible to prepare the requested number of dishes at once. In such cases, he prepares as many dishes as possible.
Your task is to write a program that simulates the restaurant. Given a list of dishes on the menu card and orders from customers with their accepted times, your program should output the times when each customer is served.
Input
The input contains multiple data sets. Each data set has the following format:
N M
Name1 Limit1 Time1
...
NameN LimitN TimeN
T1 K1 Dish1,1 . . . Dish1,K1
...
TM KM DishM,1 . . . DishM,KM
N (1 ≤ N ≤ 20) and M (1 ≤ M ≤ 100) are the number of entries on the menu card and the number of orders Steve will receive, respectively. Each Namei is the name of a dish of the i-th entry on the menu card and consists of up to 20 alphabetical letters. Limiti (1 ≤ Limiti ≤ 10) is the number of dishes he can prepare at the same time. Timei (1 ≤ Timei ≤ 1000) is the time required to prepare a dish (or dishes) of the i-th entry. Tj (1 ≤ Tj ≤ 10000000) is the time when the j-th order is accepted. Kj (1 ≤ Kj ≤ 10) is the number of dishes in the j-th order. Each Dishj,k represents a dish in the j-th order.
Every dish in the orders is listed on the menu card, but an order may contain multiple occurrences of the same dish. The orders are given in ascending order of Tj, and no two orders are accepted at the same time.
The input is terminated by a line containing two zeros. This line is not part of any data set and should not be processed.
Output
Your program should produce M lines of output for each data set. The i-th line of the output should contain a single integer that indicates the time when the i-th order will be completed and served to the customer.
Print a blank line between two successive data sets.