import heapq



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

Jobs = [list(map(int,input().split()))for i in range(n)]



Days = [[] for i in range(m)]

for i in range(n):

    if Jobs[i][0] <= m:

        Days[Jobs[i][0]-1] += [[-Jobs[i][1],Jobs[i][0]]]



Heap = []

heapq.heapify(Heap)

ans = 0

for i in range(m):

    for job in Days[i]:

        heapq.heappush(Heap,job)

    if Heap:

        ans -= heapq.heappop(Heap)[0]

        

print(ans)
from heapq import heappop,heappush



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

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



job.sort(key=lambda x:x[1],reverse=True)

job.sort(key=lambda x:x[0])



heap = []

ans = 0



j = -1

for i in range(M)[::-1]:

    while j < N-1 and job[j+1][0]+i <= M:

        j += 1

        heappush(heap,-job[j][1])

    if heap:

        ans += -heappop(heap)



print(ans)
import heapq

n, m = list(map(int, input().split()))
Jobs = [list(map(int, input().split())) for i in range(n)]
Days = [[] for i in range(m)]
for i in range(n):
    if Jobs[i][0] <= m:
        Days[Jobs[i][0] - 1] += [[-Jobs[i][1], Jobs[i][0]]]
Heap = []
heapq.heapify(Heap)
ans = 0
for i in range(m):
    for job in Days[i]:
        heapq.heappush(Heap, job)
    if Heap:
        ans -= heapq.heappop(Heap)[0]
print(ans)

from heapq import heappop, heappush

N, M = list(map(int, input().split()))
job = [tuple(map(int, input().split())) for _ in range(N)]
job.sort(key=lambda x: x[1], reverse=True)
job.sort(key=lambda x: x[0])
heap = []
ans = 0
j = -1
for i in range(M)[::-1]:
    while j < N - 1 and job[j + 1][0] + i <= M:
        j += 1
        heappush(heap, -job[j][1])
    if heap:
        ans += -heappop(heap)
print(ans)

-import heapq +from heapq import heappop, heappush -n, m = list(map(int, input().split())) -Jobs = [list(map(int, input().split())) for i in range(n)] -Days = [[] for i in range(m)] -for i in range(n): -    if Jobs[i][0] <= m: -        Days[Jobs[i][0] - 1] += [[-Jobs[i][1], Jobs[i][0]]] -Heap = [] -heapq.heapify(Heap) +N, M = list(map(int, input().split())) +job = [tuple(map(int, input().split())) for _ in range(N)] +job.sort(key=lambda x: x[1], reverse=True) +job.sort(key=lambda x: x[0]) +heap = [] -for i in range(m): -    for job in Days[i]: -        heapq.heappush(Heap, job) -    if Heap: -        ans -= heapq.heappop(Heap)[0] +j = -1 +for i in range(M)[::-1]: +    while j < N - 1 and job[j + 1][0] + i <= M: +        j += 1 +        heappush(heap, -job[j][1]) +    if heap: +        ans += -heappop(heap)