Under construction

Still being built. Things may change

Journal Editing

Time limit2sMemory limit512 MB

Summary
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 00. He has already estimated the number of words every proof needs. Find the shortest possible length of the article.

To prove Theorem 00 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 nn (1≤n≤201 \le n \le 20).

Then, for each theorem ii (0≤i≤n−10 \le i \le n-1) in order of its number:

  • One line with the number of proofs pip_i (1≤pi≤101 \le p_i \le 10) of theorem ii.
  • pip_i lines, each containing ll, kk, d0,…,dk−1d_0, \dots, d_{k-1} separated by spaces. Here ll (0≤l≤1060 \le l \le 10^6) is the length of the proof, kk (0≤k≤n−10 \le k \le n-1) is the number of theorems the proof relies on, and djd_j (0≤dj≤n−10 \le d_j \le n-1) are the numbers of those theorems.

Theorem 00 can be proven in at least one way.

Output

Print one line with a single integer, the shortest possible length of David's article.

Examples2

  1. Example 1

    Input
    2
    2
    10 0
    3 1 1
    1
    4 1 0
    
    Expected output
    10
    
  2. Example 2

    Input
    4
    2
    1 2 1 3
    5 1 2
    1
    2 0
    1
    0 0
    2
    2 0
    1 1 1
    
    Expected output
    4