Under construction

Still being built. Things may change

Quelling Blade

Time limit1sMemory limit128 MB

Summary
Given a tree of weapon prerequisites with costs and benefits, find a buying order that reaches the root in minimum time while maximizing the sum over time of owned benefit.
Level

Hard8 of 10

Topics
Greedy, DFS, Tree, Sorting
Solved
No attempts yet

Problem

A hero is playing a game in which the strongest weapon is the Quelling Blade. Every type of weapon has a benefit value BB and a cost CC. Benefits add up: if the hero owns two weapons with benefits 33 and 55, his current benefit is 3+5=83 + 5 = 8. Owning several weapons of the same type stacks their benefits as well.

To buy a weapon, the hero may first need other weapons. For example, if a weapon requires two Demon Edges, the hero must already own two Demon Edges before buying it; buying a second copy of that weapon requires two more Demon Edges. A required weapon is not consumed when the purchase is made, and one weapon type may require several weapons of the same type. Each weapon type is required by at most one other weapon type, so the dependencies form a tree rooted at the Quelling Blade.

The hero earns exactly 11 coin per second and spends coins to buy weapons; a weapon of cost CC can be bought once the hero has accumulated CC unspent coins. Because he must pay for the whole tree of prerequisites, the minimum time to obtain the Quelling Blade equals the total cost of every weapon he has to buy, and this minimum is reached no matter which valid buying order he follows.

The hero is a perfectionist: among all buying orders that reach the Quelling Blade in this minimum time, he wants to maximize the utility, defined as the sum, over each second from the start of the game up to (but not including) the second in which he obtains the Quelling Blade, of the total benefit he currently owns. Equivalently, a weapon whose purchase completes at time tt contributes its benefit during each of the S−tS - t seconds that remain before the Quelling Blade is obtained, where SS is the total buying time.

Report the maximum possible utility.

Input

The first line contains the number of test cases TT. Most test cases are small.

For each test case, the first line contains a single integer NN, the number of weapon types (1≤N≤1000)(1 \le N \le 1000).

The weapons are then described one by one. For weapon ii (weapons are numbered from 11 to NN):

  • a line with two integers BB and CC, its benefit value and cost (1≤B,C≤231−1)(1 \le B, C \le 2^{31}-1);
  • a line with a single integer PP, the number of distinct weapon types it requires;
  • PP lines, each with two integers II and AA, meaning this weapon requires AA weapons of type II.

Weapon 11 is the Quelling Blade. The total number of weapons that must be bought to obtain one Quelling Blade is less than 10610^6, and each weapon type is required by at most one other weapon type.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 11 and yy is the maximum utility. The answer always fits in a signed 64-bit integer.

Examples4

  1. Example 1

    Input
    2
    3
    1 1
    1
    2 2
    2 1
    1
    3 1
    1 1
    0
    3
    1 1
    1
    2 2
    1 1
    1
    3 1
    2 1
    0
    
    Expected output
    Case #1: 14
    Case #2: 17
    
  2. Example 2

    Input
    1
    1
    5 3
    0
    
    Expected output
    Case #1: 0
    
  3. Example 3

    Input
    1
    4
    1 2
    1
    2 1
    4 1
    1
    3 1
    2 3
    1
    4 1
    6 1
    0
    
    Expected output
    Case #1: 50
    
  4. Example 4

    Input
    1
    2
    2 2
    1
    2 3
    5 1
    0
    
    Expected output
    Case #1: 45