On a tree where each house picks a divisor of its income, maximize the total sum of chosen values so that every pair of adjacent houses picks coprime values.
Hard8Dynamic programmingTreeNumber theoryDFSNo attempts yetTime limit8sMemory limit512 MBThe Jones family is a very wealthy family that has lived in one area of the country for many generations. Houses in this area are scattered around, and dirt roads connect them. Two houses are neighbours if a dirt road connects them directly. Using the dirt roads, there is exactly one way to get from any house to any other house without walking on some road at least twice.
The country has a new king, and he has decided to collect taxes in a rather weird way.
Each household picks some divisor of its income from last year. The value may be the full income if the household wishes. If any two neighbours pick numbers whose greatest common divisor is larger than 1, the king takes every dollar from every person in the country. If every pair of neighbours picks numbers whose greatest common divisor is 1, each household keeps the number it chose.
One option is for every household to pick $1, but that is not very profitable. Maximize the total amount of money the households keep.
The input contains a single test case.
The first line contains one integer n (2≤n≤250), the number of houses. Each of the next n lines holds the information about one household. The first of those lines describes house 1, the second describes house 2, and so on.
Each of these lines begins with two integers Ik (1≤Ik≤200000000) and mk (1≤mk≤n−1), the income of household k and the number of neighbours of household k. Then follow mk distinct integers, the neighbours of household k. Each of them is between 1 and n inclusive, and none of them equals k.
Every road is bidirectional and is listed twice in the input, once for each endpoint of the road.
Output one integer, the maximum total amount of money that the households can keep.