#Q159. 「一本通 5.2 例 5」皇宫看守

「一本通 5.2 例 5」皇宫看守

Description

After the incident involving the Prince of Taiping, Lu Xiaofeng was appointed as the Imperial First-Class Guard by the emperor.

The palace layout, starting from the Meridian Gate and extending to the living quarters of the imperial concubines, forms a tree structure, where certain palaces can be seen from one another. The imperial guards are stationed with strict security measures—guards every few steps—and each palace must be monitored around the clock, with varying costs for stationing guards in different palaces.

However, Lu Xiaofeng's budget is insufficient, making it impossible to station guards in every palace.

Help Lu Xiaofeng arrange the guards such that all palaces are monitored while minimizing the total cost.

Picture1

Input Format

The input describes a tree as follows:

The first line contains an integer nn, representing the number of nodes in the tree.

The second to the (n+1)(n+1)-th lines describe each palace node, in order: the node's label i (0<in)i\ (0<i\le n), the cost kk to station a guard at this palace, the number of children mm, followed by mm numbers representing the labels of the node's children r1,r2,,rmr_1,r_2,\cdots ,r_m.

For a tree with nn nodes, the node labels range from 11 to nn, and no label is repeated.

Output Format

Output the minimum total cost.

Sample 1

The arrangement of guards in six regions is shown in the left diagram.

In the right diagram, gray dots represent stationed guards. Guard 22 can monitor 1,2,5,61,2,5,6, guard 33 can monitor 1,31,3, and guard 44 can monitor 1,41,4.

Total cost: 16+5+4=2516+5+4=25

Picture2 Picture3

6
1 30 3 2 3 4
2 16 2 5 6
3 5 0
4 4 0
5 11 0
6 5 0

25

Data Range and Hint

For 100%100\% of the data, 0<n15000 \lt n\le 1500.