CodeEff/ECCO huggingface dataset:
# Original solution
n,m=list(map(int,input().split()))
l=sorted([list(map(int,input().split())) for _ in [0]*n],key=lambda x:[x[0],-x[1]])+[[float('inf'),0]]
for i in range(n):l[i][0]-=1
w=[n]*m
for i in range(m):
    j=0
    while l[j][0]<=i:
        if l[j][1]>l[w[i]][1] and j not in w:
            w[i]=j
        j+=1
print((sum(l[w[i]][1] for i in range(m))))

# Optimized solution
from heapq import heappop, heappush

n,m=list(map(int,input().split()))
a=[]
for i in range(n):
    heappush(a,list(map(int,input().split())))
heap=[]
s=0
for i in range(m):
    while 1:
        if len(a)==0:break
        else:
            if a[0][0]==i+1:heappush(heap,-heappop(a)[1])
            else:break
    if len(heap)>0:s+=-heappop(heap)
print(s)
# Original solution
import heapq

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


d_AB = {a:[] for a,b in AB}
for a,b in AB:
    d_AB[a].append(-b)

H = []
money = 0
for i in range(1,M+1):
    if i in list(d_AB.keys()):
        H += d_AB[i]
    else:
        H += [0]
    #print(H)
    heapq.heapify(H)
    money += heapq.heappop(H)

print((-money))

# Optimized solution
import heapq

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


d_AB = {a:[] for a,b in AB}
for a,b in AB:
    d_AB[a].append(-b)
#print(d_AB)
H = []
heapq.heapify(H)
money = 0
for i in range(1,M+1):
    if i in list(d_AB.keys()):
        for ab in d_AB[i]:
            heapq.heappush(H,ab)
        #print(H)
        money += heapq.heappop(H)
        #print(money)
        
    else:
        money += heapq.heappushpop(H,0)

print((-money))
# Original solution
N, M = list(map(int, input().split()))
AB = {}
for i in range(N):
    a, b = list(map(int, input().split()))
    if a in list(AB.keys()):
        AB[a].append(b)
    else:
        AB[a] = [b]
dp = [0] * (M+1)
for i in range(1, M+1):
    tmp = 0
    km = -1
    tm = -1
    for k in range(i+1):
        if k not in list(AB.keys()):
            continue
        for t in range(len(AB[k])):
            if tmp < AB[k][t]:
                tmp = AB[k][t]
                km = k
                tm = t
    if km >= 0:
        dp[i] = dp[i-1] + tmp
        AB[km][tm] = 0
    else:
        dp[i] = dp[i-1]

print((dp[len(dp)-1]))


# 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(reverse=True)
hp = []
ret = 0
for i in range(1, M+1):
    # push
    while len(AB) > 0 and AB[len(AB)-1][0] <= i:
        heappush(hp, AB.pop()[1]*-1)
    # pop
    if len(hp) > 0:
        ret += heappop(hp)*-1

print(ret)
# 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()))
c=[list(map(int,input().split())) for i in range(n)]
ans, available = 0, []
heapq.heapify(available)
heapq.heapify(c)
for i in range(1, m+1):
    while len(c)!=0 and c[0][0]<=i:
        now = heapq.heappop(c)
        heapq.heappush(available,-now[1])
    if len(available) != 0:
        ans -= heapq.heappop(available)
print(ans)
# Original solution
from operator import itemgetter

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

d = [0] * (m+1)

ab.sort(key=itemgetter(1), reverse=True)

for a, b in ab:
    x = m - a
    while x >= 0:
        if d[x] == 0:
            d[x] = b
            break
        x -= 1

print((sum(d)))

# Optimized solution
from heapq import heappop, heappush

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

h = []

ans = 0

ab.sort()
i = 0
for d in range(1, m+1):
    while i < n:
        if ab[i][0] <= d:
            heappush(h, - ab[i][1])
            i += 1
        else:
            break
    if len(h) > 0:
        ans -= heappop(h)

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

rewards = [None] * N
for i in range(N):
    a, b = list(map(int, input().split()))
    rewards[i] = (b, a)

heap = []
total = 0
for day in range(1, M + 1):
    target_rewards = list([x for x in rewards if (day - 1) < x[1] <= day])

    for reward in target_rewards:
        heapq.heappush(heap, (-reward[0], reward[1]))

    if len(heap) > 0:
        max_reward = heapq.heappop(heap)
        total += -max_reward[0]

print(total)


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

rewards = [[] for _ in range(M + 1)]
for i in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        rewards[a].append(b)

heap = []
total = 0
for day in range(1, M + 1):
    for reward in rewards[day]:
        heapq.heappush(heap, -reward)

    if len(heap) > 0:
        max_reward = heapq.heappop(heap)
        total += -max_reward

print(total)
# Original solution
import bisect

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()


day = 1
i = 0
maxi = []
ans = 0

while day <= M:

    if i < N and AB[i][0] == day:

        ind = bisect.bisect_left(maxi,AB[i][1])
        maxi.insert(ind,AB[i][1])
        i += 1
        
    else:

        day += 1
        if len(maxi) > 0:
            ans += maxi.pop(-1)

print (ans)

    


# Optimized solution
import heapq

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()


day = 1
i = 0
lis = []
ans = 0

while day <= M:

    if i < N and AB[i][0] == day:

        heapq.heappush(lis,-1 * AB[i][1])
        i += 1

    else:

        day += 1

        if len(lis) > 0:
            ans -= heapq.heappop(lis)

print (ans)
# Original solution
def work(l):
    l.sort(key=lambda x:x[1],reverse=True)
    salary = l.pop(0)
    return salary[1]

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

queue = []
ans = 0
i = 0
while i < m:
    i += 1
    for j in range(len(a)):
        if a[j][0] == i:
            queue.append(a[j])
    if len(queue)>0:
        ans += work(queue)
print(ans)

# Optimized 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)

#解法
rewards = 0
heap = []
for i in range(M):
    for reward in jobs[i]:
        heapq.heappush(heap, reward)
    if len(heap) >0:
        reword = heapq.heappop(heap)
        rewards += -reword

print(rewards)
# Original solution
import sys
import heapq

N, M = list(map(int, input().split()))
jobs = []
for line in sys.stdin:
    A, B = list(map(int, line.split()))
    if A > M:
        continue
    jobs.append((A, B))

jobs.sort(reverse=True)

q = []
ans = 0
for i in range(1, M + 1):
    while jobs and jobs[-1][0] <= i:
        A, B = jobs.pop()
        heapq.heappush(q, -B)

    if q:
        ans += -heapq.heappop(q)
print(ans)


# Optimized solution
import sys
import heapq

N, M = list(map(int, input().split()))
possiblyValidJobs = {}
for line in sys.stdin:
    A, B = list(map(int, line.split()))
    if A > M:
        continue

    if A in possiblyValidJobs:
        possiblyValidJobs[A].append(B)
    else:
        possiblyValidJobs[A] = [B]

validJobsOnThisDay = []
ans = 0
for daysBeforeDeadline in range(1, M + 1):
    if daysBeforeDeadline in possiblyValidJobs:
        for wage in possiblyValidJobs[daysBeforeDeadline]:
            heapq.heappush(validJobsOnThisDay, -wage)
    if validJobsOnThisDay:
        ans += -heapq.heappop(validJobsOnThisDay)
print(ans)
# Original solution
import heapq
N, M = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(N)]
ab = sorted(ab, key=lambda x:x[0])
day = 0
pay = 0
q = []
for i in range(1, M+1):
  for j in range(len(ab)):
    if ab[0][0] <= i:
      a, b = ab.pop(0)
      heapq.heappush(q, (-b, a))
    else:
      break
  if len(q) > 0:
    pay += - heapq.heappop(q)[0]

print(pay)


# Optimized solution
import heapq
N, M = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(N)]
day = {i+1: [] for i in range(M)}
for a, b in ab:
  if a <= M:
    day[a] += [-b]
q = []
heapq.heapify(q)
pay = 0
for i in range(M):
  for b in day[i+1]:
    heapq.heappush(q, b)
  if len(q) > 0:
    pay += -heapq.heappop(q)

print(pay)
# Original solution
import heapq
import copy

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

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

baito.sort(key=lambda x: x[0])

que = []
ans = 0
for i in range(1, M+1):
    tmp = copy.copy(baito)
    for val in baito:
        if val[0] <= i:
            heapq.heappush(que, val[1]*-1)
            tmp.pop(0)
        else:
            break
    baito = tmp
    if len(que) != 0:
        ans += heapq.heappop(que)*-1

print(ans)

# Optimized solution
import heapq

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

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

que = []
ans = 0
for i in range(1, M+1):
    if len(baito[i]) != 0:
        for val in baito[i]:
            heapq.heappush(que, val*-1)
    if len(que) != 0:
        ans += heapq.heappop(que)*-1

print(ans)
# Original solution
from heapq import heapify, heappop, heappush
N,M = list(map(int, input().split()))
AB = []
for i in range(N):
    a,b = list(map(int, input().split()))
    if a<=M:
        AB.append((a,-b))
    else:
        N-=1
AB.sort()

Q = []
heapify(Q)
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)

# Optimized solution
from heapq import heapify, heappop, heappush
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()

Q = []
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)
# Original solution

import collections
import heapq

inputs = [int(x) for x in input().split()]
n, m = inputs[0], inputs[1]
conditions = {}
for i in range(n):
  inputs = [int(x) for x in input().split()]
  if inputs[0] in list(conditions.keys()):
    conditions[inputs[0]].append(inputs[1])
  else:
    conditions[inputs[0]] = collections.deque([inputs[1]])

for day in list(conditions.keys()):
  conditions[day] = sorted(conditions[day], reverse=True)

ans = 0
candidates = []
for reversed_day in range(1, m + 1):
  for d in range(1, reversed_day+1):
    if d in list(conditions.keys()):
      heapq.heappush(candidates, -conditions[d].pop(0))
      if len(conditions[d]) == 0:
        del conditions[d]
  if len(candidates) == 0:
    continue
  ans -= heapq.heappop(candidates)

print(ans)

# Optimized solution

import collections
import heapq

inputs = [int(x) for x in input().split()]
n, m = inputs[0], inputs[1]
conditions = collections.deque([])
for i in range(n):
  conditions.append([int(x) for x in input().split()])

conditions = sorted(conditions)

cnt, ans = 0, 0
candidates = []
for reversed_day in range(1, m + 1):
  while cnt < len(conditions) and conditions[cnt][0] <= reversed_day:
    heapq.heappush(candidates, -conditions[cnt][1])
    cnt += 1

  if len(candidates) == 0:
    continue
  ans -= heapq.heappop(candidates)

print(ans)
# Original solution
import queue

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

for n in range(N):
    a, b = list(map(int, input().split()))
    if a > M:
        continue
    AB[a].append(0-b)

earn = 0
q = queue.PriorityQueue()

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

    for job in AB[m]:
        q.put(job)

    if not q.empty():
        earn += q.get()

print((0-earn))


# Optimized solution
import heapq

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

for n in range(N):
    a, b = list(map(int, input().split()))
    if a > M:
        continue
    AB[a].append(0-b)

earn = 0
q = []

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

    for job in AB[m]:
        heapq.heappush(q, job)

    if q != []:
        earn += heapq.heappop(q)

print((0-earn))
# Original solution
N, M = list(map(int, input().split()))
from collections import deque
AB = deque(sorted([list(map(int, input().split())) for i in range(N)]))

from heapq import *
q = []
heapify(q)
ans = 0
remain_days = 1
while remain_days <= M:
    for a, b in list(AB):
        if a <= remain_days:
            heappush(q, -1 * AB.popleft()[1])
    if q:
        v = heappop(q)
        ans += -1 * v
    remain_days += 1
print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
jobs = sorted([list(map(int, input().split())) for i in range(N)], reverse=True)

from heapq import *
q = []
heapify(q)
ans = 0
for i in range(1, M+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -1*jobs.pop()[1])
    if q:
        ans += -1 * heappop(q)
print(ans)
# Original solution
import heapq
N, M = list(map(int, input().split()))
w = [[] for i in range(M)]
for i in range(N):
    a, b = list(map(int, input().split()))
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
for i in range(M):
    a += w[i]
    heapq.heapify(a)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)

# Optimized solution
import heapq
N, M = list(map(int, input().split()))
w = [[] for i in range(M)]
for i in range(N):
    a, b = list(map(int, input().split()))
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)
# Original solution
import bisect

N, M = list(map(int, input().split()))
income = [[] for _ in range(M+1)] # income[i]はi日後に報酬が得られる全ての仕事についての報酬のリスト
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        bisect.insort_left(income[a], b)

# 後ろから考える。残りi日のときに終了までに報酬が得られる仕事のうち報酬最大のものを選ぶ
ans = 0
for rest_days in range(1, M+1):
    if income[rest_days]:
        ans += income[rest_days].pop(-1)
        if rest_days < M:
            income[rest_days+1] = sorted(income[rest_days+1] + income[rest_days])

print(ans)

# Optimized solution
import heapq

N, M = list(map(int, input().split()))
income = [[] for _ in range(M+1)]  # income[i]はi日後に報酬が得られる全ての仕事についての報酬のリスト
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        income[a].append(-b)

# 後ろから考える。残りi日のときに終了までに報酬が得られる仕事のうち報酬最大のものを選ぶ
ans = 0
tmp = []
for rest_days in range(1, M+1):
    tmp = list(heapq.merge(tmp, income[rest_days]))
    heapq.heapify(tmp)
    if tmp:
        ans += heapq.heappop(tmp)

print((-ans))
# Original solution
from collections import defaultdict
import heapq
n, m = list(map(int, input().split()))
d = defaultdict(list)
for i in range(n):
    a, b = list(map(int, input().split()))
    d[a-1].append(-b)
cnt = 0
a = []
for i in range(m):
    a += d[i]
    heapq.heapify(a)
    if a:
        cnt += -heapq.heappop(a)
print(cnt)

# Optimized solution
from collections import defaultdict
import heapq
n, m = list(map(int, input().split()))
d = defaultdict(list)
for i in range(n):
    a, b = list(map(int, input().split()))
    d[a-1].append(-b)
cnt = 0
a = []
heapq.heapify(a)
for i in range(m):
    for j in d[i]:
        heapq.heappush(a, j)
    if a:
        cnt += -heapq.heappop(a)
print(cnt)
# Original solution
import bisect

n, m = list(map(int, input().split()))
ls = [list(map(int, input().split())) for _ in range(n)]
ls.sort(key=lambda x: x[0])
la = [x[0] for x in ls]
lb = [x[1] for x in ls]

selected = []
res = 0
for i in range(1, m + 1):
    k = bisect.bisect(la, i)
    bs = lb[:k]
    index = -1
    temp = 0
    for j, b in enumerate(bs):
        if j in selected:
            continue
        if temp < b:
            index = j
            temp = b
    if index >= 0:
        selected.append(index)
        res += temp

print(res)

# Optimized solution
import heapq

n, m = list(map(int, input().split()))
ls = [list(map(int, input().split())) for _ in range(n)]
ls.sort(key=lambda x: x[0])

q = []
res = 0
index = 0
for i in range(1, m + 1):
    while index < n and ls[index][0] <= i:
        heapq.heappush(q, -ls[index][1])
        index += 1
    if len(q) > 0:
        res += -heapq.heappop(q)
print(res)
# Original solution
import sys
import heapq 
input = sys.stdin.readline
n,m = list(map(int,input().split()))
alwork = [() for i in range(10**5+1)]
w = []
hpop = heapq.heappop
hpush = heapq.heappush
ans = 0

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

for i in range(1,m+1):
  for j in alwork[i]:
    hpush(w,j)
  if w:
    ans += hpop(w)
print((-ans))

# Optimized solution
import sys
import heapq 
input = sys.stdin.readline
n,m = list(map(int,input().split()))
l=[tuple(map(int,input().split())) for i in range(n)]
l.sort(reverse=True)

w = []
hpop = heapq.heappop
hpush = heapq.heappush
ans = 0

for i in range(1,m+1):
  while l and l[-1][0]==i:
    hpush(w,-l.pop()[1])
  if w:
    ans += hpop(w)
print((-ans))
# Original solution
import heapq

N,M = (int(x) for x in input().split())
AB = []
sum = 0

for i in range(N):
    a,b = (int(x) for x in input().split())
    AB.append([a,b])

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

for i in range(M+1):
    while j<N and AB[j][0] < i+1:
        heapq.heappush(heap,-AB[j][1])
        j += 1

    if len(heap):
        heapq.heapify(heap)
        sum += -heapq.heappop(heap)
                           
print(sum)
    


# Optimized solution
import heapq

N,M = (int(x) for x in input().split())
AB = []
sum = 0

for i in range(N):
    a,b = (int(x) for x in input().split())
    AB.append([a,b])

AB = sorted(AB , key = lambda x : x[0])
heap = []
heapq.heapify(heap)
j = 0

for i in range(M+1):
    while j<N and AB[j][0] < i+1:
        heapq.heappush(heap,-AB[j][1])
        j += 1

    if len(heap):
        sum += -heapq.heappop(heap)
                           
print(sum)
# Original solution
if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    ab = [list(map(int, input().split())) for i in range(n)]
    ab = sorted(ab, key=lambda x: x[1], reverse=True)

    free_day = set(list(range(m + 1)))
    answer = 0
    for i in range(n):
        a = ab[i][0]
        b = ab[i][1]
        work_days = {day for day in free_day if m-day >= a}
        if len(work_days) != 0:
            free_day.remove(max(work_days))
            answer += b
    print(answer)


# Optimized solution
from heapq import heappush, heappop


#  https://atcoder.jp/contests/abc137/submissions/6840855 を参考にした。
q = []
if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    ab = [list(map(int, input().split())) for i in range(n)]
    ab = sorted(ab, reverse=True)

    q = []
    answer = 0
    for i in range(m-1, -1, -1):
        while ab and i + ab[-1][0] <= m:
            (a, b) = ab.pop()
            heappush(q, -b)

        if q:
            answer -= heappop(q)

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

N, M = list(map(int, input().split()))
Wlist = []
for i in range(N):
    A, B = list(map(int, input().split()))
    Wlist.append([A,B])
Wlist.sort(key=lambda x:x[0])

Ans = 0
for day in range(1,M+1):
    Best = 0
    BestIdx = -1
    for i, W in enumerate(Wlist):
        if W[0] > day:
            break
        if Best < W[1]:
            Best = W[1]
            BestIdx = i
    if BestIdx >= 0:
        Ans += Best
        Wlist[BestIdx][1] = 0
            
print(Ans)


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

N, M = list(map(int, input().split()))
Wlist = []
for i in range(N):
    A, B = list(map(int, input().split()))
    Wlist.append([A,B])
Wlist.sort(key=lambda x:x[0])

h = []
Ans = 0
Index = 0
for day in range(1,M+1):
    while Index < N:
        if Wlist[Index][0] <= day:
            heapq.heappush(h, -Wlist[Index][1])
            Index += 1
        else:
            break
    if len(h):
        Ans += -heapq.heappop(h)

print(Ans)
# Original solution
import sys
from collections import deque
input = sys.stdin.readline

def main():
    n,m=list(map(int,input().split()))
    ab=[list(map(int,input().split())) for _ in range(n)]
    ab.sort()
    c=[0]*n
    ans=0
    for i in range(1,m+1):
        mb=0
        mnum=-1
        for j in range(n):
            if ab[j][0]<=i:
                if mb<ab[j][1] and c[j]==0:
                    mb=ab[j][1]
                    mnum=j
            else:
                break
        ans+=mb
        if mnum!=-1:
            c[mnum]=1
    print(ans)

if __name__ == '__main__':
    main()

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

def main():
    n,m=list(map(int,input().split()))
    ab=[]
    for i in range(n):
        a,b=list(map(int,input().split()))
        if a>m:
            continue
        ab.append((a,b))
    ab.sort(reverse=True)    
    q=[]
    ans=0
    for i in range(1,m+1):
        while ab and ab[-1][0]<=i:
            a,b=ab.pop()
            heappush(q,-b)
        if q:
            b=-heappop(q)
            ans+=b
    print(ans)

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

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

ans = 0
candidates = []
now = 0
for i in range(M+1):
  for j in range(now, N):
    if AB[j][0] <= i:
      heapq.heappush(candidates, -AB[j][1])
      now = j + 1
      
    else:
      now = j
      break
  
  if candidates:
    ans -= heapq.heappop(candidates)

print(ans)

# Optimized solution
# イベントソートでもできると思う

import heapq

N, M = list(map(int, input().split()))
event_list = [[m, 10**5] for m in range(1, M+1)]
for i in range(N):
  a, b = list(map(int, input().split()))
  event_list.append([a, b])


event_list.sort()
jobs = []
ans = 0
for event in event_list:
  if event[1] < 10**5:
    heapq.heappush(jobs, -event[1])
  else:
    if jobs:
      ans -= heapq.heappop(jobs)

print(ans)