#P1417. FunnyGame
FunnyGame
ACboy loved to play a strategy game, in a map have N castle, every castle has certain treasures , Each game Trinity allowed enter M of castle and get the treasures in this castle. But due to the geographical position reasons, some castle cannot directly to enter, if you want to enter the castle you must conquer other a particular castle. Can you help Trinity calculate to get as much treasure ?
Input
Each test case first include 2 integer, N,M. (1 < = M < = N < = 200);
In the next N business line, including two integers, a, b.In ith line,if you want to enter the ith castle you must enter ath castle first.if a=0 ,you can enter the ith castle directly.The number about b represent the treasures in ith castle.When N=0 and M=0,then input ended.
In the next N business line, including two integers, a, b.In ith line,if you want to enter the ith castle you must enter ath castle first.if a=0 ,you can enter the ith castle directly.The number about b represent the treasures in ith castle.When N=0 and M=0,then input ended.
Output
For each test case, output an integer, represent Trinity enter M castle get the most number of treasures.
Sample Input
3 2
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0
Sample Output
5
13