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)

require 'pp'
class PriorityQueue
  def initialize(array = [], &cmp)
    # topとbottomの関係を書く. デフォルトでは大きい順に出てくる
    @cmp = cmp || Proc.new { |bottom, top| bottom < top }
    @heap = []
    array.each { |a| push(a) }
  end

  def size
    @heap.size
  end

  def empty?
    @heap.size == 0
  end

  def inspect # デバック用
    tmp = Proc.new { |a,b| @cmp.call(a, b) ? 1 : 0 }
    @heap.sort(&tmp).inspect
  end

  def push(x)
    @heap << x
    index = size - 1
    while(@cmp.call(@heap[pi(index)],@heap[index]))
      @heap[pi(index)], @heap[index] = @heap[index], @heap[pi(index)]
      index = pi(index)
      break if index == 0
    end
    self
  end

  def pop
    case size
    when 0 then return nil
    when 1 then return @heap.delete_at(0)
    when 2 then return @cmp.call(@heap[1], @heap[0]) ? @heap.delete_at(0) : @heap.delete_at(1)
    end
    popped = @heap[0]
    @heap[0] = @heap.delete_at(-1)
    index = 0
    loop do
      break if lci(index) >= size || rci(index) >= size
      if @cmp.call(@heap[rci(index)], @heap[lci(index)]) && @cmp.call(@heap[index], @heap[lci(index)])
        swap!(lci(index), index)
        index = lci(index)
      elsif !(@cmp.call(@heap[rci(index)], @heap[lci(index)])) && @cmp.call(@heap[index], @heap[rci(index)])
        swap!(rci(index), index)
        index = rci(index)
      else
        break
      end
    end
    popped
  end

  alias :<< :push

  private

  def swap!(a, b)
    @heap[a], @heap[b] = @heap[b], @heap[a]
    nil
  end

  def parent_index(i)
    i.odd? ? ((i+1)/2)-1 : (i/2)-1
  end

  def left_child_index(i)
    i*2 + 1
  end

  def right_child_index(i)
    i*2 + 2
  end

  alias :pi :parent_index
  alias :lci :left_child_index
  alias :rci :right_child_index
end

pq = PriorityQueue.new { |bottom, top| bottom[1] < top[1] }
n, m = gets.split.map &:to_i
ab = []
n.times do |i|
  ab << gets.split.map(&:to_i) # 日、金
end
ab = ab.group_by{|a|a[0]}
res = 0
1.upto m do |i|
  if ab[i]
    ab[i].each{|a|pq << a}
  end
  tmp = pq.pop
  res += tmp ? tmp[1] : 0
end
p res