Toy Box
Practice
3.8 (71 votes)
Algorithms
Easy
Greedy algorithms
Problem
74% Success 9564 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(P_{i}\) and \(B_{i}\), where \(P_{i}\) is the price of i’th toy and \(B_{i}\) is the box number assigned to it.

\(1 <= N, M <= 100 \)

\(1 <= B_{i}<= M\)

\(1 <= P_{i}<= 100\)

Output Format

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

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
411 votes
Tags:
EasySorting
Points:20
16 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Points:20
20 votes
Tags:
AlgorithmsEasyGreedy AlgorithmsSortingapprovedhiring