CodeEff/ECCO huggingface dataset:
# Original solution
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, M = list(map(int, input().split()))
    A_B = [list(map(int, input().split()))[::-1] for _ in range(N)]
    logging.info("Hello!")

    sorted_A_B = sorted(A_B, reverse=True)
    logging.info(sorted_A_B)


    total = 0
    for i in range(N):
        if i >= M:
            break
        
        if sorted_A_B[i][1] + i <= M and sorted_A_B[i][0] != -1:
            total += sorted_A_B[i][0]
            sorted_A_B[i][0] = -1

        else:
            for k in range(i, N):
                if sorted_A_B[k][1] + i <= M and sorted_A_B[k][0] != -1:
                    total += sorted_A_B[k][0]
                    sorted_A_B[k][0] = -1


    print(total)
    
if __name__ == "__main__":
    main()


# Optimized solution
import logging
import heapq

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)


def main():
    N, M = list(map(int, input().split()))
    A_B = [list(map(int, input().split())) for _ in range(N)]
    logging.info("Hello!")

    A_B.sort()
    logging.info(A_B)


    heap_Q = []
    curr = 0
    total = 0
    for i in range(1, M + 1):
        while curr < N and  A_B[curr][0] <= i:
            heapq.heappush(heap_Q, A_B[curr][1] * (-1))
            curr += 1

        if heap_Q:
            total += (-1) * heapq.heappop(heap_Q)

    print(total)
    
if __name__ == "__main__":
    main()
# Original solution
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
aa = [[] for i in range(m)]
ans = 0
for i in range(n):
    a,b = inpl()
    if a <= m:
        aa[a-1].append(b)
for i in range(m):
    if aa[i]:
        aa[i].sort()
        ans += aa[i].pop()
        if i < m-1:
            for j in aa[i]:
                aa[i+1].append(j)
print(ans)




# Optimized solution
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
aa = [[] for i in range(m)]
ans = 0
max_b = [0] * m
for i in range(n):
    a,b = inpl()
    if a <= m:
        aa[a-1].append(b)
for i in range(m):
    if aa[i]:
        max_b[i] = max(aa[i])

for i in range(m):
    mmm = 0
    max_i = -1
    for j in range(i+1):
        if max_b[j] > mmm:
            mmm = max_b[j]
            max_i = j
    ans += mmm
    if len(aa[max_i]) and max_i != -1:
        aa[max_i].remove(mmm)
        if aa[max_i]:
            max_b[max_i] = max(aa[max_i])
        else :
            max_b[max_i] = 0
print(ans)
# Original solution
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
aa = [[] for i in range(m)]
ans = 0
max_b = [0] * m
for i in range(n):
    a,b = inpl()
    if a <= m:
        aa[a-1].append(b)
for i in range(m):
    if aa[i]:
        max_b[i] = max(aa[i])

for i in range(m):
    mmm = 0
    max_i = -1
    for j in range(i+1):
        if max_b[j] > mmm:
            mmm = max_b[j]
            max_i = j
    ans += mmm
    if len(aa[max_i]) and max_i != -1:
        aa[max_i].remove(mmm)
        if aa[max_i]:
            max_b[max_i] = max(aa[max_i])
        else :
            max_b[max_i] = 0
print(ans)




# Optimized solution
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

ans = 0
hq = []
n,m = inpl()
ab = [[] for i in range(m)]
for i in range(n):
    a,b = inpl()
    if a <= m:
        ab[a-1].append(b)

heapq.heapify(hq)
for i in range(m):
    for j in ab[i]:
        heapq.heappush(hq,-j)
    if hq:
        ans -= heapq.heappop(hq)
print(ans)
# Original solution
from collections import deque
import sys
input = sys.stdin.readline

def main():
    N,M = list(map(int,input().split()))
    paylist = [[] for _ in range(M)]
    for _ in range(N):
        a,b = list(map(int,input().split()))
        if M >= a:
            paylist[a-1].append(b)

    work = []
    payment = 0
    for i in range(M):
        work += paylist[i]
        work.sort()
        if work != []:
            payment += work.pop()

    print(payment)

if __name__ == "__main__":
    main()

# Optimized solution
import heapq
import sys
input = sys.stdin.readline

def main():
    N,M = list(map(int,input().split()))
    paylist = [[] for _ in range(M)]
    for _ in range(N):
        a,b = list(map(int,input().split()))
        if M >= a:
            heapq.heappush(paylist[a-1],-b)
    work = []
    payment = 0
    for i in range(M):
        for j in paylist[i]:
            heapq.heappush(work,j)
        if work:
            payment += heapq.heappop(work)

    print((-payment))

if __name__ == "__main__":
    main()
# Original solution
n,m = list(map(int,input().split()))
lis = [[] for _ in range(10**5)]
for _ in range(n):
  a,b = list(map(int,input().split()))
  lis[a-1].append(b)
ans = 0
li = []
for i in range(m):
  li += lis[i]
  li.sort(reverse=True)
  try:
    ans += li[0]
    del(li[0])
  except:
    continue
print(ans)

# Optimized solution
from heapq import heappush,heappop,heapify

n,m = list(map(int,input().split()))
lis = [[] for i in range(m+1)]

for _ in range(n):
    a,b = list(map(int,input().split()))
    if a <= m:
        lis[a].append(-b)

ans = 0
num = []
heapify(num)

for j in range(1,m+1):
    for nu in lis[j]:
        heappush(num,nu)
    if len(num) > 0:
        work = heappop(num)
        ans += -work
print(ans)
# Original solution
import heapq

n,m = list(map(int,input().split()))
lb = [[] for _ in range(10**5+5)]
for _ in range(n):
    a,b = list(map(int,input().split()))
    lb[a].append(b)
res = 0
q = []
heapq.heapify(q)
for i in range(1,m+1):
    for b in lb[i]:
        heapq.heappush(q,b * (-1))
    if q:
        res += heapq.heappop(q) * (-1)

print(res)

# Optimized solution
import heapq
def main():
    n,m = list(map(int,input().split()))
    lb = [[] for _ in range(10**5+5)]
    for _ in range(n):
        a,b = list(map(int,input().split()))
        lb[a].append(b)
    res = 0
    q = []
    heapq.heapify(q)
    for i in range(1,m+1):
        for b in lb[i]:
            heapq.heappush(q,b * (-1))
        if q:
            res += heapq.heappop(q) * (-1)

    print(res)

main()
# Original solution
#coding utf-8
import heapq
N,M=list(map(int,input().split()))
AB= [[int(i) for i in input().split()] for i in range(N)]
work=[]
heapq.heapify(work)
ans=0
for i in range(1,M+1):
  for j in range(N):
    if AB[j][0]==i:
      heapq.heappush(work,-AB[j][1])
  if work:
    tmp=heapq.heappop(work)
    ans-=tmp
  
print(ans)

# Optimized solution
#coding utf-8
import heapq
N,M=list(map(int,input().split()))
AB= [[int(i) for i in input().split()] for i in range(N)]
AB.sort(reverse=True)
work=[]
heapq.heapify(work)
ans=0
for i in range(1,M+1):
  while AB and AB[-1][0] <=i:
      tmp=AB.pop()
      heapq.heappush(work,-tmp[1])
  if work:
    tmp=heapq.heappop(work)
    ans-=tmp
  
print(ans)
# Original solution
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 02:06:07 2020

@author: Kanaru Sato
"""

import heapq

n,m = list(map(int, input().split()))
albeit = []
for i in range(m+1):
    albeit.append([])

for i in range(n):
    a,b = list(map(int, input().split()))
    if a <= m:
        albeit[a].append(-1*b)

##print(albeit)

for i in range(m):
    heapq.heapify(albeit[i+1])

##print(albeit)

sum = 0
for i in range(m):
    day = i+1 #day:1->m
    ##print("day=",day)
    minlist = []
    for j in range(day):
        if len(albeit[j+1]) >= 1:
            minlist.append(albeit[j+1][0])
        else:
            minlist.append(0)
    ##print(minlist)
    m = min(minlist)
    if m != 0:
        Maxalbeitindex = minlist.index(m)
        sum += -1*heapq.heappop(albeit[Maxalbeitindex+1])
    ##print(sum)

print(sum)

# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 02:06:07 2020

@author: Kanaru Sato
"""

import heapq

n,m = list(map(int, input().split()))
albeit = []
for i in range(m+1):
    albeit.append([])

for i in range(n):
    a,b = list(map(int, input().split()))
    if a <= m:
        albeit[a].append(-1*b)

##print(albeit)
Heap = [0]
sum = 0
for i in range(m):
    day = i+1
    for money in albeit[day]:
        heapq.heappush(Heap, money)
    if Heap[0] != 0:
        daymax = heapq.heappop(Heap)*-1
        sum += daymax

print(sum)
# Original solution
import heapq

n, m = list(map(int, input().split()))
ba=[]
for i in range(n):
    a, b = list(map(int, input().split()))
    ba.append([-b, a])
heapq.heapify(ba)

ans=0

for i in range(m - 1, -1, -1):
    not_use = []
    while ba:
        l = heapq.heappop(ba)
        if l[1] + i <= m:
            ans += l[0] * -1
            break
        else:
            not_use.append(l)
    for j in not_use:
        heapq.heappush(ba, j)
print(ans)    

# Optimized solution
import heapq

n, m = list(map(int, input().split()))
l = [[] for _ in range(m)]
 
for i in range(n):
    a, b = list(map(int, input().split()))
    if a <= m:
        l[a-1].append(b)

a = []
ans = 0

for i in range(m):
    for j in l[i]:
        heapq.heappush(a, (-1) * j)
    if a:
        ans += heapq.heappop(a) * (-1)
        
print(ans)
# Original solution
import heapq

n ,m = list(map(int,input().split()))
li = []
for i in range(n):
    x, y= list(map(int,input().split()))
    li.append([x,y])
li.sort()
heap = []

ans = 0
for i in range(m):
    bar = True
    while bar:
        if len(li) == 0:
            break
        if i >= li[0][0] - 1:
            price = li.pop(0)
            heapq.heappush(heap,-price[1])
        else:
            bar = False
    try:
        ans += -heapq.heappop(heap)
    except IndexError:
        ans += 0
 
print(ans)

# Optimized solution
import heapq

n ,m = list(map(int,input().split()))
li = []
for i in range(n):
    x, y= list(map(int,input().split()))
    li.append([x,y])
li.sort()
heap = []

ans = 0
j = 0
for i in range(1, m + 1):
    while j < n and li[j][0] <= i:
        heapq.heappush(heap,-li[j][1])
        j += 1
    if heap:
        ans += -heapq.heappop(heap)
 
print(ans)
# Original solution
n,m = list(map(int,input().split()))
work = [list(map(int, input().split())) for i in range(n)]
from operator import itemgetter
work = sorted(work,key=itemgetter(1),reverse=True)
count = 0
for i in range(1,m+1):
  for j in range(len(work)):
    if work[j][0] <= i:
      count += work[j][1]
      del work[j]
      break
print(count)



# Optimized solution
import heapq

n,m = list(map(int,input().split()))

a = [0] * n
b = [0] * n
info = [[] for _ in range(m+1)]

for i in range(n):
    a[i], b[i] = list(map(int,input().split()))
    if a[i] <= m:
        info[a[i]].append(b[i])

l = []
count = 0

for j in range(1,m+1):
    for x in info[j]:
        heapq.heappush(l,-x)
    if l != []:
        count += -heapq.heappop(l)

print(count)
# Original solution
import heapq

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)


# Optimized solution
import heapq

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
# AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)
# Original solution
import heapq

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
# AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)


# Optimized solution
import heapq

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

pay_list = []
heapq.heapify(pay_list)
ans = 0
AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    # 実行可能なバイトリストを抽出
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    # 最大の報酬の値を取り出して足す
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)
# Original solution

N,M = list(map(int,input().split()))
AB = [list(map(int,input().split())) for _ in range(N)]
import heapq
AB.sort()
heap = []
heapq.heapify(heap)
ans = 0
i = 0
day = 1
while day <= M:
    while i < N and AB[i][0] <= day:
        heapq.heappush(heap,-AB[i][1])
        i += 1
    day += 1
    if not(heap):continue
    ans -= heapq.heappop(heap)

print(ans)

# Optimized solution
import heapq


N,M = list(map(int,input().split()))
AB = [list(map(int,input().split())) for _ in range (N)]

AB.sort()
ans = 0 
q = []
heapq.heapify(q)
idx = 0
for i in range(1,M+1):
    while idx<N and AB[idx][0]<=i:
        heapq.heappush(q, -AB[idx][1])
        idx += 1
    if q:
        p = -heapq.heappop(q)
        ans += p
print(ans)
# Original solution


import collections
 
N,M = list(map(int,input().split()))

allhiyatoi = [[] for j in range(M+1)]
for i in range(N):
    temp = list(map(int,input().split()))
    if temp[0] > M:
        continue
    allhiyatoi[temp[0]].append(temp[1])

temp = []
for i in allhiyatoi:
    temp.append(collections.deque(sorted(i)))

allhiyatoi = temp
 
reward = 0
for i in range(M+1):
    D = -1
    maxrw = -1
    for j,day in enumerate(allhiyatoi):
        if j > i:
            break
        if len(day) == 0:
            continue
        rw = max(day)
        if maxrw < rw:
            D = j
            maxrw = rw
    if D == -1:
        continue
    reward += allhiyatoi[D].pop()

print(reward)

# Optimized solution
from heapq import heappush, heappop
 
N,M = list(map(int,input().split()))

allhiyatoi = [[] for j in range(M+1)]
for i in range(N):
    temp = list(map(int,input().split()))
    if temp[0] > M:
        continue
    allhiyatoi[temp[0]].append(temp[1])
 
reward = 0
Q = []
for i in allhiyatoi:
    for j in i:
        heappush(Q,j*-1)
    if len(Q) == 0:
        continue
    reward -= (heappop(Q))

print(reward)
# Original solution
#解説の方法
N,M = list(map(int,input().split()))
AB = [[] for _ in range(10**5+1)]#報酬振り込み日数ごとに報酬を格納
for _ in range(N):
    a,b = list(map(int,input().split()))
    AB[a].append(b)
q = []
ans =0
for i in range(1,M+1):
    q += AB[i]#後ろから見るイメージ　残りi日で請けれる仕事をリストに追加する
    if len(q)>0:
        q.sort()
        ans += q.pop()
print(ans)



# Optimized solution
import heapq
#解説の方法
N,M = list(map(int,input().split()))
AB = [[] for _ in range(10**5+1)]#報酬振り込み日数ごとに報酬を格納
for _ in range(N):
    a,b = list(map(int,input().split()))
    AB[a].append(-b)
q = []
heapq.heapify(q)
ans =0
for i in range(1,M+1):
    for b in AB[i]:
        heapq.heappush(q,b)
    if len(q)>0:
        ans += -heapq.heappop(q)
print(ans)
# Original solution

class Job(object):
    def __init__(self, day, reward):
        self.day = day
        self.reward = reward
        self.is_used = False


class MyJobs(object):
    def __init__(self, num_elements_in_array):
        self.heapm = {}

    def append(self, priority, value):
        if not priority in list(self.heapm.keys()):
            self.heapm[priority] = []
        self.heapm[priority].append(value)

    def get_best(self, priority):
        day = -1
        reward = -1

        for i in range(priority + 1):
            if not (i in list(self.heapm.keys())):
                continue
            for j in range(len(self.heapm[i])):
                if self.heapm[i][j].is_used:
                    continue
                cur = self.heapm[i][j].reward
                if not (0 > day):
                    best = self.heapm[day][reward].reward
                if (0 > day) or (cur > best):
                    day = i
                    reward = j
        if (0 > day):
            return 0
        self.heapm[day][reward].is_used = True
        return self.heapm[day][reward].reward


(n, m) = list(map(int, input().split()))
jobs = MyJobs(m)
for _ in range(n):
    (a, b) = list(map(int, input().split()))
    jobs.append(a - 1, Job(a - 1, b))

sum = 0
for i in range(m):
    sum += jobs.get_best(i)
print(sum)


# Optimized solution
import heapq

(n, m) = list(map(int, input().split()))
jobs = {}
for _ in range(n):
    (a, b) = list(map(int, input().split()))
    if not a in jobs:
        jobs[a] = []
    jobs[a].append(-b)

heap = []
sum = 0
for i in range(1, m + 1):
    if i in jobs:
        for j in jobs[i]:
            heapq.heappush(heap, j)
    if 0 < len(heap):
        sum -= heapq.heappop(heap)
print(sum)
# Original solution
from queue import PriorityQueue
N, M = list(map(int, input().split()))  # N件の仕事, M日後までに得られる報酬

jobs = [[] for _ in range(M)]  # 締め切りごとに仕事を格納する二重配列

for i in range(N):  # N件の仕事を見て行く
    a, b = list(map(int, input().split()))
    if a > M:  # a日後にもらえる、そのaが期日のMよりも大きかったらスルー
        continue
    jobs[M-a].append(b)
    # 報酬がもらえるまでに時間がかかる仕事は、小さいindexに格納されていく
    # PriorityQueueに追加するときはjobsの後ろから見ていく

ans = 0
pq = PriorityQueue()

for i in range(M-1, -1, -1):  # jobsの後ろから見ていく
    for b in jobs[i]:
        pq.put(-b)  # min-heapだからマイナスに追加する

    if not pq.empty():
        ans += -(pq.get())

print(ans)


# Optimized solution
from queue import PriorityQueue
N, M = list(map(int, input().split()))  # N件のアルバイト, M日後までに欲しい

jobs_list = [[] for _ in range(M+1)]

for i in range(N):
    a, b = list(map(int, input().split()))
    if a > M:  # M日後にもらえる仕事ならok
        continue
    jobs_list[a].append(b)

pq = PriorityQueue()

ans = 0

for jobs in jobs_list:
    for job in jobs:
        pq.put(-job)  # min-heapであるから、マイナスにして追加する
    if not pq.empty():  # pqに要素があるなら仕事を一つ行う
        ans += -(pq.get())

print(ans)
# Original solution
N, M = list(map(int, input().split()))

AB = {}
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a in AB:
        AB[a].append(b)
    else:
        AB[a] = [b]

count = 0
current = []

for day in range(1, M + 1):
    if day in AB:
        current.extend(AB[day])
        current.sort(reverse=True)

    #print("day{}:{}".format(day, current))
    if len(current) != 0:
        count += current.pop(0)

print(count)




# Optimized solution
import heapq
N, M = list(map(int, input().split()))

AB = {}
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a in AB:
        AB[a].append(b)
    else:
        AB[a] = [b]

count = 0
current = []

for day in range(1, M + 1):
    if day in AB:
    	for work in AB[day]:
            heapq.heappush(current, -work)

    #print("day{}:{}".format(day, current))
    if len(current) > 0:
        count += -heapq.heappop(current)

print(count)
# Original solution
import bisect
n,m = list(map(int,input().split()))
data = [tuple(map(int,input().split())) for i in range(n)]
data.sort(key = lambda tup:tup[0])
salary =[]
ans=0
prev=0
memo=0
for day in range(m):
	for i in range(memo,n):
		if data[memo][0]<=(day+1):
			bisect.insort(salary,data[memo][1])
			# salary.append(data[memo][1])
			memo+=1
		else:
			break

	# ans+=salary.pop(max)
	if len(salary)!=0:
		ans+=salary.pop()
print(ans)	

# Optimized solution
import heapq
n,m = list(map(int,input().split()))
AB =[tuple(map(int,input().split())) for i in range(n)]
ans,memo=0,0
hq=[]
AB.sort(key=lambda tup:tup[0])
for day in range(m):
	for i in range(memo,n):
		if AB[i][0]<=(day+1):
			heapq.heappush(hq,-AB[i][1])
			memo+=1
		else:
			break
	if len(hq):	
		ans += -heapq.heappop(hq)
print(ans)
# Original solution
import heapq

N, M = list(map(int, input().split()))
AB = {}
q = []
ans = 0

for i in range(N):
    A, B = list(map(int, input().split()))
    
    if A in AB:
        AB[A].append(-B)
    else:
        AB[A] = [-B]

for i in range(1, M+1):
    if i in AB:
        for B in AB[i]:
            heapq.heappush(q, B)
        
    if len(q):
        ans -= heapq.heappop(q)
    
print(ans)    

# Optimized solution
import sys
from heapq import heappush, heappop

input = sys.stdin.readline

def main():
    N, M = list(map(int, input().split()))
    A, B = [[] for i in range(M)], []
    
    for i in range(N):
        a, b = list(map(int, input().split()))
        if a > M: continue
    
        A[a-1].append(-b)
        
    ans = 0
    
    for i in range(M):
        for a in A[i]: heappush(B, a)
        
        if not B: continue
    
        ans += heappop(B)
        
    print((-ans))
    
if __name__ == '__main__':
    main()
# Original solution
from heapq import*
(N,M),*t=[list(map(int,s.split()))for s in open(0)]
z,*q=[0]*99*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
for i in v[:M]:
 for j in i:heappush(q,-j)
 z-=heappop(q)
print(z)

# Optimized solution
from heapq import*
(N,M),*t=[list(map(int,s.split()))for s in open(0)]
z,*q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
for i in v[:M]:
 for j in i:heappush(q,-j)
 z-=heappop(q)
print(z)
# Original solution
from heapq import*
(N,M),*t=[list(map(int,s.split()))for s in open(0)]
z,*q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
for i in v[:M]:[heappush(q,-j)for j in i];z-=heappop(q)
print(z)

# Optimized solution
from heapq import*
(N,M),*t=[list(map(int,s.split()))for s in open(0)]
q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
print((sum(([heappush(q,-j)for j in i],-heappop(q))[1]for i in v[:M])))
# Original solution
from collections import deque

N, M = list(map(int,input().split()))

class WorkList:
    def __init__(self):
        self.payList = [[] for _ in range(10001)]
        self.payList_sorted = []

    def add(self, a, b):
        if a <= M:
            self.payList[10001 - b].append(a)

    def sort_task(self):
        self.payList_sorted = [deque(sorted(l)) for l in self.payList]

    def get_task(self, deadline):
        for i, q in enumerate(self.payList_sorted):
            if len(q) > 0 and q[0] <= deadline:
                q.popleft()
                return 10001 - i
        return 0


w = WorkList()
for _ in range(N):
    A, B = list(map(int, input().split()))
    w.add(A, B)

w.sort_task()

ans = 0

for d in range(M + 1):
    ans += w.get_task(d)

print(ans)

# Optimized solution
import bisect
from collections import deque

N, M = list(map(int,input().split()))

work = [list(map(int,input().split())) for _ in range(N)]

wq = deque(sorted(work))

paytree = []

ans = 0

for day in range(M + 1):
    while len(wq) > 0 and wq[0][0] <= day:
        bisect.insort_right(paytree,wq.popleft()[1])
    if len(paytree) > 0:
        ans += paytree.pop(len(paytree) - 1)

print(ans)