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)

def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n 
end
def array(size1,init=nil,size2=1) #size2に二次元配列時の最初の要素数、size1に次の配列の大きさ、initに初期値を投げることでその配列を返す
  if size2==1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

INF=Float::INFINITY
class PriorityQueue
    attr_accessor:queue,:n
    def initialize(n)
        @n=0
        @queue=array(n,INF)
    end

    def push(x)
        i=@n
        @n+=1
        while i>0
          p=(i-1)/2
          break if x>=@queue[p]
          @queue[i]=@queue[p]
          i=p
        end
        @queue[i]=x
    end

    def pop()
       ret=@queue[0]
       @n-=1
       x=@queue[@n]
       i=0
       while i*2+1<@n
        l=i*2+1
        r=i*2+2 
        l=r if r<@n and @queue[l]>@queue[r]
        break if x<=@queue[l]
        @queue[i]=@queue[l]
        i=l
       end
       @queue[i]=x
       return ret
    end
end

N,M=get_i
pq=PriorityQueue.new(N)
ab=Array.new(M+1){[]}
N.times do|i|
    a,b=get_i
    ab[a].push(b) if M>=a
end
ans=0
1.upto(M) do|i|
    ab[i].each do|b|
        pq.push(-b)
    end
    if pq.n>0
        b=pq.pop
        ans-=b
    end
end
puts ans