Bank Queue
InterviewTime limit1sMemory limit256 MB
Pick at most one person per minute before each deadline to maximize the total cash collected.
Problem
Oliver manages a bank and wants to close for the day soon. A long queue has formed at the counter: people heard that the bank raised its interest rate by 42 percent (from 0.01 percent per year to 0.0142 percent per year) and they all want to deposit cash.
There are far more people than time. Only one counter is open and it serves one person per minute. Oliver is greedy, so he wants to pick a subset of the queue whose total deposited cash is as large as possible. That money then works for the bank overnight.
There is a catch. Some people have to be somewhere else and cannot wait until the bank closes, so if they are not served by a certain time they simply leave. Oliver also switched off the infrared door sensor so that nobody else can come in, because the hall is crowded already.
Compute the largest amount of cash Oliver can collect before the bank closes, serving at most one person per minute.
Input
The first line contains the number of people in the queue and the time in minutes until the bank closes (, ).
Each of the next lines contains two integers and . Here is the amount of cash in Swedish crowns that person carries, and is the time in minutes from now after which person leaves if not served. Serving one person takes one minute, and person must be started no later than minute . A service starts at an integer minute among , , up to , and only one person is served at each of those minutes. The values satisfy and .
Output
Print one line with the largest amount of money that can be collected from the queue before the bank closes.