#K90867. Minimum Cost to Feed All Animals

    ID: 37848 Type: Default 1000ms 256MiB

Minimum Cost to Feed All Animals

Minimum Cost to Feed All Animals

You are given (N) animals and (M) food types. Each food type (i) has a cost (c_i). Each animal has a list of preferred food types. Your task is to assign a food type to each animal from its preference list such that the total cost is minimized. The total cost is defined as the sum of the costs of each distinct food type used. In other words, if (S) is the set of food types chosen, the total cost is given by:

[ \sum_{i \in S} c_i ]

Find the minimum total cost to feed all the animals.

Note: It is allowed for multiple animals to choose the same food type, and the cost for that food is only counted once.

inputFormat

The first line contains two integers (N) and (M), denoting the number of animals and the number of food types respectively.

The second line contains (M) integers: (c_1, c_2, \ldots, c_M), where (c_i) is the cost of food type (i).

Then follow (N) lines. Each of these lines begins with an integer (P) representing the number of food types the animal prefers, followed by (P) integers which are the indices of the food types the animal can eat.

outputFormat

Output a single integer representing the minimum total cost required to feed all the animals.## sample

3 4
3 5 4 2
2 1 2
1 3
3 2 3 4
7