CodeEff/ECCO huggingface dataset:
# Original solution
import heapq

N, M = list(map(int, input().split()))
job_list = {}
ans = 0
kouho = []
for _ in range(N):
  A, B = list(map(int, input().split()))
  B = B * (-1)
  if A in list(job_list.keys()):
    job_list[A].append(B)
  else:
    job_list[A] = [B]

for day in range(1, M + 1):
  try: 
    kouho += job_list[day]
    heapq.heapify(kouho)
  except KeyError:
    pass
  if len(kouho) != 0:
    ans += heapq.heappop(kouho)

print((ans * (-1)))

# Optimized solution
import heapq

N, M = list(map(int, input().split()))
AB_list = []
[heapq.heappush(AB_list, list(map(int, input().split()))) for _ in range(N)]
ans = 0
kouho = []

for day in range(1, M + 1):
    if len(AB_list) != 0:
        while AB_list[0][0] <= day:           
            heapq.heappush(kouho, heapq.heappop(AB_list)[1]*(-1))
            if len(AB_list) == 0:
                break
    if len(kouho) != 0:
        ans += heapq.heappop(kouho)

print((ans * (-1)))
# Original solution
import heapq

n, m = list(map(int, input().split()))
lis = []
for i in range(m + 1):
    lis.append([])

for i in range(n):
    a, b = list(map(int, input().split()))
    if a <= m:
        lis[a].append(-b)

cnd = []
heapq.heapify(cnd)
res = 0

for j in range(1, m + 1):
    for e in lis[j]:
        heapq.heappush(cnd, e)
    if cnd:
        res += -heapq.heappop(cnd)
print(res)


# Optimized solution
import heapq

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

for i in range(n):
    a, b = list(map(int, input().split()))
    if a <= m:
        lis[a].append(-b)

cnd = []
res = 0

for j in range(1, m + 1):
    for e in lis[j]:
        heapq.heappush(cnd, e)
    if cnd:
        res += -heapq.heappop(cnd)
print(res)
# Original solution
import heapq

N, M = list(map(int,input().split()))
jobs = [[] for i in range(10 ** 5 + 1)]
lst = []

for i in range(N):
    a, b = list(map(int,input().split()))
    jobs[a].append((-1) * b)

ans = 0

for i in range(1,M+1):
    lst += jobs[i]
    if lst:
        heapq.heapify(lst)
        k = heapq.heappop(lst)
        ans += (-1) * k

print(ans)

# Optimized solution
import heapq
N, M = list(map(int,input().split()))
L = [[] for _ in range(M)]
for _ in range(N):
    A, B = list(map(int,input().split()))
    try:
        L[A-1].append(-B)
    except:
        pass
Q = []
ans = 0
for i in range(M):
    for j in L[i]:
        heapq.heappush(Q, j)
    try:
        ans -= heapq.heappop(Q)
    except:
        pass
print(ans)
# Original solution
from sys import exit, stderr, stdin
input = stdin.readline
import heapq
from collections import defaultdict
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N,M = [int(n) for n in input().split()]
    # {期限:価値}
    jobs = defaultdict(list)
    v = 0
    for i in range(N):
        a,b = [int(n) for n in input().split()]
        if a <= M:
            jobs[a].append(-b)
    cand = []
    ans = 0
    for i in range(1,M+1):
        if i in jobs:
            cand += jobs[i]
            heapq.heapify(cand)
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)



if __name__ == "__main__":
    main()


# Optimized solution
from sys import exit, stderr, stdin
input = stdin.readline
import heapq
# from collections import defaultdict
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N,M = [int(n) for n in input().split()]
    # {期限:価値}
    jobs = [[]]*N
    v = 0
    for i in range(N):
        jobs[i] = [int(n) for n in input().split()]
    # 価値降順→期間昇順
    jobs = sorted(jobs,key=lambda x:-x[1])
    jobs = sorted(jobs,key=lambda x:x[0])
    cur = 0
    ans = 0
    cand = []
    for i in range(1,M+1):
        while(cur < N and i >= jobs[cur][0]):
            heapq.heappush(cand,-jobs[cur][1])
            cur += 1
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)



if __name__ == "__main__":
    main()
# Original solution
import bisect
N,M = list(map(int,input().split()))
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        index = bisect.bisect_left(can,AB[p][1])
        can.insert(index,AB[p][1])
        p += 1
        
    if can != []:
        ans += can.pop(-1)
print(ans)

# Optimized solution
import heapq
N,M = list(map(int,input().split()))
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        heapq.heappush(can,-AB[p][1])
        p += 1
        
    if can != []:
        ans += heapq.heappop(can)
print((-ans))
# Original solution
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
ab = [tuple(int(i) for i in input().split()) for _ in range(n)]
ab.sort()
hq = []
result = 0
j = 0
for i in range(1, m + 1):
    for a, b in ab[j:]:
        if a > i:
            break
        heappush(hq, -b)
        j += 1
    if hq:
        result -= heappop(hq)
print(result)

# Optimized solution
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
n, m = list(map(int, input().split()))
ab = [tuple(int(i) for i in input().split()) for _ in range(n)]
ab.sort()
hq = []
result = 0
j = 0
for i in range(1, m + 1):
    while j < n and ab[j][0] <= i:
        heappush(hq, -ab[j][1])
        j += 1
    if hq:
        result -= heappop(hq)
print(result)
# Original solution
from collections import defaultdict
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)

works = []
for k in range(M, 0, -1):
    d[k] = sorted(d[k], reverse=True)
    if M-k+1 >= 1:
        works.extend(d[k][:M-k+1])
        works = sorted(works, reverse=True)
        works = works[:M-k+1]
        # print(works)

works = sorted(works, reverse=True)

print((sum(works[:M])))


# Optimized solution
# from collections import defaultdict
import heapq
N, M = list(map(int, input().split()))

W = []
for _ in range(N):
    W.append(tuple(map(int, input().split())))
W = sorted(W, key=lambda x: (x[0], -x[1]))


a = []
heapq.heapify(a)  # リストaのheap化

ans = 0
c = 0
for day in range(1, M+1):
    day_c = 0
    while c != N and W[c][0] == day:
        if day_c <= M-day:
            x = W[c][1]
            heapq.heappush(a, -x)  # heap化されたリストaに要素xを追加
            day_c += 1
        c += 1
    if len(a) >= 1:
        ans += -heapq.heappop(a)  # heap化されたリストaから最小値を削除＆その最小値を出力

print(ans)
# Original solution
import heapq
N, M = list(map(int, input().split()))

W = []  # 取り組める仕事のリスト
for _ in range(N):
    W.append(tuple(map(int, input().split())))
W = sorted(W, key=lambda x: (x[0], -x[1]))


a = []
heapq.heapify(a)  # リストaのheap化

ans = 0
c = 0
for day in range(1, M+1):
    day_c = 0
    while c != N and W[c][0] == day:
        if day_c <= M-day:
            x = W[c][1]
            heapq.heappush(a, -x)  # heap化されたリストaに要素xを追加
            day_c += 1
        c += 1
    if len(a) >= 1:
        ans += -heapq.heappop(a)  # heap化されたリストaから最小値を削除＆その最小値を出力

print(ans)


# Optimized solution
import heapq
N, M = list(map(int, input().split()))

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

hq = []
heapq.heapify(hq)  # リストhqのheap化

ans = 0
i = 0
for d in range(1, M+1):
    while i <= N-1 and AB[i][0] <= d:
        heapq.heappush(hq, -AB[i][1])  # heap化されたリストhqに要素xを追加
        i += 1
    if hq:
        ans -= heapq.heappop(hq)  # heap化されたリストhqから最小値を削除＆その最小値を出力
print(ans)
# Original solution
import heapq as hq
n,m=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    a[i][1]*=-1
hq.heapify(a)
k=1
ans=0
q=0
while a and k<=m:
    p=hq.heappop(a)
    if p[0]>k:
        hq.heappush(a,p)
        k+=1
    elif p[0]==k:
        ans-=p[1]
        k+=1
    elif p[0]<k:
        p[0]=k
        hq.heappush(a,p)
print(ans)

# Optimized solution
import heapq as hq
n,m=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(n)]
a.sort()
for i in range(n):
    a[i][1]*=-1
b=[]
hq.heapify(b)
k,ans,i=1,0,0
while k<=m:
    while i<n and a[i][0]<=k:
        hq.heappush(b,a[i][1])
        i+=1
    if b:
        ans-=hq.heappop(b)
    k+=1
print(ans)
# Original solution
import heapq as hq
n,m=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(n)]
a.sort()
for i in range(n):
    a[i][1]*=-1
b=[]
hq.heapify(b)
k,ans,i=1,0,0
while k<=m:
    while i<n and a[i][0]<=k:
        hq.heappush(b,a[i][1])
        i+=1
    if b:
        ans-=hq.heappop(b)
    k+=1
print(ans)

# Optimized solution
import heapq as hq
n,m=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(n)]
a.sort()
for i in range(n):
    a[i][1]*=-1
b=[]
hq.heapify(b)
ans,i=0,0
for k in range(m-1,-1,-1):
    while i<n and a[i][0]+k<=m:
        hq.heappush(b,a[i][1])
        i+=1
    if b:
        ans-=hq.heappop(b)
print(ans)
# Original solution
from bisect import insort_left as sort

import sys
def input():
    return sys.stdin.readline()

n,m = list(map(int,input().split()))
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
list = sorted(list)
for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      sort(temp,list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans += temp.pop()
print(ans)

# Optimized solution
import heapq as hp
import sys
def input():
    return sys.stdin.readline()

n,m = list(map(int,input().split()))
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
j = 0
list = sorted(list)

for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      hp.heappush(temp,-list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans -= hp.heappop(temp)
print(ans)
# Original solution
L=lambda:list(map(int,input().split()));n,m=L();t=sorted([L()for _ in range(n)]+[[float("inf")]]);import heapq as q;h=[];a=j=0
for i in range(t[0][0],m+1):
  while t[j][0]==i:q.heappush(h,-t[j][1]);j+=1
  if h:a-=q.heappop(h)
print(a)

# Optimized solution
n,m=list(map(int,input().split()))
ab=[list(map(int,input().split()))for _ in range(n)]+[[100000000,0]]
for i in range(m):ab.append([i+1,0])
ab.sort()
from heapq import heappop,heappush
h=[]
j=0
ans=0
for i in range(1,m+1):
  while ab[j][0]<=i:
    heappush(h,-ab[j][1])
    j+=1
  ans-=heappop(h)
print(ans)
# Original solution
from heapq import heappop,heappush

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

AB = [[int(i) for i in input().split()] for j in range(n)]

#ab.sort()

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

#print(ab)

tmp = 0
hq = []
ans = 0

for i in range(1,m+1):
    while tmp <= n-1 and AB[tmp][0] <= i:
        heappush(hq,-AB[tmp][1])
        tmp += 1
    if len(hq) > 0:
        ans += -heappop(hq)
        
print(ans)

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

import heapq
import bisect

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

ab = [[int(i) for i in input().split()] for j in range(n)]

ab.sort()

a_li = [-1] * n
b_li = [-1] * n

for i in range(n):
    a_li[i] = ab[i][0]
    b_li[i] = ab[i][1]

kouho = []
bfr_idx = 0
ans = 0

for i in range(m):
    day = i + 1
    idx = bisect.bisect_right(a_li,day)
    for j in range(idx-bfr_idx):
        heapq.heappush(kouho,-b_li[bfr_idx+j])
    bfr_idx = idx
    if len(kouho) != 0:
        ans += -heapq.heappop(kouho)

print(ans)
# Original solution
import heapq


class Reverse:
    def __init__(self, val):
        self.val = val

    def __lt__(self, other):
        return self.val > other.val

    def __repr__(self):
        return repr(self.val)


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        if len(self.hq) == 0:
            return None
        if self.desc:
            return heapq.heappop(self.hq).val
        else:
            return heapq.heappop(self.hq)

    def push(self, a):
        if self.desc:
            heapq.heappush(self.hq, Reverse(a))
        else:
            heapq.heappush(self.hq, a)

    def top(self):
        if self.desc:
            return self.hq[0].val
        else:
            return self.hq[0]

    def __len__(self):
        return len(self.hq)


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

ans = 0
h = Heapq([], desc=True)
for i in range(1, m+1):
    for j in [x for x in ab if x[0] == i]:
        h.push(j[1])
    ab = list([x for x in ab if x[0] != i])
    if len(h) == 0:
        continue
    ans += h.pop()
print(ans)


# Optimized solution
import heapq


class Reverse:
    def __init__(self, val):
        self.val = val

    def __lt__(self, other):
        return self.val > other.val

    def __repr__(self):
        return repr(self.val)


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        if len(self.hq) == 0:
            return None
        if self.desc:
            return heapq.heappop(self.hq).val
        else:
            return heapq.heappop(self.hq)

    def push(self, a):
        if self.desc:
            heapq.heappush(self.hq, Reverse(a))
        else:
            heapq.heappush(self.hq, a)

    def top(self):
        if self.desc:
            return self.hq[0].val
        else:
            return self.hq[0]

    def __len__(self):
        return len(self.hq)


n, m = list(map(int, input().split(' ')))
ab = []
for _ in range(n):
    a, b = list(map(int, input().split(' ')))
    if a > m:
        continue
    ab.append((a, b))
ab.sort(reverse=True)

ans = 0
h = Heapq([], desc=True)
for i in range(1, m+1):
    while ab and ab[-1][0] <= i:
        a, b = ab.pop()
        h.push(b)
    if h:
        ans += h.pop()
print(ans)
# Original solution
from operator import itemgetter
a,b=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(a)]
c.sort()
d=[]
sum=0
e=0
for i in range(1,b+1):
  for j in range(e,a):
    if c[j][0]<=i:
      d.append(c[j][1])
    else:
      e=j
      break
  d.sort()
  if len(d)>0:
    sum+=d[-1]
    d.pop()
      
    
  
print(sum)

# Optimized solution
import heapq
a,b=list(map(int,input().split()))
c=[list(map(int,input().split())) for i in range(a)]
c.sort()
d=[[0]for i in range(b)]
sum=0
for i,j in c:
  if i<=b:
    d[i-1].append(j)
e=[]

for i in d:
  for j in i:
    heapq.heappush(e,-j)
  sum-=heapq.heappop(e)
      
    
  
print(sum)
# Original solution
import heapq

def main():
    n, m = list(map(int, input().split()))
    a = [list(map(int, input().split())) for i in range(n)]
    a_t = list(zip(*a))
    a_s = set(a_t[0])
    cnt = 0
    h = [(l[1]*-1) for l in a if l[0] == 1]
    heapq.heapify(h)
    if 1 in a_s:
        cnt += heapq.heappop(h)
    for i in range(2, m+1):
        if i in a_s:
            p = [(l[1]*-1) for l in a if l[0] == i]
            for j in p:
                heapq.heappush(h, j)
        if h !=[]:
            cnt += heapq.heappop(h)
    print((-1*cnt))   
    
if __name__ == '__main__':
    main()

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import heapq

def main():
    n, m = list(map(int, input().split()))
    ab = [list(map(int, input().split())) for i in range(n)]
    a_t = list(zip(*ab))
    AB = [[] for _ in range(m+1)]
    for a, b in zip(*a_t):
        if a <= m:
            AB[a].append(b*-1)
    # print(AB)
    cnt = 0
    h = [] 
    heapq.heapify(h)
    for i in AB:
        for j in i:
            heapq.heappush(h, j)
            # print('h', h)
        if h:
            cnt += heapq.heappop(h)
    print((-1*cnt))   

if __name__ == '__main__':
    main()
# Original solution
import heapq
n,m = list(map(int, input().split()))
job_list = []
for i in range(n):
    a,b = list(map(int, input().split()))
    job_list.append((a,b))
job_list.sort(key=lambda x:x[0])
# print(job_list)

hq = []
max_income = 0
j = 0
for i in range(1,m+1):
    while j < n and job_list[j][0] <= i:
        heapq.heappush(hq, (-job_list[j][1],job_list[j][0]))
        j += 1
    if len(hq) < 1:
        continue
    b_i, a_i = heapq.heappop(hq)
    max_income += -b_i

print(max_income)

# Optimized solution
import sys
import heapq
input = sys.stdin.readline
n,m = list(map(int, input().split()))
job_list = []
for i in range(n):
    a,b = list(map(int, input().split()))
    job_list.append((a,b))
job_list.sort(key=lambda x:x[0])
# print(job_list)

hq = []
max_income = 0
j = 0
for i in range(1,m+1):
    while j < n and job_list[j][0] <= i:
        heapq.heappush(hq, (-job_list[j][1],job_list[j][0]))
        j += 1
    if len(hq) < 1:
        continue
    b_i, a_i = heapq.heappop(hq)
    max_income += -b_i

print(max_income)
# Original solution
# 2019-11-16 14:51:41(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import heapq

def main():
    # 10**5連勤とか超人すぎる
    n, m = [int(x) for x in sys.stdin.readline().split()]

    job_list = dict((i, list()) for i in range(1, m+1))
    for _ in range(n):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a <= m:
            job_list[a].append(b)
    
    schedule = [0 for _ in range(m+1)] # 当日は報酬0
    q = []
    for i in range(1, m+1):
        for reward in job_list[i]: # 期限i日前にできる仕事の報酬一覧をheqp queueに入れる
            heapq.heappush(q, -reward) 
            # あとで最大値を取り出すために-1かける(heappop()で取り出せるのは最小値)

        if q: # その日できる仕事が一つでもあれば
            # 報酬の最小値(マイナスなので元々の最大値)を取り出して-1をかけ、scheduleにいれる
            schedule[i] = -heapq.heappop(q) # 戻り値を返すと同時にq自体も変更している
        else: # できる仕事がなければ
            pass  # その日の報酬は0のまま

    print((sum(schedule)))


if __name__ == "__main__":
    main()


# Optimized solution
# 2019-11-16 14:51:41(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 
import heapq

def main():
    # 10**5連勤とか超人すぎる
    n, m = [int(x) for x in sys.stdin.readline().split()]

    job_list = dict((i, list()) for i in range(1, m+1))
    for _ in range(n):
        a, b = [int(x) for x in sys.stdin.readline().split()]
        if a <= m:
            job_list[a].append(b)
    
    schedule = [0 for _ in range(m+1)] # 当日は報酬0
    q = []
    for i in range(1, m+1):
        for reward in job_list[i]: # 期限i日前にできる仕事の報酬一覧をheqp queueに入れる
            heapq.heappush(q, -reward) 
            # あとで最大値を取り出すために-1かける(heappop()で取り出せるのは最小値)

        if q: # その日できる仕事が一つでもあれば
            # 報酬の最小値(マイナスなので元々の最大値)を取り出して-1をかけ、scheduleにいれる
            schedule[i] = -heapq.heappop(q) # 戻り値を返すと同時にq自体も変更している
        else: # できる仕事がなければ
            pass  # その日の報酬は0のまま

    print((sum(schedule)))


if __name__ == "__main__":
    main()
# Original solution
N,M = list(map(int,input().split()))
from collections import defaultdict
hist = defaultdict(int)
AB = [[0,0]]*N
for n in range(N):
    temp = list(map(int,input().split()))
    if temp[0] <= M:
        AB[n] = temp
        hist[temp[0]] = temp[0]-1
work_schedule = [0]*M
sorted_AB = sorted(AB,key=lambda x:(-x[1],x[0]))
for work in sorted_AB:
    while True:
        if hist[work[0]] >= M:
            break
        elif work_schedule[hist[work[0]]] == 0:
            work_schedule[hist[work[0]]]  = work[1]
            break
        elif  work_schedule[hist[work[0]]] != 0:
            hist[work[0]] += 1
print((sum(work_schedule)))

# Optimized solution
from collections import defaultdict
import heapq

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

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

h = []
res = 0

for i in range(1, m + 1):
    if dct[i]:
        for item in dct[i]:
            heapq.heappush(h, item)
    if h:
        res += heapq.heappop(h)

print((-res))
# Original solution
from heapq import heapify, heappop

nm = list(map(int, input().split()))
n, m = nm[0], nm[1]
d = {}
ab_list = list()
for i in range(n):
    ab = list(map(int, input().split()))
    rem_day, reward = ab[0], ab[1]
    if rem_day in d:
        d[rem_day].append(-reward)
    else:
        d[rem_day] = [-reward]


result = 0

candidates = list()

for rem in range(1, m + 1):
    added_candidates = d.get(rem)
    if added_candidates is not None and len(added_candidates) > 0:
        candidates.extend(added_candidates)

    if len(candidates) > 0:
        heapify(candidates)
        top = heappop(candidates)
        result += -top

print(result)


# Optimized solution
from heapq import heapify, heappop, heappush

nm = list(map(int, input().split()))
n, m = nm[0], nm[1]
d = {}
ab_list = list()
for i in range(n):
    ab = list(map(int, input().split()))
    rem_day, reward = ab[0], ab[1]
    if rem_day in d:
        d[rem_day].append(-reward)
    else:
        d[rem_day] = [-reward]


result = 0

candidates = list()

for rem in range(1, m + 1):
    added_candidates = d.get(rem)
    if added_candidates is not None:
        for ac in added_candidates:
            heappush(candidates, ac)

    if len(candidates) > 0:
        top = heappop(candidates)
        result += -top

print(result)
# Original solution
import heapq as hq

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

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append([b * (-1), a * (-1)])


# print(A)
ans = 0
Q = []
hq.heapify(Q)

for i in range(1, M + 1):
    for j in (A):
        if j[1] * (-1) <= i:
            hq.heappush(Q, j)
            A.remove([j[0], j[1]])
            # print(Q)
    if Q:
        ans += hq.heappop(Q)[0] * (-1)


print(ans)


# Optimized solution
import heapq as hq

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

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append([a, b * (-1)])

A.sort()
# print(A)

ans = 0
j = 0
Q = []
hq.heapify(Q)

for i in range(1, M + 1):
    while j < len(A):
        if A[j][0] <= i:
            hq.heappush(Q, A[j][1])
            # print(Q)
        else:
            break
        j += 1
    if Q:
        ans += hq.heappop(Q) * (-1)


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

from operator import itemgetter
AB.sort(key = itemgetter(0))
AB.sort(key = itemgetter(1),reverse = True)

days = [0]*(M+1)
points = list(range(M+1))

from  bisect import bisect_right

for a,b in AB:
    point = M - a
    i = bisect_right(points,point)
    if i > 0:
        days[points[i-1]] = b
        points.pop(i-1)
print((sum(days)))

# Optimized solution
import heapq
from operator import itemgetter

def main():
    N, M = list(map(int,input().split()))
    AB = [list(map(int,input().split())) for _ in range(N)]
    AB.sort(key = itemgetter(0))
    heap = []
    ans = 0
    cnt = 0
    for i in range(1,M + 1):
        while cnt < N and AB[cnt][0] <= i:
            heapq.heappush(heap,-1*AB[cnt][1])
            cnt += 1
        if len(heap) > 0:
            ans += heapq.heappop(heap)*-1
    print(ans)
    return

if __name__ == "__main__":
    main()