Shopping Offers
IOI'95

In a certain shop, each kind of product has an integer price. For example, the price of a flower is 2 zorkmids (z) and the price of a vase is 5z. In order to attract more customers, the shop introduces some special offers.

A special offer consists of one or more product items together for a reduced price, also an integer. Examples:

• three flowers for 5z instead of 6z, or
• two vases together with one flower for 10z instead of 12z.

Write a program that calculates the price a customer has to pay for a purchase, making optimal use of the special offers to make the price as low as possible. You are not allowed to add items, even if that would lower the price.

For the prices and offers given above, the (lowest) price for three flowers and two vases is 14z: two vases and one flower for the reduced price of 10z and two flowers for the regular price of 4z.

### INPUT FORMAT

The input file has a set of offers followed by a purchase.
 Line 1: s, the number of special offers, (0 <= s <= 99). Line 2..s+1: Each line describes an offer using several integers. The first integer is n (1 <= n <= 5), the number of products that are offered. The subsequent n pairs of integers c and k indicate that k items (1 <= k <= 5) with product code c (1 <= c <= 999) are part of the offer. The last number p on the line stands for the reduced price (1 <= p <= 9999). The reduced price of an offer is less than the sum of the regular prices. Line s+2: The first line contains the number b (0 <= b <= 5) of different kinds of products to be purchased. Line s+3..s+b+2: Each of the subsequent b lines contains three values: c, k, and p. The value c is the (unique) product code (1 <= c <= 999). The value k indicates how many items of this product are to be purchased (1 <= k <= 5). The value p is the regular price per item (1 <= p <= 999). At most 5*5=25 items can be in the basket.

### SAMPLE INPUT (file shopping.in)

`21 7 3 52 7 1 8 2 1027 3 28 2 5`

### OUTPUT FORMAT

A single line with one integer: the lowest possible price to be paid for the purchases.

### SAMPLE OUTPUT (file shopping.out)

```14代码：
/*LANG: CTASK: shopping*/#include<stdio.h>#define MAXS 106#define MAX 6int encode[MAX];int dp[MAX][MAX][MAX][MAX][MAX];int sell[MAXS][MAXS];int buy[MAX];int s;int Encode(int code){    int i;    for (i = 0; i < 5; i++)    {        if (encode[i] == code)        {            return i;        }        else if (!encode[i])        {            encode[i] = code;            return i;        }    }}void Read(){    int i, j, code, n, c, k, b;    scanf("%d", &s);    for (i = 0; i < s; i++)    {        scanf("%d", &n);        for (j = 0; j < n; j++)        {            scanf("%d%d", &c, &k);            code = Encode(c);            sell[i][code] = k;        }        scanf("%d", &sell[i]);    }    scanf("%d", &b);    for (i = 0; i < b; i++)    {        scanf("%d%d", &c, &k);        code = Encode(c);        sell[s+i][code] = 1;        buy[code] = k;        scanf("%d", &sell[s+i]);    }    s += b;}void Solve(){    int i0, i1, i2, i3, i4, j, tmp;    for (i0 = 0; i0 <= buy; i0++)    {        for (i1 = 0; i1 <= buy; i1++)        {            for (i2 = 0; i2 <= buy; i2++)            {                for (i3 = 0; i3 <= buy; i3++)                {                    for (i4 = 0; i4 <= buy; i4++)                    {                        for (j = 0; j < s; j++)                        {                            if (i0 >= sell[j] && i1 >= sell[j] && i2 >= sell[j] && i3 >= sell[j] && i4 >= sell[j])                            {                                tmp = dp[i0 - sell[j]][i1 - sell[j]][i2 - sell[j]][i3 - sell[j]][i4 - sell[j]] + sell[j];                                if (!dp[i0][i1][i2][i3][i4] || dp[i0][i1][i2][i3][i4] > tmp)                                {                                    dp[i0][i1][i2][i3][i4] = tmp;                                }                            }                        }                    }                }            }        }    }    printf("%d\n", dp[buy][buy][buy][buy][buy]);}int main(){    freopen("shopping.in", "r", stdin), freopen("shopping.out", "w", stdout);    Read();    Solve();    fclose(stdin), fclose(stdout);    //system("pause");    return 0;}
```