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



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



AB_array = sorted(AB_array, key=lambda x: -x[1])



# print(AB_array)



used_array = [0] * M

next_array = [i for i in range(M)]



ans = 0



for AB in AB_array:

    A, B = AB

    # print(A, B)

    ok_flag = 0

    start = M - A

    if start < 0:

        continue

    while(ok_flag == 0):

        start = next_array[start]

        if start == -1:

            next_array[M-A] = -1

            break

        if used_array[start] == 0:

            ok_flag = 1

    else:

        ans += B

        used_array[start] = 1

        next_array[start] = start - 1

        next_array[M-A] = start - 1



print(ans)

import heapq



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



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



AB_array = sorted(AB_array, key=lambda x: x[0])





h = []



ab_index = 0

ans = 0

for m in range(1, M+1):

    while ab_index != N:

        if AB_array[ab_index][0] == m:

            heapq.heappush(h, -AB_array[ab_index][1])

            ab_index += 1

        else:

            break



    if len(h):

        ans += - heapq.heappop(h)



print(ans)

N, M = list(map(int, input().split()))
AB_array = [list(map(int, input().split())) for _ in range(N)]
AB_array = sorted(AB_array, key=lambda x: -x[1])
# print(AB_array)
used_array = [0] * M
next_array = [i for i in range(M)]
ans = 0
for AB in AB_array:
    A, B = AB
    # print(A, B)
    ok_flag = 0
    start = M - A
    if start < 0:
        continue
    while ok_flag == 0:
        start = next_array[start]
        if start == -1:
            next_array[M - A] = -1
            break
        if used_array[start] == 0:
            ok_flag = 1
    else:
        ans += B
        used_array[start] = 1
        next_array[start] = start - 1
        next_array[M - A] = start - 1
print(ans)

import heapq

N, M = list(map(int, input().split()))
AB_array = [list(map(int, input().split())) for _ in range(N)]
AB_array = sorted(AB_array, key=lambda x: x[0])
h = []
ab_index = 0
ans = 0
for m in range(1, M + 1):
    while ab_index != N:
        if AB_array[ab_index][0] == m:
            heapq.heappush(h, -AB_array[ab_index][1])
            ab_index += 1
        else:
            break
    if len(h):
        ans += -heapq.heappop(h)
print(ans)

+import heapq + -AB_array = sorted(AB_array, key=lambda x: -x[1]) -# print(AB_array) -used_array = [0] * M -next_array = [i for i in range(M)] +AB_array = sorted(AB_array, key=lambda x: x[0]) +h = [] +ab_index = 0 -for AB in AB_array: -    A, B = AB -    # print(A, B) -    ok_flag = 0 -    start = M - A -    if start < 0: -        continue -    while ok_flag == 0: -        start = next_array[start] -        if start == -1: -            next_array[M - A] = -1 +for m in range(1, M + 1): +    while ab_index != N: +        if AB_array[ab_index][0] == m: +            heapq.heappush(h, -AB_array[ab_index][1]) +            ab_index += 1 +        else: -        if used_array[start] == 0: -            ok_flag = 1 -    else: -        ans += B -        used_array[start] = 1 -        next_array[start] = start - 1 -        next_array[M - A] = start - 1 +    if len(h): +        ans += -heapq.heappop(h)