#P2732. [USACO3.3] 商店购物 Shopping Offers

[USACO3.3] 商店购物 Shopping Offers

Description

Promotions bundle one or more products and sell them at a discount. For example:

Three flowers cost 55 instead of 66, and 22 vases plus 11 flower cost 1010 instead of 1212. Write a program to compute the cost for a customer to buy certain products, using the offers to minimize the total cost. Even if adding extra products could lower the total cost, you are not allowed to do that.

For the products above, the minimum cost to buy three flowers and two vases is: buy the offer of two vases and one flower for 1010, and buy two flowers at the regular price for 44.

Input Format

The input file contains some offers provided by the store, followed by a shopping list (at most 55 types of products).

Line 11: The number of offer types ss (0s990 \leq s \leq 99).

Lines 22 to s+1s+1: Each line describes one offer using several integers. The first integer nn (1n51 \leq n \leq 5) is the number of different products in this offer. Then there are nn pairs of integers cc and kk, meaning kk (1k51 \leq k \leq 5) units of the product with code cc (1c9991 \leq c \leq 999) are included in this offer. The last integer pp is the offer price (1p9,9991 \leq p \leq 9{,}999). The offer price is always lower than the regular price.

Line s+2s+2: An integer bb (0b50 \leq b \leq 5), the number of different products to buy.

Lines s+3s+3 to s+b+2s+b+2: Each of these bb lines contains three integers c,k,pc, k, p. Here cc is the unique product code (1c9991 \leq c \leq 999), kk is the required quantity of product cc (1k51 \leq k \leq 5), and pp is the regular price of product cc (1p9991 \leq p \leq 999). In total, at most 5×5=255 \times 5 = 25 items are to be purchased.

Output Format

Output a single line with one integer: the minimum total price to purchase these items.

2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 2 5
14

Hint

Translation from NOCOW.

USACO Training Section 3.3.

Translated by ChatGPT 5