CodeEff/ECCO huggingface dataset:
# Original solution
import heapq
from collections import defaultdict
N,M = list(map(int,input().split()))
AB = defaultdict(list)
for i in range(N):
    a,b = list(map(int,input().split()))
    if a not in AB:
        AB[a] = [-b]
    else:
        AB[a] += [-b]
work = 0
AB2 = []
heapq.heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heapq.heappush(AB2,j)
    if not AB2:
        continue
    b = heapq.heappop(AB2)
    work += -b
print(work)

# Optimized solution
import heapq
N,M = list(map(int,input().split()))
AB = [[] for i in range(M+1)]
for i in range(N):
    a,b = list(map(int,input().split()))
    if a <= M:
        AB[a].append(-b)
work = 0
AB2 = []
heapq.heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heapq.heappush(AB2,j)
    if not AB2:
        continue
    b = heapq.heappop(AB2)
    work += -b
print(work)
# Original solution
import heapq
N,M = list(map(int,input().split()))
AB = [[] for i in range(M+1)]
for i in range(N):
    a,b = list(map(int,input().split()))
    if a <= M:
        AB[a].append(-b)
work = 0
AB2 = []
heapq.heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heapq.heappush(AB2,j)
    if len(AB2) > 0:
        b = heapq.heappop(AB2)
        work += -b
print(work)

# Optimized solution
import sys
import heapq
input = sys.stdin.readline
N,M = list(map(int,input().split()))
AB = [[] for i in range(M+1)]
for i in range(N):
    a,b = list(map(int,input().split()))
    if a <= M:
        AB[a].append(-b)
work = 0
AB2 = []
heapq.heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heapq.heappush(AB2,j)
    if len(AB2) > 0:
        b = heapq.heappop(AB2)
        work += -b
print(work)
# Original solution
from heapq import heapify, heappush, heappop
N,M = list(map(int,input().split()))
AB = []
for i in range(N):
    a,b = list(map(int,input().split()))
    AB.append((-b,a))
heapify(AB)
ans = 0
for i in range(1,M+1):
    rem = []
    while True:
        if AB:
            ab = heappop(AB)
            if ab[1] <= i:
                ans -= ab[0]
                for j in rem:
                    heappush(AB,j)
                break
            else:
                rem.append(ab)
        else:
            for j in rem:
                heappush(AB,j)
            break
print(ans)

# Optimized solution
from heapq import heapify, heappush, heappop
N,M = list(map(int,input().split()))
AB = [[] for i in range(M+1)]
for i in range(N):
    a,b = list(map(int,input().split()))
    if a <= M:
        AB[a].append(-b)
AB2 = []
work = 0
heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heappush(AB2,j)
    if len(AB2):
        b = heappop(AB2)
        work -= b
print(work)
# Original solution
def main():
    import heapq
    n,m = list(map(int,input().split()))
    alb = {}
    for i in range(n):
        a,b = list(map(int,input().split()))
        if a not in list(alb.keys()):
            alb[a] = [b]
        else:
            alb[a] += [b]
    h = []
    s = 0
    for i in range(1,m+1):
        if i in list(alb.keys()):
            for j in range(len(alb[i])):
                heapq.heappush(h, -1*alb[i][j])
        if len(h)>0:
            m = heapq.heappop(h)
            s += -1*m
    print(s)

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    import heapq
    n,m = list(map(int,input().split()))
    jobs = {}
    for i in range(n):
        a,b = list(map(int,input().split()))
        if a not in list(jobs.keys()):
            jobs[a] = [b]
        else:
            jobs[a] += [b]
    ob = []
    ans = 0
    for i in range(1,m+1):
        if i in list(jobs.keys()):
            for j in range(len(jobs[i])):
                heapq.heappush(ob,-1*jobs[i][j])
        if len(ob)>0:
            ans += heapq.heappop(ob)*-1
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
import heapq

n, m = list(map(int,input().split()))
joblist = []
for i in range(n):
    a,b = list(map(int,input().split()))
    joblist.append([a,b])
joblist.sort(key=lambda x :x[0])
kouho = []
 
ans =0
 
for i in range(1,m+1):
    count=0
    if joblist:
        for j,job in enumerate(joblist):
            if job[0]==i:
                
                heapq.heappush(kouho,(-1*job[1]))
                
                
            if job[0]!=i:
                if count:
                    for _ in range(count-1):
                        joblist.pop(0)
        if kouho:
            ans += -1*heapq.heappop(kouho)
 
print(ans)

# Optimized solution
import heapq

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

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

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

N, M = LIST()

A = [[] for _ in range(10**4+1)]
for _ in range(N):
    a, b = LIST()
    if a <= M:
        A[b].append(a)

for a in A:
    a.sort(reverse=True)

off = [False] * M
ans = 0

i = 10**4
while i > 0:
    for a in A[i]:
        j = M - a
        while j >= 0:
            if off[j] == False:
                off[j] = True
                ans += i
                break;
            else:
                j -= 1

    if off == [True] * M:
        break;
    else:
        i -= 1

print(ans)


# Optimized solution
import sys
import bisect

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

N, M = LIST()

A = [[] for _ in range(10**4+1)]
for _ in range(N):
    a, b = LIST()
    if a <= M:
        A[b].append(a)

for a in A:
    a.sort(reverse=True)

off = list(range(1, M+1))
ans = 0

i = 10**4
while i > 0:
    for a in A[i]:
        attempt = bisect.bisect_left(off, a)

        if attempt < len(off):
            off.pop(attempt)
            ans += i

    if off == []:
        break;
    else:
        i -= 1

print(ans)
# Original solution
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print((k.join(list(map(str, lst)))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
import heapq


def solve():
    n, m = MI()

    AB = []
    for i in range(n):
        a, b = MI()
        AB.append((a, b))

    AB = sorted(AB, key=lambda x: x[0])
    # print(AB)

    q = [AB[0][1]]
    pre = AB[0][0]
    memo = {}
    for i in range(1, n):
        a, b = AB[i]
        if a == pre:
            q.append(b)
        else:
            memo[pre] = deque(sorted(q))
            q = [b]
            pre = a
    memo[pre] = deque(sorted(q))

    # print(memo)

    ans = 0
    for i in range(1, m+1):
        q = memo.get(i, deque([]))

        # print(i, hp)
        if len(q) > 0:
            ans += q.pop()

        q.extend(memo.get(i + 1, []))

        memo[i+1] = deque(sorted(q)[-m-i:])
    print(ans)


if __name__ == '__main__':
    solve()


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print((k.join(list(map(str, lst)))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappush, heappop


def solve():
    n, m = MI()

    jobs = [[] for _ in range(m)]
    for i in range(n):
        a, b = MI()
        if m-a >= 0:
            jobs[m-a].append(b)

    # print(jobs)
    hp = []
    heapify(hp)
    ans = 0
    for i in range(m-1, -1, -1):
        js = jobs[i]
        for j in js:
            heappush(hp, -j)

        if len(hp) > 0:
            ans += -heappop(hp)
    print(ans)





if __name__ == '__main__':
    solve()
# Original solution
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect



def main():
    num, max_num = list(map(int, input().split()))
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x:(-x[1]))
    # for


    visit = [0 for i in range(max_num + 1)]

    ans = 0
    # print(data)
    for i in range(num):
        day, value = data[i]
        ind_lim = day
        try:
            ind_insert = visit[ind_lim:].index(0)
            visit[ind_insert + ind_lim] = 1
            ans += value
        except:
            pass
        # print(visit)

    print(ans)







if __name__ == '__main__':
    main()


# Optimized solution
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect


visit = []

def aaa(ind_lim, dame_num):
    global visit
    if ind_lim >= dame_num:
        return -1
    elif visit[ind_lim] == -1:
        return -1
    elif visit[ind_lim] == 0:
        return ind_lim
    else:
        visit[ind_lim] = aaa(visit[ind_lim], dame_num)
        return visit[ind_lim]

def main():
    num, max_num = list(map(int, input().split()))
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x:(-x[1]))
    # for

    global visit
    visit = [0 for i in range(max_num + 1)]

    ans = 0

    for i in range(num):
        day, value = data[i]
        ind_lim = day
        # print(i, num, visit)
        ind_insert = aaa(ind_lim, max_num + 1)
        if ind_insert != -1:
            visit[ind_insert] = ind_insert + 1
            ans += value

    # print('uuu')
    print(ans)







if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        cur+= ref
        cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )


# Optimized solution
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
for i in range(m):
    if baito[i]: baito[i].sort()
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur:
            cur= ref
        else:
            if ref[-1]<=cur[0]:
                cur= ref + cur
            elif cur[-1]<=ref[0]:
                cur= cur + ref
            else:
                cur+= ref
                cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )
# Original solution
#!/usr/bin/env python

import sys
import heapq

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(-bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur: cur= ref
        else: cur+= ref
        heapq.heapify(cur)
    #
    if not cur: continue
    tot+= heapq.heappop(cur)
#
print(( -tot ))


# Optimized solution
#!/usr/bin/env python

import sys
import heapq

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(-bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref: heapq.heapify(ref)
    while ref: heapq.heappush(cur,heapq.heappop(ref))
    #
    if not cur: continue
    tot+= heapq.heappop(cur)
#
print(( -tot ))
# Original solution
import heapq
n, m = list(map(int, input().split()))
byte = [[] for _ in range(100001)]

for _ in range(n):
    a, b = list(map(int, input().split()))
    byte[a].append(b)
search = []
day = 1
ans = 0
while day-1 != m:
    for b in byte[day]:
        heapq.heappush(search, -b)
    if not search:
        day += 1
        continue
    tmp = -heapq.heappop(search)
    ans += tmp
    day += 1

print(ans)





# Optimized solution
import heapq
n, m = list(map(int, input().split()))
byte = [[] for _ in range(m+2)]

for _ in range(n):
    a, b = list(map(int, input().split()))
    if a > m+1:
        continue
    byte[a].append(b)
search = []
day = 1
ans = 0
while day-1 != m:
    for b in byte[day]:
        heapq.heappush(search, -b)
    if not search:
        day += 1
        continue
    tmp = -heapq.heappop(search)
    ans += tmp
    day += 1

print(ans)
# Original solution
# D - Summer Vacation
from heapq import *
from collections import defaultdict

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

result = 0
pool = []
for i in range(1, m+1):
    for j in jobs[i]:
        heappush(pool, j)
    if 0 < len(pool):
        result += -heappop(pool)

print(result)

# Optimized solution
import sys
from heapq import heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
# M-1日後から選んでいく,
for _ in range(N):
    a, b = lr()
    if a > M:
        continue
    works[a].append(-b)

heap = []
answer = 0
for i in range(1, M+1):
    for x in works[i]:
        heappush(heap, x)
    if len(heap) == 0:
        continue
    answer += heappop(heap)

answer *= -1
print(answer)
# 08
# Original solution
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
#Bが報酬
AB = [(a, b) for a, b in AB if a<=M]
AB.sort(reverse=True, key=lambda x: x[1])
AB = deque(AB)
answer = 0
used = set()
out = set()
while AB:
    x, y = AB.popleft()
    if x > M or x in out:
        continue
    else:
        z = x
        while z in used:
            z += 1
        if z > M:
            out.add(x)
            continue
        used.add(z)
        answer += y

print(answer)
        

# Optimized solution
import sys
from heapq import heappush, heappop

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    if a <= M:
        works[a].append(b)

cand = []
answer = 0
for i in range(1, M+1):
    for work in works[i]:
        heappush(cand, -work)
    if cand:
        answer -= heappop(cand)

print(answer)
# Original solution
import sys
from heapq import heappush, heappop, merge, heapify

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    if a <= M:
        works[a].append(-b)

cand = []
answer = 0
for i in range(1, M+1):
    cand = list(merge(cand, works[i]))
    heapify(cand)
    if cand:
        answer -= heappop(cand)

print(answer)


# Optimized solution
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
jobs = [[] for _ in range(M+1)]
for _ in range(N):
    a, b = lr()
    if a > M:
        continue
    jobs[a].append(b)

answer = 0
candidate = []
for i in range(1, M+1):
    for job in jobs[i]:
        heappush(candidate, -job)
    if candidate:
        answer += heappop(candidate)

answer *= -1
print(answer)
# 31
# Original solution
from collections import defaultdict, deque
import sys
import math

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


def main():
    # N = int(input())
    N, M = [int(a) for a in input().split()]
    AB = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    AB = sorted(AB, key=lambda it: it[1])

    ARR = [[] for _ in range(M + 1)]
    for a, b in AB:
        if a > M:
            continue
        ARR[a].append(b)
    # print("ARR", ARR)

    res = 0
    q = []
    for i in range(1, M + 1):
        current = ARR[i]
        if len(current) != 0:
            if len(q) != 0:
                if current[-1] > q[-1]:
                    res += current.pop()
                else:
                    res += q.pop()
            else:
                res += current.pop()
            q += current
            q.sort()
            # q = sorted(q + current)
            q = q[-(M - i):]
        else:
            if len(q) != 0:
                res += q.pop()
            else:
                pass
    print(res)


if __name__ == '__main__':
    main()


# Optimized solution
from collections import defaultdict, deque
import sys
import heapq
import math

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


def main():
    # N = int(input())
    N, M = [int(a) for a in input().split()]
    AB = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    # AB = sorted(AB, key=lambda it: it[1])

    ARR = [[] for _ in range(M + 1)]
    for a, b in AB:
        if a > M:
            continue
        ARR[a].append(b)
    # print("ARR", ARR)

    res = 0
    q = []
    for i in range(1, M + 1):
        current = ARR[i]
        for a in current:
            heapq.heappush(q, -a)
        if len(q) != 0:
            res -= heapq.heappop(q)
    print(res)


if __name__ == '__main__':
    main()
# Original solution
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

from itertools import accumulate, permutations, combinations, takewhile, compress, cycle

# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint


INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))

N, M = [int(x) for x in input().split()]
AB = []
for _ in range(N):
    A, B = [int(x) for x in input().split()]
    AB.append((A, B))

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

ans = 0
used = set()
for m in range(M, -1, -1):
    for i in range(N):
        a, b = AB[i]
        if a + m <= M and i not in used:
            ans += b
            used.add(i)
            # print("m:{}, a:{},b{}".format(m, a, b))
            break

print(ans)


# Optimized solution
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

from itertools import accumulate, permutations, combinations, takewhile, compress, cycle

# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint
import heapq

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))

N, M = [int(x) for x in input().split()]
AB = []
for _ in range(N):
    A, B = [int(x) for x in input().split()]
    AB.append((A, B))

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

idx = 0
available = []
ans = 0
for d in range(1, M + 1):
    while idx < N and AB[idx][0] <= d:
        heapq.heappush(available, -AB[idx][1])
        idx += 1

    if len(available) != 0:
        ans += (-heapq.heappop(available))

print(ans)
# Original solution
import math, heapq
from operator import itemgetter
from collections import defaultdict
INF = float("inf")
MOD = int(1e9 + 7)

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

ab = sorted(AB)

v, que = 0, []
for n in range(1, M + 1):
    while ab:
        a, b = ab.pop(0)
        if a <= n:
            heapq.heappush(que, -b)
        else:
            ab.insert(0, (a, b))
            break
    if que:
        v += -heapq.heappop(que)
print(v)


# Optimized solution
import math, heapq
from operator import itemgetter
from collections import defaultdict
INF = float("inf")
MOD = int(1e9 + 7)

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

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

    v, que = 0, []
    for m in range(1, M + 1):
        if m in AB:
            for b in AB[m]:
                heapq.heappush(que, -b)
        if que:
            v += -heapq.heappop(que)
    print(v)

main()
# Original solution
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    q = deque()
    for _ in range(N):
        a, b = list(map(int, input().split()))
        if a > M:
            continue
        q.append((a, b))

    ans = 0
    cnt = 1
    while cnt <= M:
        q_ = deque()
        max_a, max_b = 0, 0
        while q:
            a, b = q.pop()
            if a > cnt:
                q_.append((a, b))
                continue
            elif b > max_b:
                q_.append((max_a, max_b))
                max_a, max_b = a, b
            else:
                q_.append((a, b))

        ans += max_b
        cnt += 1
        q = q_


    return ans


if __name__ == '__main__':
    print((main()))


# Optimized solution
import sys
from collections import defaultdict
from heapq import heappop, heappush

def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    d = defaultdict(list)
    for _ in range(N):
        a, b = list(map(int, input().split()))
        d[a].append(-b)

    ans = 0
    heap = []
    for i in range(1, M+1):
        for b in d[i]:
            heappush(heap, b)
        if heap:
            ans -= heappop(heap)

    return ans


if __name__ == '__main__':
    print((main()))
# Original solution
import sys
input = sys.stdin.readline

def main():
    n,m = list(map(int,input().split()))
    money = [[] for _ in range(10**5)]
    dp = [0] * m

    for _ in range(n):
        a,b = list(map(int,input().split()))
        money[a-1].append(b)

    max1 = []
    for i in range(m):
        max1 += money[i]
        if max1:
            dp[i] = max1.pop(max1.index(max(max1)))
        if i >= 1:
            dp[i] += dp[i-1]

    print((dp[-1]))

if __name__ == '__main__':
    main()


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

def main():
    n,m = list(map(int,input().split()))
    money = [[] for _ in range(10**5)]
    dp = [0] * m
    hq = []
    heapq.heapify(hq)

    for _ in range(n):
        a,b = list(map(int,input().split()))
        money[a-1] += [b]

    for i in range(m):
        for j in money[i]:
            heapq.heappush(hq,-j)
        if hq:
            dp[i] += -heapq.heappop(hq)
        if i >= 1:
            dp[i] += dp[i-1]
    print((dp[-1]))

if __name__ == '__main__':
    main()