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: Accepted)

class Heap

  attr_accessor(:data, :size)

  def initialize()
    @data = []
    @size = 0
  end

  def data()
    @data
  end

  def push(x)
    @data[@size] = x
    i = @size
    j = ((i + 1) / 2 ) - 1
    issorted = false
    while j >= 0 && !issorted
      if @data[i] > @data[j]
        @data[i], @data[j] = @data[j], @data[i]
        i = j
        j = ((i + 1) / 2 ) - 1
      else
        issorted = true
      end
    end
    @size += 1
  end

  def pop()
    if @size == 0
      nil
    else
      re = @data[0]
      @size -= 1
      @data[0] = @data[size]
      i = 0
      j1 = (i+1) * 2 - 1
      j2 = (i+1) * 2
      issorted = false
      while size > j1 && !issorted
        if size > j2
          max_i = @data[j1] > @data[j2] ? j1 : j2
          if @data[i] >= @data[max_i]
            issorted = true
          else
            @data[i], @data[max_i] = @data[max_i], @data[i]
            i = max_i
            j1 = (i+1) * 2 - 1
            j2 = (i+1) * 2
          end
        else
          if @data[i] >= @data[j1]
            issorted = true
          else
            @data[i], @data[j1] = @data[j1], @data[i]
            i = j1
            issorted = true
          end
        end
      end
      re
    end
  end

end

def connect(a,b)
  list = []
  while !a.empty? && !b.empty?
    if a[0] > b[0]
      list.push(a[0])
      a.shift
    else
      list.push(b[0])
      b.shift
    end
  end
  if a.empty?
    list = list + b
  else
    list = list + a
  end
  list
end

n,m = gets.chomp.split(" ").map(&:to_i)

works = Array.new(10**5+1).map{Array.new}
n.times do |i|
  a,b = gets.chomp.split(" ").map(&:to_i)
  works[a].push(b)
end

ans = 0
heap = Heap.new
for i in 1..m
  works[i].each do |x|
    heap.push(x)
  end

  highest = heap.pop()
  if highest != nil
    ans += highest
  end
end
puts ans