Journal Editing
Time limit2sMemory limit512 MB
Given theorems that depend on other theorems and multiple possible proofs with costs, find the minimum total cost to prove Theorem 0.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Bit manipulation, DFS
- Solved
- No attempts yet
Problem
David is writing an article for the Bulletin of the Association of Proof Completions. The article proves several theorems. He wrote one proof for every theorem, and because he is an eager student he wrote several proofs for some of them. A proof of a theorem may rely on a few other theorems.
The article has to be as short as possible to get published, and the only result David really cares about is the main theorem, Theorem . He has already estimated the number of words every proof needs. Find the shortest possible length of the article.
To prove Theorem you pick one of its proofs and put it in the article, then prove every theorem that proof relies on inside the same article, each of those by picking one of its own proofs in the same way. A theorem is never written twice, so a theorem that several proofs rely on is paid for once. The length of the article is the sum of the lengths of the proofs it contains. Circular reasoning is not allowed: no theorem may support its own proof, directly or indirectly.
Input
The first line contains the number of theorems ().
Then, for each theorem () in order of its number:
- One line with the number of proofs () of theorem .
- lines, each containing , , separated by spaces. Here () is the length of the proof, () is the number of theorems the proof relies on, and () are the numbers of those theorems.
Theorem can be proven in at least one way.
Output
Print one line with a single integer, the shortest possible length of David's article.