Find Fame in the Disc Arena

Time limit5sMemory limit128 MB

Summary
A DAG gives games with fame values and prerequisite sets; pick a closed-under-prerequisites set (possibly empty) maximizing total fame.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, Greedy, Implementation
Solved
No attempts yet

Problem

In Grid World, the Disc Arena is a place where programs play gladiator disc games against other programs. The arena hosts kk games. Winning a game earns you fame and makes you popular in Grid World; losing a game deletes you. Winning the ii-th game earns fame fif_i, which need not be positive.

To be allowed to play the ii-th game, you must first have played and won every game in a fixed, known set SiS_i. The games in SiS_i are the prerequisites of game ii. The prerequisite relation has no cycles: it can never happen that game aa is required for game bb, bb is required for cc, and cc is required for aa. The only reason to play a game with negative fame is that it is a prerequisite of a game with higher positive fame.

A set CC of games is feasible if, for every game i∈Ci \in C, all of its prerequisites are also in CC; that is, i∈Ci \in C implies Si⊆CS_i \subseteq C. The fame of CC is the sum of the fames of all games in CC.

Your goal is to become as famous as possible. Given the fames and prerequisites of the games, find a feasible set of games with maximum total fame.

The figure below shows examples; arrows indicate prerequisites. In the first example, game 1 has games 2 and 3 as prerequisites, and game 3 has game 2 as a prerequisite. The maximum fame there is obtained by selecting {1,2,3}\{1, 2, 3\}: game 3 must be selected even though its fame is negative, because it is a prerequisite of game 1. Selecting no games (the empty set) is always feasible and yields fame 00, so the answer is never negative.

Input

The first line contains the number of test cases (at most 100100).

The first line of each test case contains kk, the number of games (k≤1000k \le 1000). The games are labeled 11 through kk. The next kk lines describe the games. The (i+1)(i+1)-th line has the form

fidiu1u2⋯udif_i \quad d_i \quad u_1 \quad u_2 \quad \cdots \quad u_{d_i}

where fif_i is the fame gained by winning game ii, di=∣Si∣d_i = |S_i| is the number of prerequisites, and u1,…,udiu_1, \dots, u_{d_i} are the members of SiS_i (the prerequisite game numbers).

Within each test case, the total number of prerequisites over all games, ∑i∣Si∣\sum_i |S_i|, is at most 60006000.

Output

For each test case, print the maximum fame attainable with a feasible set of games. For the xx-th test case, print exactly:

Case x: Maximum attainable fame = y

where yy is the maximum fame.

Examples1

  1. Example 1

    Input
    3
    3
    3 2 2 3
    1 0
    -2 1 2
    6
    -1 0
    3 1 1
    2 2 5 2
    2 1 6
    -4 1 6
    -1 0
    10
    -44 5 7 2 4 9 6
    -2 1 5
    10 3 4 9 6
    -129 1 8
    -71 0
    34 1 5
    -27 3 8 5 2
    -121 2 6 2
    169 3 6 7 8
    -117 3 7 1 4
    
    Expected output
    Case 1: Maximum attainable fame = 2
    Case 2: Maximum attainable fame = 3
    Case 3: Maximum attainable fame = 0