#K73887. Maximum Total Movie Rating

    ID: 34075 Type: Default 1000ms 256MiB

Maximum Total Movie Rating

Maximum Total Movie Rating

You are given the number of friends and for each friend a list of movies they are interested in together with their corresponding ratings. Each friend’s input consists of an integer n (the number of movies), followed by n movie indices, and then n ratings for these movies. Note that the same movie may be offered by different friends with different ratings.

Your task is to compute the maximum total rating obtainable by selecting at most one rating per movie. In other words, for each distinct movie you may only take the highest rating available. Then, you should choose a collection of up to f movies (one per friend) such that the sum of their ratings is maximized. If there are fewer than f distinct movies, simply sum the ratings of all available movies.

The final answer is the sum of the top f ratings from these distinct movies.

inputFormat

The input is read from standard input and has the following format:

f
n1 m1 m2 ... mn1 r1 r2 ... rn1
n2 m1 m2 ... mn2 r1 r2 ... rn2
...
nf m1 m2 ... mnf r1 r2 ... mnf

Here, the first line contains an integer f representing the number of friends. This is followed by f lines. For each friend, the first integer is n, the number of movies that friend is interested in. Then follow n integers which are the movie indices, and then n integers representing the corresponding ratings.

outputFormat

Output a single integer to standard output: the maximum total rating achievable by selecting the best available rating for each movie and taking the top f among them.

## sample
3
3 1 2 3 5 6 9
2 2 4 7 10
2 1 4 8 8
27