Simulate judges proposing problems in turn and keep each proposal whose hardness reaches the running total until k problems are chosen.
Easy2SimulationInterviewNo attempts yetTime limit1sMemory limit256 MBOne of the hardest parts of preparing an ACM ICPC contest is building a problemset with a reasonable number of easy problems. At the Not Easy European Regional Contest the jury does it as follows.
There are n jury members, numbered from 1 to n. Before the meeting, judge i prepared pi easy problems. Each of those problems has a hardness between 0 and 49, and a larger value means a harder problem. Every judge also knows a practically infinite supply of hard problems, whose hardness is 50. During the meeting the judges pick k problems for the contest.
The judges propose problems in increasing order of their numbers. Judge 1 takes the first problem from the list of easy problems he has not proposed yet, or a hard problem if he has already proposed all of them, and proposes it. The proposed problem is selected for the contest if its hardness is greater than or equal to the total hardness of the problems selected so far, and otherwise it is considered too easy and dropped. Judge 2 then does the same, and after judge n the turn returns to judge 1. The procedure stops as soon as k problems are selected.
If every judge has proposed all of his easy problems and fewer than k problems are selected, the judges fill the remaining slots with hard problems regardless of the total hardness.
Compute the total hardness of the problemset the judges built.
The first line contains the number of judges n and the number of problems k (2≤n≤10, 8≤k≤14). Each of the next n lines describes the problems prepared by one judge, in order of judge number. Line i starts with pi (1≤pi≤10), followed by pi integers between 0 and 49, the hardnesses of the problems prepared by judge i in the order they will be proposed.
Print one integer, the total hardness of the selected problems.
In the first example, three problems with hardnesses 0, 1 and 1 are selected first. Judge 1 then proposes the problem with hardness 3 and it is selected, while the problem with hardness 1 proposed by judge 2 is too easy. Next the problems with hardnesses 5, 12 and 23, proposed by judges 3, 1 and 2, are selected. The following three proposals with hardnesses 17, 1 and 20 are rejected, and the problemset is completed by the hardness 49 problem of judge 3. The total hardness is 94.
In the second example, three problems with hardnesses 1, 1 and 2 are selected first. The second problem of judge 1 has hardness 3 and is too easy. Judge 2 has run out of easy problems, so he proposes a problem with hardness 50 and it is selected. The hardness 5 problem of judge 3 is rejected. The remaining six slots are filled with hard problems, so the total hardness is 54+6×50=354.