ZOJ Problem Set - 3164
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 Ki 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" Ei, if DD bought Ai pieces of this kind for her, and Ai didn't exceed her upper bound, MM get EiAi of enjoyable value. After buying cookies, MM's total enjoyable value will be the sum of EiAi.
But actually, poor DD had only D dollars, and the price for the ith kind of cookies is Pi 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 non-negative.
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 Ki, Ei and Pi. If Ki equals to 0, it means for ith kind of cookies, there didn't exist an upper bound that MM wanted to buy, otherwise Ki is the upper bound for ith kind of cookies.
The third part describes the groups. A non-negative 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.
If the proper and optimal plan exists, output the maximal total enjoyable value ΣEiAi, otherwise output "i'm sorry...".
Output one line per text case.
Data Restriction1 <= N <= 1024, 0 <= D <= 1024.
0 <= Ki <= 1024, -1024 <= Ei <= 1024, 0 < Pi <= D.
0 <= G <= 8.
All numbers referred are integers.
Number of test cases is no more than 80.
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
341 5 i'm sorry...
Author: CUI, Tianyi
Source: ZOJ Monthly, February 2009