CodeEff/ECCO huggingface dataset:
# Original solution
from heapq import*
N,M,*t=list(map(int,open(0).read().split()))
v=[[]for _ in range(M)]
for i in range(N):
	if t[2*i]<=M:
		v[t[2*i]-1].append(t[2*i+1])
z=0
q=[]
for i in range(M):
	for j in v[i]:
		heappush(q,-j)
	b=0
	if len(q):
		b=heappop(q)
	z-=b
print(z)


# Optimized solution
from heapq import*
N,M,*t=list(map(int,open(0).read().split()))
v=[[]for _ in range(M)]
for i in range(N):
	if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1])
z=0
q=[]
for i in range(M):
	for j in v[i]:heappush(q,-j)
	z-=len(q)and heappop(q)
print(z)
# Original solution
from heapq import*
N,M,*t=list(map(int,open(0).read().split()))
v=[[]for _ in range(M)]
for i in range(N):
	if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1])
z=0
q=[]
for i in range(M):
	for j in v[i]:heappush(q,-j)
	z-=len(q)and heappop(q)
print(z)

# Optimized solution
from heapq import*
N,M,*t=list(map(int,open(0).read().split()))
v=[[]for _ in'_'*M]
for i in range(N):
	if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1])
z=0
q=[]
for i in v:
	for j in i:heappush(q,-j)
	z-=len(q)and heappop(q)
print(z)
# Original solution
from heapq import heappop, heappush

N, M = list(map(int, input().split()))
W = []
for i in range(N):
  a = list(map(int, input().split()))
  heappush(W, a)
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)

# Optimized solution
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
W = []
for i in range(N):
  a = list(map(int, input().split()))
  heappush(W, a)
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)
# Original solution
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
W = []
for i in range(N):
  a = list(map(int, input().split()))
  heappush(W, a)
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)

# Optimized solution
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

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

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)
# Original solution
from heapq import heapify, heappush, heappop

maxA = 10**5

N, M = list(map(int, input().split()))
works = [[] for _ in range(maxA)]
for _ in range(N):
    A, B = list(map(int, input().split()))
    works[A-1].append(-B)

PQ = []
ans = 0
for dayRest in range(1, M+1):
    for B in works[dayRest-1]:
        heappush(PQ, B)
    if not PQ: continue
    B = heappop(PQ)
    ans += -B

print(ans)


# Optimized solution
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline

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

ABs.sort()

PQ = []
iAB = 0
ans = 0
for rest in range(1, M+1):
    while iAB < N and ABs[iAB][0] <= rest:
        heappush(PQ, -ABs[iAB][1])
        iAB += 1
    if PQ:
        B = -heappop(PQ)
        ans += B

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

n,m = list(map(int,input().split()))
byte = {}
for i in range(n):
    a,b = list(map(int,input().split()))
    if a not in byte:
        byte[a] = []
    heappush(byte[a], [-b,a])

ans = 0
res = []
for i in range(1, m+1):
    if i in byte:
        heappush(res, heappop(byte[i]))
    if not res:
        continue
    b,a = heappop(res)
    ans -= b
    if byte[a]:
        heappush(res, heappop(byte[a]))
     
print(ans)

# Optimized solution
from heapq import heappop, heappush

n,m = list(map(int,input().split()))
byte = {}
for i in range(n):
    a,b = list(map(int,input().split()))
    if a > m:
        continue
    if a not in byte:
        byte[a] = []
    heappush(byte[a], -b)

ans = 0
res = []
for i in range(1, m+1):
    if i in byte:
        while byte[i]:
            heappush(res, heappop(byte[i]))
    if res:
        ans -= heappop(res)
print(ans)
# Original solution
#-*-coding:utf-8-*-

import sys
import heapq

def pickup_job(tmp_jobs):
    tmp_jobs = sorted(tmp_jobs,key=lambda x:x[1],reverse=True)
    worked_job=heapq.heappop(tmp_jobs)
    return worked_job

def main():
    N,M = list(map(int, input().split()))
    job_datas = [list(map(int,input().split())) for _ in range(N)]
    job_datas=sorted(job_datas)
    day_counter=M
    tmp_jobs=[]
    ans=0
    while day_counter >= 0:
        for job_data in job_datas:
            if (M-day_counter)>=job_data[0]:
                tmp_jobs.append(job_data)
            else:
                break
        if len(tmp_jobs)!=0:
            worked_job=pickup_job(tmp_jobs)
            ans+=worked_job[1]
            job_datas.remove(worked_job)
            tmp_jobs=[]
        day_counter-=1
    print(ans)

if __name__ == "__main__":
    main()

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

import heapq

def main2():
    n,m=list(map(int,input().split()))
    jobs_list=[list(map(int,input().split())) for _ in range(n)]
    #breakするタイミングを作るため、Mより大きい値の仕事を作成
    banpei_list=[[i,0] for i in range(1,m+2)]
    #リストの統合
    jobs_list.extend(banpei_list)
    jobs_list=sorted(jobs_list)
    ans=0
    index=0
    que=[]
    while 1:
        day,pay=jobs_list[index]
        #dayがMを超えていればbreak
        if day>m:
            break
        #同じdayのデータが次にもあれば-1倍してheapにpush
        if day==jobs_list[index+1][0]: 
            heapq.heappush(que,-pay)
        #仕事の報酬の最大値を答えに加える
        #前の日のデータからも報酬が良い仕事は選べるようにqueにデータは貯めておく
        else:
            heapq.heappush(que,-pay)
            ans-=heapq.heappop(que)
        index+=1
    print(ans)

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

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

l = []
for i in range(n):
    ab = list(map(int,input().split()))
    l.append(ab)

l.sort()
q = []
ans = 0
for i in range(1,m+1):
    while len(l) > 0 and l[0][0] == i:
        tmp = l.pop(0)
        heapq.heappush(q,-tmp[1])
    
    if len(q) != 0:
        ans += -heapq.heappop(q)

print(ans)

# Optimized solution
import heapq

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

l = []
for i in range(n):
    ab = list(map(int,input().split()))
    l.append(ab)

l.sort()
q = []
ans = 0
cnt = 0
for i in range(1,m+1):
    while cnt < n and l[cnt][0] == i:
        heapq.heappush(q,-l[cnt][1])
        cnt += 1
    
    if len(q) != 0:
        ans += -heapq.heappop(q)

print(ans)
# Original solution
import heapq
job = list()
salary = list()
n,m = list(map(int,input().split()))
k = -1
def h_sort(list):
    h = []
    for v in list:
        heapq.heappush(h, v)
    return [heapq.heappop(h) for i in range(len(h))]

for i in range(n):
    a,b = list(map(int,input().split()))
    job.append((a,b))

job = h_sort(job)
job.reverse()


for x in job:
    s,t = x
    if k == -1 or k > t:
        k = t
    salary.append(t)

    if s - m > 0:
        if salary[0] > k:
            salary = h_sort(salary)
            k = -1
        del salary[0]
    else:
        m -= 1

print((sum(salary)))


# Optimized solution
import heapq
job = list()
salary = list()
n,m = list(map(int,input().split()))

for i in range(n):
    a,b = list(map(int,input().split()))
    job.append((a,b))

job.sort(reverse = True)

for x in job:
    s,t = x
    heapq.heappush(salary,t)
    if s - m > 0:
        heapq.heappop(salary)
    else:
        m -= 1

print((sum(salary)))
# Original solution
n, m = list(map(int, input().split()))
data = [[int(e) for e in input().split()] for _ in range(n)]
d1 = {}
for i in range(n):
  a, b = data[i]
  if a not in d1:
    d1[a] = []
  d1[a].append(b)
result = 0
d2 = {}
maxb = -1
for i in range(1, m + 1):
  if i in d1:
    for j in d1[i]:
      if j not in d2:
        d2[j] = 0
      d2[j] += 1
      if j > maxb:
        maxb = j
  else:
    if len(d2) == 0:
      continue
  result += maxb
  if d2[maxb] == 1:
    del d2[maxb]
    if len(d2) == 0:
      maxb = 0
    else:
      maxb = max(d2.keys())
  else:
    d2[maxb] -= 1
print(result)


# Optimized solution
n, m = list(map(int, input().split()))
d1 = {}
for _ in range(n):
  a, b = list(map(int, input().split()))
  if a in d1:
    d1[a].append(b)
  else:
    d1[a] = [b]
result = 0
d2 = {}
maxb = -1
for i in range(1, m + 1):
  if i in d1:
    for j in d1[i]:
      if j not in d2:
        d2[j] = 0
      d2[j] += 1
      if j > maxb:
        maxb = j
  else:
    if len(d2) == 0:
      continue
  result += maxb
  if d2[maxb] == 1:
    del d2[maxb]
    if len(d2) == 0:
      maxb = 0
    else:
      maxb = max(d2.keys())
  else:
    d2[maxb] -= 1
print(result)
# Original solution
def main():
  _map = map
  _range = range
  _len = len
  _max = max
  _input = input
  _int = int
  n, m = _map(_int, _input().split())
  d1 = {}
  for _ in _range(n):
    a, b = _map(_int, _input().split())
    if a in d1:
      d1[a].append(b)
    else:
      d1[a] = [b]
  result = 0
  d2 = {}
  maxb = -1
  for i in _range(1, m + 1):
    if i in d1:
      for j in d1[i]:
        if j not in d2:
          d2[j] = 1
        else:
          d2[j] += 1
        if j > maxb:
          maxb = j
    else:
      if _len(d2) == 0:
        continue
    result += maxb
    if d2[maxb] == 1:
      del d2[maxb]
      if _len(d2) == 0:
        maxb = -1
      else:
        maxb = _max(list(d2.keys()))
    else:
      d2[maxb] -= 1
  print(result)
main()

# Optimized solution
def main():
  _map = map
  _range = range
  _len = len
  _max = max
  _input = input
  _int = int
  n, m = _map(_int, _input().split())
  d1 = {}
  for _ in _range(n):
    a, b = _map(_int, _input().split())
    if a in d1:
      d1[a].append(b)
    else:
      d1[a] = [b]
  result = 0
  d2 = {}
  maxb = -1
  for i in _range(1, m + 1):
    if i in d1:
      for j in d1[i]:
        if j not in d2:
          d2[j] = 1
        else:
          d2[j] += 1
        if j > maxb:
          maxb = j
    else:
      if _len(d2) == 0:
        continue
    result += maxb
    d2[maxb] -= 1
    if d2[maxb] == 0:
      del d2[maxb]
      if _len(d2) == 0:
        maxb = -1
      else:
        maxb = _max(list(d2.keys()))
  print(result)
main()
# Original solution
n,m=list(map(int,input().split()))
ab=[]
q=0
for i in range(n):
    p=[int(j) for j in input().split()]
    q=max(q,p[0])
    ab.append(p)
from operator import itemgetter
 
ab.sort(key=itemgetter(1))
ab.reverse()
ab.sort(key=itemgetter(0))
l=[]
import bisect
ans=0
for i in range(1,m+1):
    while True:
        if ab==[]:
            break
        if ab[0][0]<=i:
            insert_index = bisect.bisect_left(l,ab[0][1])  #2
            l.insert(insert_index,ab[0][1])
            del ab[0]
        else:
            break
    if l!=[]:
        ans+=l[-1]
        del l[-1]
        

print(ans)

# Optimized solution
n,m=list(map(int,input().split()))
ab=[]
l=[[] for i in range(m+1)]
for i in range(n):
    tmp=[int(j) for j in input().split()]
    ab.append(tmp)
    a,b=tmp
    if a<=m:
        l[a].append(b)
import heapq
q=[]
ans=0
for i in range(1,1+m):
    for j in l[i]:
        heapq.heappush(q,-j)
    if q:
        ans-=heapq.heappop(q)
print(ans)
# Original solution
n,m= list(map(int, input().split()))
ab_list=[]
for _ in range(n):
    a, b = list(map(int, input().split()))
    ab_list.append([a,b])
ab_list.sort()

import heapq
l = []
heapq.heapify(l)

ind=0
ans=0
for i in range(1,m+1):
    while n>ind and ab_list[ind][0] <= i:
        heapq.heappush(l,-ab_list[ind][1])
        ind+=1

    if len(l) !=0:
        b = -heapq.heappop(l)
        ans+=b
print(ans)

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

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

import heapq
l = []
heapq.heapify(l)

ind=0
ans=0
for i in range(1,m+1):
    while n>ind and ab_list[ind][0] <= i:
        heapq.heappush(l,-ab_list[ind][1])
        ind+=1

    if len(l) !=0:
        b = -heapq.heappop(l)
        ans+=b
print(ans)
# Original solution
import heapq

N, M = [int(_) for _ in input().split()]
ABs = [[] for _ in range(M + 1)]
r = 0

for i in range(N):
    A, B = [int(x) for x in input().split()]
    if A <= M:
        ABs[A].append(-B)

for m in range(1, M + 1):
    B_max = -1
    A_max = -1

    heapq.heapify(ABs[m])

    # print("{0}".format(m))

    for a in range(1, m + 1):
        # print("\t{0}".format(a))
        if len(ABs[a]) == 0:
            continue

        b = - ABs[a][0]
        if B_max < b:
            A_max = a
            B_max = b

    if A_max != -1:
        r -= heapq.heappop(ABs[A_max])

print(r)


# Optimized solution
import heapq

N, M = [int(_) for _ in input().split()]
ABs = [[] for _ in range(M + 1)]
r = 0

for i in range(N):
    A, B = [int(x) for x in input().split()]
    if A <= M:
        ABs[A].append(-B)

q = []

for m in range(1, M + 1):
    if ABs[m] != 0:
        for b in ABs[m]:
            heapq.heappush(q, b)

    if len(q) != 0:
        r -= heapq.heappop(q)

print(r)
# Original solution
import sys
from collections import defaultdict
input = sys.stdin.readline

n, m = list(map(int, input().split()))
dic = defaultdict(list)
total = 0
candidates = []

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

for day in range(1, m+1):
    if day in dic:
        candidates += dic[day]

    if candidates:
        fee = max(candidates)
        total += fee
        candidates.remove(fee)

print(total)

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

n, m = list(map(int, input().split()))
dic = defaultdict(list)
total = 0
candidates = []
heapq.heapify(candidates)

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

for day in range(1, m+1):
    if day in dic:
        for x in dic[day]:
            heapq.heappush(candidates, -x)

    if candidates:
        total -= heapq.heappop(candidates)

print(total)
# Original solution
N, M = list(map(int, input().split()))
ABs = [0] * N

for i in range(N):
    ABs[i] = list(map(int, input().split()))

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

rewards = [0] * (M + 1)

count = 0
for AB in ABs:
    day, reward = AB
    if day <= M:
        if rewards[day] == 0:
            rewards[day] = reward
            count += 1
        else:
            for i in range(day, M + 1):
                if rewards[i] == 0:
                    rewards[i] = reward
                    count += 1
                    break
    if count == M:
        break

result = sum(rewards)
print(result)

# Optimized solution
N, M = list(map(int, input().split()))
ABs = [0] * N
day_dic = {}

for i in range(N):
    ABs[i] = list(map(int, input().split()))

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

rewards = [0] * (M + 1)

count = 0
for AB in ABs:
    day, reward = AB
    if day <= M:
        if rewards[day] == 0:
            rewards[day] = reward
            day_dic[day] = day
            count += 1
        else:
            esti = day_dic.get(day)
            if esti is None:
                esti = day
            for i in range(esti, M + 1):
                if rewards[i] == 0:
                    rewards[i] = reward
                    day_dic[day] = i
                    count += 1
                    break
    if count == M:
        break

result = sum(rewards)
print(result)
# Original solution
import heapq
N,M = list(map(int,input().split()))
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l,reverse=True)
heap = []
ans = 0
for i in range(1,M+1):
        while l and l[-1][0] == i:
            heapq.heappush(heap, (l.pop()[1]*(-1)))
        if heap :
            ans -= (heapq.heappop(heap))
print(ans)


# Optimized solution
import heapq
N,M = list(map(int,input().split()))
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l,reverse=True)
heap = []
ans = 0
for i in range(1,M+1):
        while l and l[-1][0] == i:
            heapq.heappush(heap, -(l.pop()[1]))
        if heap :
            ans -= (heapq.heappop(heap))
print(ans)
# Original solution
import heapq

N,M = list(map(int,(input().split())))
A = [0]*N
B = [0]*N
mission = [[] for _ in range(M+1)]
work = 0

for i in range(N):
    if i <= M:
        heapq.heapify(mission[i])
    A[i],B[i] = list(map(int,(input().split())))
    if A[i] <= M:
        mission[A[i]].append(-B[i])

for day in range(1,M+1):
    mission[day].extend(mission[day-1])
    if mission[day]:
        heapq.heapify(mission[day])
        work += heapq.heappop(mission[day])

print((-work))

# Optimized solution
import heapq

N,M = list(map(int,(input().split())))
A = [0]*N
B = [0]*N
mission = [[] for _ in range(M+1)]
work = 0

for i in range(N):
    if i <= M:
        heapq.heapify(mission[i])
    A[i],B[i] = list(map(int,(input().split())))
    if A[i] <= M:
        mission[A[i]].append(-B[i])

for day in range(1,M+1):
    for i in range(len(mission[day])):
        heapq.heappush(mission[0],mission[day][i])
    if mission[0]:
        work += heapq.heappop(mission[0])

print((-work))
# Original solution
import sys
from collections import defaultdict
n, m = list(map(int, sys.stdin.readline().split()))
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

L =[]
money = 0
for d in range(1, m+1):
    if dic[d]:
        L+= dic[d]
    if L:
        L.sort()
        money += L[-1]
        L.pop()
print(money)

# Optimized solution
import sys
import heapq
from collections import defaultdict
n, m = list(map(int, sys.stdin.readline().split()))
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

#print(dic)
L =[]
money = 0
for d in range(1, m+1):
    for e in dic[d]:
        heapq.heappush(L, -e)
    if L:
        money +=heapq.heappop(L)
print((-money))
# Original solution
import heapq as hq
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() #日が短い順に
ans = 0
idx = 0
q = []
for i in range(1,m+1):
    while idx<n and ab[idx][0] ==i:
        a,mb = ab[idx]
        hq.heappush(q,mb)
        idx+=1
    if q!=[]:
        ans += -1*hq.heappop(q)

print(ans)


# Optimized solution
import heapq as hq
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()
ans = 0
idx = 0
q = []
for i in range(1,m+1):
    while idx<n and ab[idx][0] ==i:
        a,mb = ab[idx]
        hq.heappush(q,mb)
        idx+=1
    if q!=[]:
        ans += -1*hq.heappop(q)

print(ans)
# Original solution
import heapq
n, m = list(map(int, input().split(' ')))
df = []
for _ in range(n):
    x, y = list(map(int, input().split(' ')))
    df.append([x, y])
df.sort()
h = []
result = 0
counter = 0
for i in range(1, m+1):
    while counter < len(df) and df[counter][0] <= i:
        heapq.heappush(h, (-1)*df[counter][1])
        counter += 1
    if len(h) == 0:
        continue
    else:
        result -= heapq.heappop(h)
print(result)

# Optimized solution
def main():
    import heapq
    n, m = list(map(int, input().split(' ')))
    df = []
    for _ in range(n):
        x, y = list(map(int, input().split(' ')))
        df.append([x, y])
    df.sort()
    h = []
    result = 0
    counter = 0
    for i in range(1, m+1):
        while counter < len(df) and df[counter][0] <= i:
            heapq.heappush(h, (-1)*df[counter][1])
            counter += 1
        if len(h) == 0:
            continue
        else:
            result -= heapq.heappop(h)
    print(result)
if __name__ == '__main__':
    main()
# Original solution
import heapq
from collections import deque

N, M=list(map(int,input().split()))
AB=[list(map(int,input().split())) for _ in range(N)]
AB.sort(key=lambda x:x[0])
d=deque(AB)
h=[]
ans=0
for i in range(M+1):
    while d and d[0][0]<=i:
        heapq.heappush(h, -d.popleft()[1])
    if h:
        ans-=heapq.heappop(h)
print(ans)

# Optimized solution
import heapq
from collections import defaultdict

N, M = list(map(int, input().split()))
d = defaultdict(list)
for i in range(N):
    A, B = list(map(int, input().split()))
    d[A].append(-B)

ans = 0
work = []
for i in range(1, M+1):
    for j in range(len(d[i])):
        heapq.heappush(work, d[i][j])
    if len(work):
        ans -= heapq.heappop(work)
print(ans)
# Original solution
import heapq

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

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)


# Optimized solution
import heapq

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

hq = []

ans = 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)
# Original solution
import heapq as pq

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

day_to_reward = {}
for a, b in AB:
    if day_to_reward.get(a) is None:
        day_to_reward[a] = [b]
    else:
        day_to_reward[a].append(b)

ans = 0
candidate = []
for i in range(1, M+1):
    for reward in day_to_reward.get(i, []):
        pq.heappush(candidate, - reward)
    if candidate:
        work_reward = - pq.heappop(candidate)
        ans += work_reward


print(ans)


# Optimized solution
import heapq as pq

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

day_to_reward = {}
for a, b in AB:
    if day_to_reward.get(a) is None:
        day_to_reward[a] = [b]
    else:
        day_to_reward[a].append(b)

ans = 0
candidate = []
for i in range(1, M+1):
    for reward in day_to_reward.get(i, []):
        pq.heappush(candidate, - reward)
    if candidate:
        ans += - pq.heappop(candidate)

print(ans)
# Original solution
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True)
x = [0] * M
cnt = 0
cntIN = M
preB = 0
for a, b in AB:
    if preB != b:
        k = M
    i = min(M-a, k)
    while i>=0 and x[i] != 0:
        i -= 1
    else:
        if i >= 0:
            x[i] = b
            k = i - 1
            cntIN -= 1
            if cntIN <= 0:
                break

print((sum(x)))


# Optimized solution
N, M = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True)
x = [0] * M
p = [x for x in range(M)]

cnt = 0
cntIN = M
for a, b in AB:
    i = M-a

    s = []
    while i >= 0 and p[i] != i:
        s.append(i)
        i = p[i]
    for j in s:
        p[j] = i - 1

    if i >= 0:
        x[i] = b
        p[i] = i - 1
        cntIN -= 1            
        if cntIN <= 0:
            break

print((sum(x)))