Bosses

Build a rooted tree on n employees where each node's parent is one of its accepted bosses, then assign minimum positive salaries with every boss exceeding the sum of children.

Hard8TreeDynamic programmingGreedyImplementationNo attempts yetTime limit1.5sMemory limit256 MB

Problem

A company of nn employees is due for a restructuring. The new hierarchy is a rooted tree, and every node is the boss of its children.

Each employee has a list of employees they accept as their boss. Every employee must also be assigned a salary. A salary is a positive integer, and the salary of each boss must be larger than the sum of the salaries of their immediate subordinates.

Structure the company so that all of the above conditions hold and the sum of all salaries is as small as possible.

Input

The first input line contains an integer nn: the number of employees. The employees are numbered 11, 22, \dots, nn.

After this, the input contains nn lines that describe the preferences of the employees. The iith such line contains an integer kik_i, followed by a list of kik_i integers. The list consists of all employees that the iith employee accepts as their boss.

Here 1n3001 \le n \le 300 and the sum of all kik_i is at most 50005\,000. The numbers on one line are distinct and never include ii itself.

Output

Print the lowest total salary among all valid restructurings on one line. You can assume that at least one solution exists.