import bisect

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

C = []

for _ in range(N):

    A,B = list(map(int,input().split()))

    C.append([A,B])

C = sorted(C,key=lambda x:x[0],reverse=True)

C = sorted(C,key=lambda x:x[1],reverse=True)

rev = 0

hist = list(range(0,M+1))

for x in C:

    A,B = x[0],x[1]

    ind = bisect.bisect_right(hist,M-A)

    if ind>0:

        hist.pop(ind-1)

        rev += B

print(rev)
import heapq

from collections import deque

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

A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])

A = [(-A[i][1],A[i][0]) for i in range(N)]

A = deque(A)

heap = []

cnt = 0

for i in range(M-1,-1,-1):

    k = M-i

    while len(A)>0 and A[0][1]<=k:

        heapq.heappush(heap,A.popleft())

    if len(heap)>0:

        b,a = heapq.heappop(heap)

        b = -b

        cnt += b

print(cnt)
import bisect

N, M = list(map(int, input().split()))
C = []
for _ in range(N):
    A, B = list(map(int, input().split()))
    C.append([A, B])
C = sorted(C, key=lambda x: x[0], reverse=True)
C = sorted(C, key=lambda x: x[1], reverse=True)
rev = 0
hist = list(range(0, M + 1))
for x in C:
    A, B = x[0], x[1]
    ind = bisect.bisect_right(hist, M - A)
    if ind > 0:
        hist.pop(ind - 1)
        rev += B
print(rev)

import heapq
from collections import deque

N, M = list(map(int, input().split()))
A = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
A = [(-A[i][1], A[i][0]) for i in range(N)]
A = deque(A)
heap = []
cnt = 0
for i in range(M - 1, -1, -1):
    k = M - i
    while len(A) > 0 and A[0][1] <= k:
        heapq.heappush(heap, A.popleft())
    if len(heap) > 0:
        b, a = heapq.heappop(heap)
        b = -b
        cnt += b
print(cnt)

-import bisect +import heapq +from collections import deque -C = [] -for _ in range(N): -    A, B = list(map(int, input().split())) -    C.append([A, B]) -C = sorted(C, key=lambda x: x[0], reverse=True) -C = sorted(C, key=lambda x: x[1], reverse=True) -rev = 0 -hist = list(range(0, M + 1)) -for x in C: -    A, B = x[0], x[1] -    ind = bisect.bisect_right(hist, M - A) -    if ind > 0: -        hist.pop(ind - 1) -        rev += B -print(rev) +A = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[0]) +A = [(-A[i][1], A[i][0]) for i in range(N)] +A = deque(A) +heap = [] +cnt = 0 +for i in range(M - 1, -1, -1): +    k = M - i +    while len(A) > 0 and A[0][1] <= k: +        heapq.heappush(heap, A.popleft()) +    if len(heap) > 0: +        b, a = heapq.heappop(heap) +        b = -b +        cnt += b +print(cnt)