Problem:
Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0

Solution in Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

n,m=gets.split(' ').map{|i| i.to_i}
matrix = []
n.times do 
  job=gets.split(' ').map{|i| i.to_i}
  if matrix[job[0]].nil?
    matrix[job[0]]=[job[1]]
  else
    ary=matrix[job[0]]
    pos=ary.size-1
    0.upto(pos) do |i|
      if ary[i] < job[1]
        ary.insert(i, job[1])
        break
      end
    end
  end
end

total=0
(0...m).each do |i|
  temp_days=0
  temp_wage=0
  (m-i).downto(0) do |j|
    if not matrix[j].nil? and not matrix[j].empty? and temp_wage < matrix[j][0]
      #j
      #matrix[j][0]
      temp_days=j
      temp_wage=matrix[j][0]
    end
  end
  if temp_days != 0
    total+=temp_wage
    matrix[temp_days].shift
  end
end
puts total