Open-Pit Mining

Given blocks with values and digging costs plus precedence constraints, find the maximum profit subset closed under the dig-before relation.

Hard8GraphMinimum spanning treeGreedyUnion-findNo attempts yetTime limit1sMemory limit512 MB

Problem

Open-pit mining takes rock or minerals out of the ground through a pit dug at the surface. Miners use it when a commercially useful deposit sits near the surface. The mining company ACM wants the largest profit it can get this way, and it has asked you for a program that reads a description of a piece of land and reports that profit.

One piece of land is modelled as a set of blocks of material. Block ii has a value viv_i, and digging it out of the land costs cic_i. Some blocks bury other blocks. If block jj and block kk obstruct block ii, then block jj and block kk must both be dug up before block ii can be dug up. A block can be dug up only once no block obstructs it.

The profit of a set of dug blocks is the sum of their values minus the sum of their costs. ACM may stop digging at any point, and digging no block at all gives a profit of 0.

Input

The first line contains an integer NN (1N2001 \le N \le 200), the number of blocks. The blocks are numbered 1 through NN.

The next NN lines describe the blocks. The ii-th of those lines describes block ii and starts with the value viv_i and the cost cic_i of block ii (0vi,ci2000 \le v_i, c_i \le 200). A third integer mim_i then gives the number of blocks that block ii obstructs (0miN10 \le m_i \le N - 1). After it come the labels of those blocks, mim_i integers separated by spaces. The labels are distinct, lie between 1 and NN, and never include ii.

Some digging order gets every block out. The sum of mim_i over all blocks ii is at most 500.

Output

Print a single integer, the maximum profit ACM can achieve from the given piece of land.