Find Fame in the Disc Arena
Time limit5sMemory limit128 MB
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 games. Winning a game earns you fame and makes you popular in Grid World; losing a game deletes you. Winning the -th game earns fame , which need not be positive.
To be allowed to play the -th game, you must first have played and won every game in a fixed, known set . The games in are the prerequisites of game . The prerequisite relation has no cycles: it can never happen that game is required for game , is required for , and is required for . 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 of games is feasible if, for every game , all of its prerequisites are also in ; that is, implies . The fame of is the sum of the fames of all games in .
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 : 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 , so the answer is never negative.

Input
The first line contains the number of test cases (at most ).
The first line of each test case contains , the number of games (). The games are labeled through . The next lines describe the games. The -th line has the form
where is the fame gained by winning game , is the number of prerequisites, and are the members of (the prerequisite game numbers).
Within each test case, the total number of prerequisites over all games, , is at most .
Output
For each test case, print the maximum fame attainable with a feasible set of games. For the -th test case, print exactly:
Case x: Maximum attainable fame = y
where is the maximum fame.