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 PriorityQueue
#   def initialize(array = [])
#     @data = []
#     array.each{|a| push(a)}
#   end

#   def push(element)
#     @data.push(element)
#     bottom_up
#   end

#   def pop
#     if size == 0
#       return nil
#     elsif size == 1
#       return @data.pop
#     else
#       min = @data[0]
#       @data[0] = @data.pop
#       top_down
#       return min
#     end
#   end

#   private

#   def swap(i, j)
#     @data[i], @data[j] = @data[j], @data[i]
#   end

#   def size
#     @data.size
#   end

#   def parent_idx(target_idx)
#     (target_idx - (target_idx.even? ? 2 : 1)) / 2
#   end

#   def bottom_up
#     target_idx = size - 1
#     return if target_idx == 0
#     parent_idx = parent_idx(target_idx)
#     while (@data[parent_idx] > @data[target_idx])
#       swap(parent_idx, target_idx)
#       target_idx = parent_idx
#       break if target_idx == 0
#       parent_idx = parent_idx(target_idx)
#     end
#   end

#   def top_down
#     target_idx = 0

#     # child がある場合
#     while (has_child?(target_idx))

#       a = left_child_idx(target_idx)
#       b = right_child_idx(target_idx)

#       if @data[b].nil?
#         c = a
#       else
#         c = @data[a] <= @data[b] ? a : b
#       end

#       if @data[target_idx] > @data[c]
#         swap(target_idx, c)
#         target_idx = c
#       else
#         return
#       end
#     end
#   end

#   # @param Integer
#   # @return Integer
#   def left_child_idx(idx)
#     (idx * 2) + 1
#   end

#   # @param Integer
#   # @return Integer
#   def right_child_idx(idx)
#     (idx * 2) + 2
#   end

#   # @param Integer
#   # @return Boolent
#   def has_child?(idx)
#     ((idx * 2) + 1) < (@data.size - 1)
#   end
# end



class PriorityQueue
  attr_accessor :data, :size
 
  def initialize(array = [])
    @data = []
    array.each { |a| push(a) }
  end
 
  def push(element)
    current = @data.size
    while current > 0
      p = parent(current)
      break if @data[p] <= element
      @data[current] = @data[p]
      current = p
    end
    @data[current] = element
  end
 
  def pop
    ret = @data[0]
    x = @data[-1]
    current = 0
    while have_child?(current)
      a = left_child(current)
      b = right_child(current)
      a = b if b < @data.size - 1 && @data[b] < @data[a]
      break if @data[a] >= x
      @data[current] = @data[a]
      current = a
    end
    @data[current] = x
    @data.pop
 
    ret
  end
 
  def size
    @data.size
  end
 
  def front
    @data[0]
  end
 
  def empty?
    @data.empty?
  end
 
  private
  def parent(num)
    (num - 1) / 2
  end
 
  def left_child(num)
    num * 2 + 1
  end
 
  def right_child(num)
    num * 2 + 2
  end
 
  def have_child?(num)
    left_child(num) < @data.size - 1
  end
end


N, M = gets.chomp.split(' ').map(&:to_i)

AB = []
N.times do |i|
  AB[i] = gets.chomp.split(' ').map(&:to_i)
end

group_by_day = Hash[AB.group_by{|v|v[0]}.sort]

day_fee = Array.new(M){[]}
group_by_day.each do |k,v|
  day_fee[k-1] = v.map{|j| j[1]}
end

# require 'pry'
# binding.pry

ans = 0
pq = PriorityQueue.new
day_fee.each_with_index do |fees, idx|
  break if idx > M - 1
  fees.each do |fee|
    pq.push(fee*(-1))
  end
  ans += pq.pop.to_i
  # p pq
  # p pq.pop*(-1)
end

puts ans*(-1)