These days, robots can drive cars, but can they throw a good party? The Code Jam team's research into this topic is still at an early stage. We just deployed robot shoppers to our local supermarket to buy party supplies for our World Finals in Toronto, but their first-order model of a Canadian party was very simple: they just bought "bits" (a bit being a small donut-like treat found in the area). We will work on improving their AI later, but for now, we want to help them purchase all of those bits as quickly as possible.
The supermarket has cashiers who can scan customers' purchases. The cashier will:
- accept a maximum of items per customer
- take seconds to scan each item
- spend a further seconds handling payment and packaging up the bits.
That is, a customer who brings bits to the cashier (where must be less than or equal to ) will spend a total of seconds interacting with that cashier.
Before the robots interact with any cashiers, you will distribute the bits among the robots however you want. (Bits must remain intact; you cannot break them up into fractional pieces!) Any robot that gets no bits will not get to interact with a cashier, and will go away disappointed.
Then, for each robot with at least one bit, you will choose a different single cashier. (Two robots cannot use the same cashier, and a robot cannot use more than one cashier.) The robots all start interacting with their cashiers at time 0. Note that once a robot finishes interacting with its cashier, it cannot be given more bits and cannot interact with other cashiers.
If you help the robots make optimal choices, what is the earliest time at which all of the robots can finish interacting with their cashiers?
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each begins with one line with three integers , , and : the numbers of robot shoppers, bits, and cashiers. Then, there are more lines. The of these represents the cashier, and it has three integers , , and : the maximum number of bits, scan time per bit (in seconds), and payment/packaging time (in seconds) for that cashier, as described above.
Output Specification
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the earliest time (in seconds) at which all robots can finish interacting with their cashiers.
Limits
.
, for all .
, for all .
, for all .
The sum of the largest values of . (It is possible for at least one subset of cashiers to handle all of the bits.)
Time limit: 15 seconds per test set.
Memory limit: 1 GB.
Test Set 1
.
.
Test Set 2
.
.
Sample Input
3
2 2 2
1 2 3
1 1 2
2 2 2
1 2 3
2 1 2
3 4 5
2 3 3
2 1 5
2 4 2
2 2 4
2 5 1
Sample Output
Case #1: 5
Case #2: 4
Case #3: 7
In Sample Case #1, there are two robots, two bits, and two cashiers, and each cashier can only handle one item. So, you must give one bit to each robot. Cashier 1 takes 5 seconds, and Cashier 2 takes 3 seconds, so the time required is 5 seconds.
Sample Case #2 is similar to the previous case, except that now Cashier 2 can handle up to 2 items. So, it is best to give all the bits to one robot and have that robot use Cashier 2. This takes 1 second per item plus 2 seconds = 4 seconds.
In Sample Case #3, the optimal strategy is to send one robot with 2 bits to cashier 2, and two robots with 1 bit each to any of the other cashiers.
Comments