Toy Box

3.8

71 votes
Algorithms, Easy, Greedy Algorithms
Problem

You have N toys and M 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 N and M, they are number of toys and number of toy boxes repectively. Each of the next N lines will contain information about each toy Pi and Bi, where Pi is the price of i’th toy and Bi is the box number assigned to it.

1<=N,M<=100

1<=Bi<=M

1<=Pi<=100

Output Format

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

 

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

?