#K47162. Candy Dispenser
Candy Dispenser
Candy Dispenser
In a small town, a local vending machine dispenses candies based on customers’ preferences. The machine holds several types of candies, each with a limited stock. Customers approach the machine one after the other with a prioritized list of their favorite candy types.
For each customer, the machine checks their list in order and dispenses the first candy that is still available. If none of the requested candies is available, the machine returns \(-1\). In other words, given \(n\) candy types with an initial stock array \(stocks\), and \(m\) customers each with a list of preferred candy types, the machine dispenses candy type \(c\) if \(stocks[c-1] > 0\) (reducing the stock by one), otherwise \(-1\) is returned for that customer.
inputFormat
The input is read from stdin in the following format:
(n) (m)
(stocks[0] stocks[1] ... stocks[n-1])
Then, for each of the (m) customers, a line is given that starts with an integer (k) (the number of preferences) followed by (k) integers representing the preferred candy types in order.
outputFormat
Output to stdout a single line with (m) integers separated by spaces. The (i)-th integer is the candy type dispensed to the (i)-th customer, or (-1) if no candy could be dispensed.## sample
5 3
4 2 0 5 1
3 1 3 5
2 2 4
4 1 2 5 3
1 2 1