Toy Box

3.8

71 votes
Algorithms, Easy, Greedy Algorithms
Problem

You have toys and toy boxes. Initially all boxes are empty, and each box can contain only one toy. Each toy has a price and a box number assigned to it. If you want to choose a toy, you must put it in its assigned box, and of course that box can’t be used for any other toys. You need choose some toys (with their boxes) such that summation of their price is maximized.

Input Format

First line contains two integers  and , they are number of toys and number of toy boxes repectively. Each of the next  lines will contain information about each toy  and , where  is the price of i’th toy and  is the box number assigned to it.

Output Format

Print one integer, maximum summation of  price of toys that you can choose by following the problem description.

 

Sample Input
5 6
3 1
4 2
1 1
2 3
1 6
Sample Output
10
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

You can get 10 by choosing all toys except the third one. Beacuse it can't be put in first box, which is already carrying first toy.

Editor Image

?