CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import collections
import heapq
input = sys.stdin.readline


def main():
    N, M = list(map(int, input().split()))
    AB = []
    for _ in range(N):
        a, b = list(map(int, input().split()))
        AB.append((a, b))
    AB.sort(key=lambda x: x[0])

    H = []
    I = 0
    ans = 0
    for m in range(M):
        for i in range(I, N):
            if AB[i][0] <= m+1:
                heapq.heappush(H, -AB[i][1])
                I += 1
        if H:
            ans += -heapq.heappop(H)
    print(ans)


if __name__ == '__main__':
    main()


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


def main():
    N, M = list(map(int, input().split()))
    AB = []
    for _ in range(N):
        a, b = list(map(int, input().split()))
        AB.append((a, b))
    AB.sort(key=lambda x: x[0])

    H = []
    I = 0
    ans = 0
    for m in range(M):
        for i in range(I, N):
            if AB[i][0] <= m+1:
                heapq.heappush(H, -AB[i][1])
                I += 1
            else:
                break
        if H:
            ans += -heapq.heappop(H)
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
# ABC137D - Summer Vacation
import sys
from heapq import heappop, heappush

readline = sys.stdin.buffer.readline
heappop_max = lambda heap: -heappop(heap)
heappush_max = lambda heap, x: heappush(heap, -x)


def main():
    N, M = list(map(int, readline().split()))
    all_jobs = tuple(tuple(map(int, readline().split())) for _ in range(N))
    available_jobs = [[] for _ in range(M + 1)]
    for waiting_days, reward in all_jobs:
        if waiting_days <= M:
            available_jobs[waiting_days].append(reward)
    heap = []
    max_rewards = 0
    for jobs in available_jobs[1:]:
        for job in jobs:
            heappush_max(heap, job)
        if heap:
            max_rewards += heappop_max(heap)
    print(max_rewards)


if __name__ == "__main__":
    main()


# Optimized solution
# ABC137D - Summer Vacation
import sys
from heapq import heappop, heappush

readline = sys.stdin.buffer.readline
heappop_max = lambda heap: -heappop(heap)
heappush_max = lambda heap, x: heappush(heap, -x)


def main():
    N, M = list(map(int, readline().split()))
    available_jobs = [[] for _ in range(M + 1)]
    for _ in range(N):
        waiting_days, reward = list(map(int, readline().split()))
        if waiting_days <= M:
            available_jobs[waiting_days].append(reward)
    heap = []
    max_rewards = 0
    for jobs in available_jobs[1:]:
        for job in jobs:
            heappush_max(heap, job)
        if heap:
            max_rewards += heappop_max(heap)
    print(max_rewards)


if __name__ == "__main__":
    main()
# Original solution
# ABC137D - Summer Vacation
import sys
from heapq import heappop, heappush

readline = sys.stdin.buffer.readline
heappop_max = lambda heap: -heappop(heap)
heappush_max = lambda heap, x: heappush(heap, -x)


def main():
    N, M = list(map(int, readline().split()))
    available_jobs = [[] for _ in range(M + 1)]
    for _ in range(N):
        waiting_days, reward = list(map(int, readline().split()))
        if waiting_days <= M:
            available_jobs[waiting_days].append(reward)
    heap = []
    max_rewards = 0
    for jobs in available_jobs[1:]:
        for job in jobs:
            heappush_max(heap, job)
        if heap:
            max_rewards += heappop_max(heap)
    print(max_rewards)


if __name__ == "__main__":
    main()


# Optimized solution
# ABC137D - Summer Vacation
import sys
from heapq import heappop, heappush

read = sys.stdin.buffer.read
heappop_max = lambda heap: -heappop(heap)
heappush_max = lambda heap, x: heappush(heap, -x)


def main():
    N, M, *all_jobs = list(map(int, read().split()))
    available_jobs = [[] for _ in range(M + 1)]
    for waiting_days, reward in zip(*[iter(all_jobs)] * 2):
        if waiting_days <= M:
            available_jobs[waiting_days].append(reward)
    heap = []
    max_rewards = 0
    for jobs in available_jobs[1:]:
        for job in jobs:
            heappush_max(heap, job)
        if heap:
            max_rewards += heappop_max(heap)
    print(max_rewards)


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

import heapq
N,M = (int(x) for x in input().split())
jobs = []
taken = [0] * (M+1)
jappend = jobs.append
for i in range(N):
    A,B = (int(x) for x in input().split())
    if A <= M:
        jappend((B*(-1),A))
heapq.heapify(jobs)
for i in range(len(jobs)):
    temp = heapq.heappop(jobs)
    if taken[(M-temp[1]+1)//2:M-temp[1]+1].count(0) > 0:
        searching = M - temp[1]
    else:
        if temp[1] == M:
            searching = 0
        elif taken[0:(M-temp[1]+1)//2].count(0) > 0:
            searching = ((M-temp[1]+1)//2) - 1
        else:
            continue
    for i in range(searching+1):
        if taken[searching-i] == 0:
            taken[searching-i] = temp[0]
            break
print((sum(taken)*(-1)))

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

import heapq
N,M = (int(x) for x in input().split())
jobs = [[] for i in range(M)]
count = 0
h = []
for i in range(N):
    A,B = (int(x) for x in input().split())
    if A <= M:
        jobs[A-1].append(B*(-1))
heapq.heapify(h)
for i in range(M):
    if jobs[i]:
        for job in jobs[i]:
            heapq.heappush(h,job)
    if h:
        count -= heapq.heappop(h)
print(count)
# Original solution
import heapq
N,M=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(N)]

AB.sort()
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[0][0]<=latest:
    a,b=AB.pop(0)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

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(reverse=True)
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-1][0]<=latest:
    a,b=AB.pop()
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)
# Original solution
import heapq
N,M=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(N)]

AB.sort(reverse=True)
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-1][0]<=latest:
    a,b=AB.pop(-1)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

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(reverse=True)
works=[]
heapq.heapify(works)
ans=0
AB=AB+[[0,0]]
for i in range(1,M+1):
  day=M-i
  latest=i
  while len(AB)>=2 and AB[-2][0]<=latest:
    a,b=AB.pop(-2)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)
# Original solution
from heapq import heappush, heappop

n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
ans = 0
hq = []
for i in range(1, m+1):
    for a, b in ab:
        if a == i:
            heappush(hq, (-b, a))
        
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)

# Optimized solution
from heapq import heappush, heappop

n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort()
ans = 0
hq = []
idx = 0
for i in range(1, m+1):
    for j in range(idx, n):
        if ab[j][0] == i:
            heappush(hq, (-ab[j][1], ab[j][0]))
            idx += 1
        else:
            break
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)
# Original solution
n,m = list(map(int,input().split()))
ab = []
for i in range(n):
  a,b = list(map(int,input().split()))
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  pool.sort()
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

# Optimized solution
n,m = list(map(int,input().split()))
ab = []
for i in range(n):
  a,b = list(map(int,input().split()))
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)
# Original solution
import heapq

def main():

    N, M = list(map(int, input().split()))
    AB = []
    for i in range(N):
        a, b = list(map(int, input().split()))
        AB.append([a, b])
        AB.sort(reverse = True)
    q = []
    ans = 0
    j = N-1
    for i in range(M-1, -1, -1):
        while j >=0 and AB[j][0] <= M-i:
            a, b = AB[j]
            heapq.heappush(q, -b)
            j -= 1
        if q:
            v = -heapq.heappop(q)
            ans += v
    return ans


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

# Optimized solution
import heapq
def main():

    N, M = list(map(int, input().split()))
    task = []
    for _ in range(N):
        A, B = list(map(int, input().split()))
        task.append([A, B])
    task.sort()

    hq = []
    ans = 0
    for i in range(1, M+1):
        while task and task[0][0]<= i:
            _, v = heapq.heappop(task)
            heapq.heappush(hq, -v)
        if hq:
            v = heapq.heappop(hq)
            ans += -v
    return ans

if __name__ == '__main__':
    print((main()))
# Original solution
import sys
from collections import deque
sys.setrecursionlimit(20000000)
input = sys.stdin.readline


def main():
    n, limit = list(map(int, input().split()))
    jobs = [list(map(int, input().split())) for _ in range(n)]

    for j in jobs:
        j.append(True)

    money = 0

    for d in range(1, limit+1):
        available_jobs = []

        for j in jobs:
            if j[0] <= d:
                available_jobs.append(j)
        
        if available_jobs:
            available_jobs.sort(key=lambda x: x[1], reverse=True)
            for aj in available_jobs:
                if aj[2]:
                    money += aj[1]
                    aj[2] = False
                    break

    print(money)


if __name__ == '__main__':
    main()



# Optimized solution
import sys
from collections import deque, defaultdict
from heapq import heappop, heappush
sys.setrecursionlimit(20000000)
input = sys.stdin.readline


def main():
    n, limit = list(map(int, input().split()))
    
    jobs = defaultdict(deque)

    for _ in range(n):
        d, v = list(map(int, input().split()))
        jobs[d].append(v)

    money = 0
    available_jobs = []

    for d in range(1, limit+1):
        if d in jobs:
            for j in jobs[d]:
                heappush(available_jobs, -j)

        if available_jobs:
            money += -heappop(available_jobs)

    print(money)


if __name__ == '__main__':
    main()
# Original solution
n,m = list(map(int,input().split()))
ab = {i:[] for i in range(m+1)}
for i in range(n):
    a,b = list(map(int,input().split()))
    if a in ab:
        ab[a].append(b)
    else:
        ab[a] = [b]
#print(ab)
ans = 0
for i in range(m)[::-1]:
    rem = m - i
    if ab[rem]:
        job = ab[rem]
        ans += max(job)
        del ab[rem][job.index(max(job))]
        if rem < m:
            ab[rem+1] += ab[rem]
    #print(ans)
print(ans)


# Optimized solution
n,m = list(map(int,input().split()))
ab = {i:[] for i in range(m+1)}
for i in range(n):
    a,b = list(map(int,input().split()))
    if a in ab:
        ab[a].append(b)
    else:
        ab[a] = [b]
#print(ab)
ans = 0
for i in range(m)[::-1]:
    rem = m - i
    if ab[rem]:
        job = ab[rem]
        ans += max(job)
        del ab[rem][job.index(max(job))]
        if rem < m:
            ab[rem+1] += ab[rem]
            ab[rem] = []
    #print(ans)
print(ans)
# Original solution
def main():
    from collections import deque
    n, m, *ab = list(map(int, open(0).read().split()))
    job = [[] for _ in range(1 + max(ab))]
    for i, j in zip(*[iter(ab)] * 2):
        job[i].append(j)

    ans = 0
    t = []
    for i in range(1, m + 1):
        t += job[i]
        if len(t) == 0:
            continue
        else:
            m = max(t)
            t.remove(m)
            ans += m

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import heapq
    n, m, *ab = list(map(int, open(0).read().split()))
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            l[i].append(-j)

    ans = 0
    h = []
    for i in range(1, m + 1):
        g = l[i]
        for j in g:
            heapq.heappush(h, j)
        if len(h) > 0:
            x = heapq.heappop(h)
            ans += x

    print((-ans))


if __name__ == '__main__':
    main()
# Original solution
def main():
    import heapq
    n, m, *ab = list(map(int, open(0).read().split()))
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            heapq.heappush(l[i], -j)
    
    ans = 0
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            heapq.heappush(h, j)
        if len(h) > 0:
            x = heapq.heappop(h)
            ans += x

    print((-ans))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    from heapq import heappush as sushi
    from heapq import heappop as tempra
    n, m, *ab = list(map(int, open(0).read().split()))
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            l[i].append(-j)

    w = []
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            sushi(h, j)
        if len(h) > 0:
            x = tempra(h)
            w.append(x)

    print((-sum(w)))


if __name__ == '__main__':
    main()
# Original solution
import heapq
N, M = list(map(int, input().split()))
S = [[0] for _ in range(M)]
for _ in range(N):
    a, b = list(map(int, input().split()))
    if M < a:
        continue
    heapq.heappush(S[M-a], -b)

ans = heapq.heappop(S[M-1])
for i in range(M-2, -1, -1):
    for num in S[i+1]:
        heapq.heappush(S[i], num)
    ans += heapq.heappop(S[i])
print((-ans))

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
S = list()
A = [list() for _ in range(M+1)]
for _ in range(N):
    a, b = list(map(int, input().split()))
    if M < a:
        continue
    heapq.heappush(A[a], -b)

ans = 0
for i in range(1, M+1):
    for num in A[i]:
        heapq.heappush(S, num)
    if S == []:
        continue
    ans -= heapq.heappop(S)
print(ans)
# Original solution
from heapq import heapify, heappush, heappop

N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for i in range(N)]
AB.sort()
queue = []
heapify(queue)
ans = 0

for i in range(1, M+1):
    while len(AB) > 0:
        if AB[0][0] <= i:
            heappush(queue, -AB.pop(0)[1])
        else:
            break

    if queue:
        ans += -heappop(queue)

print(ans)


# Optimized solution
from heapq import heappush, heappop

N, M = list(map(int, input().split()))
salary = [[] for i in range(M+1)]
for i in range(N):
    A, B = list(map(int, input().split()))
    if A <= M:
        salary[A].append(B)
queue = []
ans = 0

for i in range(1, M+1):
    for s in salary[i]:
        heappush(queue, -s)
    if queue:
        ans += -heappop(queue)

print(ans)
# Original solution
# -*- coding: utf-8 -*-

from heapq import heappush, heappop

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

# A日後、報酬B
a = []
b = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)

sum = 0
used = [False] * n
for i in range(0, m):
    # A <= iで未使用のものをheapqに入れる
    hq = []
    for j in range(n):
        # print(a[j], i+1,a[j]<=i+1, not used[j])

        if a[j] <= i + 1 and not used[j]:
            heappush(hq, (-b[j], j, a[j]))

    val = None
    if len(hq) > 0:
        val = heappop(hq)
    if val is not None:
        sum += -val[0]
        used[val[1]] = True

print(sum)


# Optimized solution
# -*- coding: utf-8 -*-

from heapq import heappush, heappop

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

# A日後、報酬B
workList = {}
for i in range(n):
    a, b = list(map(int, input().split()))
    if a not in workList:
        workList[a] = [-b]
    else:
        workList[a].append(-b)
# print(workList)

hq = []
sum = 0
for i in range(1, m + 1):
    if i in workList:
        for w in workList[i]:
            heappush(hq, w)

    val = None
    if len(hq) > 0:
        val = heappop(hq)
    if val is not None:
        sum += -val

print(sum)
# Original solution
# -*- coding: utf-8 -*-

from heapq import heappush, heappop

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

# A日後、報酬B
workList = {}
for i in range(n):
    a, b = list(map(int, input().split()))
    if a not in workList:
        workList[a] = [-b]
    else:
        workList[a].append(-b)
# print(workList)

hq = []
sum = 0
for i in range(1, m + 1):
    if i in workList:
        for w in workList[i]:
            heappush(hq, w)

    val = None
    if len(hq) > 0:
        val = heappop(hq)
    if val is not None:
        sum += -val

print(sum)


# Optimized solution
# -*- coding: utf-8 -*-

from heapq import heappush, heappop

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

# A日後、報酬B
# 日付ごとに報酬をリスト化
workList = {}
for i in range(n):
    a, b = list(map(int, input().split()))
    if a not in workList:
        workList[a] = [b]
    else:
        workList[a].append(b)

hq = []
sum = 0
for i in range(1, m + 1):
    # 日付が短い順にheapに追加
    # 最大値を取り出すため正負は逆に
    if i in workList:
        for w in workList[i]:
            heappush(hq, -w)

    # 最大値を取り出し、正負を戻して加算
    if len(hq) > 0:
        val = heappop(hq)
        sum += -val

print(sum)
# Original solution
from operator import itemgetter
import sys
input = sys.stdin.readline

N, M = list(map(int,input().split())) # 複数の数字を変数に格納
List = [list(map(int,input().split())) for i in range(N)] # [[1,2],[3,4],[5,6]]

List.sort(key=itemgetter(0))
List_S = []
 
result = 0
p = 0

for i in range(1, M+1):
    add = 0
    while p < N and List[p][0] == i:
        List_S.append(List[p][1])
        p += 1
        add = 1
    if len(List_S) != 0:
        if add == 1:
            List_S.sort()
        result += List_S.pop()
 
print(result)

# Optimized solution
from operator import itemgetter
import sys
input = sys.stdin.readline

N, M = list(map(int,input().split())) # 複数の数字を変数に格納
List = [list(map(int,input().split())) for i in range(N)] # [[1,2],[3,4],[5,6]]

List.sort(key=itemgetter(0))
List_S = [0]
 
result = 0
p = 0

for i in range(1, M+1):
    add = 0
    add_list = []
    while p < N and List[p][0] == i:
        if List_S[len(List_S)-1] < List[p][1]:            
            List_S.append(List[p][1])
        else:
            add_list.append(List[p][1])
            add += 1
        p += 1
            
    if len(List_S) != 1:
        result += List_S.pop()
        if add > 0:
            List_S.extend(add_list)
            List_S.sort()
 
print(result)
# Original solution
import collections
import heapq

n, m = list(map(int, input().split()))
que = []
for _ in range(n):
    a, b = list(map(int, input().split()))
    que.append((-b, a))
que.sort(key=lambda x: x[1])
que = collections.deque(que)
h = []
heapq.heapify(h)
ans = 0
for i in range(1, m + 1):
    while len(que) != 0 and que[0][1] <= i:
        tmp = que.popleft()
        heapq.heappush(h, tmp)
    if len(h) == 0: continue
    tmp = heapq.heappop(h)
    ans -= tmp[0]
print(ans)

# Optimized solution
import heapq

n, m = list(map(int, input().split()))
que = []
for _ in range(n):
    a, b = list(map(int, input().split()))
    que.append((-b, a))
que.sort(key=lambda x: x[1], reverse=True)

def solve(que, m):
    ans = 0
    h = []
    heapq.heapify(h)
    for i in range(1, m + 1):
        while len(que) != 0 and que[-1][1] <= i:
            tmp = que.pop()
            heapq.heappush(h, tmp)
        if len(h) == 0: continue
        tmp = heapq.heappop(h)
        ans -= tmp[0]
    return ans

print((solve(que, m)))
# Original solution
import heapq

n,m=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(n)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
arr=sorted(arr,reverse=True,key=lambda x:x[0])
q=[]
tmp=0
cnt=0
for a,b in arr:
  if tmp!=a:
    tmp=a
    cnt=0
  if m-tmp<cnt:
    continue
  heapq.heappush(q,(-b,a))
  cnt+=1
ans=0
cnt=0
box=[0]*(m+1)
while len(q)!=0:
  if cnt>=m:
    break
  tmp,date=heapq.heappop(q)
  if box[date]==0:
    box[date]=1
    ans-=tmp
    cnt+=1
  else:
    pos=date
    while 1:
      if pos==m:
        break
      pos+=1
      if box[pos]==0:
        box[pos]=1
        ans-=tmp
        cnt+=1
        break
print(ans)

# Optimized solution
import heapq

n,m=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+5)]
arr.extend(arr2)
arr=sorted(arr,reverse=True,key=lambda x:x[1])
arr=sorted(arr,key=lambda x:x[0])
tmp=arr[0][0]
q=[]
ans=0
pos=0
while 1:
  a,b=arr[pos]
  if a>m:
    break
  if a==arr[pos+1][0]:
    heapq.heappush(q,-b)
  else:
    heapq.heappush(q,-b)
    ans-=heapq.heappop(q)
  pos+=1
print(ans)
# Original solution
import heapq

n,m=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+2)]
arr.extend(arr2)
arr=sorted(arr,reverse=True,key=lambda x:x[1])
arr=sorted(arr,key=lambda x:x[0])
q=[]
ans=0
pos=0
while 1:
  a,b=arr[pos]
  if a>m:
    break
  if a==arr[pos+1][0]:
    heapq.heappush(q,-b)
  else:
    heapq.heappush(q,-b)
    ans-=heapq.heappop(q)
  pos+=1
print(ans)

# Optimized solution
import heapq

n,m=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+2)]
arr.extend(arr2)
arr=sorted(arr,key=lambda x:x[0])
q=[]
ans=0
pos=0
while 1:
  a,b=arr[pos]
  if a>m:
    break
  if a==arr[pos+1][0]:
    heapq.heappush(q,-b)
  else:
    heapq.heappush(q,-b)
    ans-=heapq.heappop(q)
  pos+=1
print(ans)
# Original solution
import heapq
n,m=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(n)]
l=list()
v=0
heapq.heapify(l)
for i in range(1,m+1):
  for j in range(n):
    if c[j][0]==i:
      heapq.heappush(l,(-1)*c[j][1])
  if l:
    v+=heapq.heappop(l)*(-1)
print(v)

# Optimized solution
import heapq
n,m=list(map(int,input().split()))
lis=[[] for i in range(m)]
for i in range(n):
  a,b=list(map(int,input().split()))
  if a-1<m:
    lis[a-1].append(-b)
l=list()
v=0
for i in range(m):
  for j in lis[i]:
    heapq.heappush(l,j)
  if l:
    v+=heapq.heappop(l)*(-1)
print(v)
# Original solution
import heapq
N, M = list(map(int, input().split()))
jobs = [[] for _ in range(M)]
for i in range(N):
    d, r = list(map(int, input().split()))
    if d-1 < M:
        jobs[d-1].append(-r)

m = 0
heap = []
for i in range(M):
    for a in jobs[i]:
        heapq.heappush(heap, a)
    if len(heap) > 0:
        b = heapq.heappop(heap)
        m += -b
print(m)

# Optimized solution
import heapq
def main():
  N, M = list(map(int, input().split()))
  jobs = [[] for _ in range(M)]
  for i in range(N):
    d, r = list(map(int, input().split()))
    if d-1 < M:
      jobs[d-1].append(-r)

  m = 0
  heap = []
  for i in range(M):
    for a in jobs[i]:
      heapq.heappush(heap, a)
    if len(heap) > 0:
      b = heapq.heappop(heap)
      m += -b
  print(m)
if __name__ == '__main__':
  main()