
75  ZOJ Monthly, February 2009  G
MM enjoyed cookies very much. On Saint Valentine's Day, when she stepped into a big cookie store again, she wouldn't leave unless DD spent all his money in pocket! There are N kinds of cookies, labeled from 1 to N, and all can be bought without any restriction by the store. But actually, for some kinds of cookies, MM wanted to buy one piece at most, and for some kinds of cookies, MM wanted to buy K_{i} pieces at most, and for some other kinds of cookies, there didn't exist an upper bound that MM wanted to buy. There is another requirement from MM: there are some groups of cookies, MM considered their tastes similar, so she wanted to buy at most one kind of cookies in each group. A kind of cookie wouldn't appear in more than one group. For the ith kind of cookies, MM has an "enjoyable value" E_{i}, if DD bought A_{i} pieces of this kind for her, and A_{i} didn't exceed her upper bound, MM get E_{i}A_{i} of enjoyable value. After buying cookies, MM's total enjoyable value will be the sum of E_{i}A_{i}. But actually, poor DD had only D dollars, and the price for the ith kind of cookies is P_{i} dollars per piece. DD must spend all his D dollars to buy cookies, to meet requirements about amount and taste from MM, and to make MM's enjoyable value as high as possible. What's more, as you know, a legal plan's enjoyable value must be nonnegative. Input There are multiple test cases. Each test case consists of three parts. The first part is one line with two integers N and D. The second part has N lines, line i consists of three integers K_{i}, E_{i} and P_{i}. If K_{i} equals to 0, it means for ith kind of cookies, there didn't exist an upper bound that MM wanted to buy, otherwise K_{i} is the upper bound for ith kind of cookies. The third part describes the groups. A nonnegative integer G represents the number of groups, and then G lines, each line consists of some integers represents labels of kinds of cookies in this group. One blank line between test cases. Output If the proper and optimal plan exists, output the maximal total enjoyable value ΣE_{i}A_{i}, otherwise output "i'm sorry...". Output one line per text case. Data Restriction 1 <= N <= 1024, 0 <= D <= 1024.0 <= K_{i} <= 1024, 1024 <= E_{i} <= 1024, 0 < P_{i} <= D. 0 <= G <= 8. All numbers referred are integers. Number of test cases is no more than 80. Sample Input 2 1024 0 1 3 0 0 1 0 10 1023 1 1 1 1 1 2 1 1 4 1 1 8 1 1 16 1 1 32 1 1 64 1 1 128 3 1 256 1 1 512 1 9 10 10 1023 1 1 1 1 1 2 1 1 4 1 1 8 1 1 16 1 1 32 1 1 64 1 1 128 1 1 256 1 1 512 1 9 10 Sample Output 341 5 i'm sorry... Author: CUI, Tianyi Source: ZOJ Monthly, February 2009 