AtCoder Beginner Contest 137 - Summer Vacation

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.
You can take and complete at most one of these jobs in a day.
However, you cannot retake a job that you have already done.
Find the maximum total reward that you can earn no later than M days from today.
You can already start working today.

-----Constraints-----
 - All values in input are integers.
 - 1 \leq N \leq 10^5
 - 1 \leq M \leq 10^5
 - 1 \leq A_i \leq 10^5
 - 1 \leq B_i \leq 10^4

-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

-----Output-----
Print the maximum total reward that you can earn no later than M days from today.

-----Sample Input-----
3 4
4 3
4 1
2 2

-----Sample Output-----
5

You can earn the total reward of 5 by taking the jobs as follows:
 - Take and complete the first job today. You will earn the reward of 3 after four days from today.
 - Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Source: atcoder | Difficulty: MEDIUM


Solutions:
import heapq
(N, M) = 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)


from heapq import *
(n, m) = map(int, input().split())
x = [[] for _ in range(m)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		x[a - 1] += [-b]
q = []
ans = 0
for i in range(m):
	for j in x[i]:
		heappush(q, j)
	if q:
		ans -= heappop(q)
print(ans)


import bisect
(N, M) = map(int, input().split())
A = []
for _ in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		A.append([a, b])
A.sort(reverse=True)
import heapq
h = []
ans = 0
for m in range(1, M + 1):
	while A and A[-1][0] == m:
		heapq.heappush(h, A.pop()[1] * -1)
	if h:
		ans -= heapq.heappop(h)
print(ans)


import heapq
(N, M) = map(int, input().split())
H = [[] for _ in range(M)]
for _ in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		H[A - 1].append(-B)
Q = []
r = 0
for d in range(M):
	for v in H[d]:
		heapq.heappush(Q, v)
	if Q:
		r -= heapq.heappop(Q)
print(r)


from heapq import *
(N, M) = map(int, input().split())
L = [[] for m in range(M)]
H = []
ans = 0
for n in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		L[M - a] += [b]
for m in range(M - 1, -1, -1):
	for l in L[m][::-1]:
		heappush(H, -l)
	if 0 < len(H):
		ans -= heappop(H)
print(ans)


import heapq
(N, M) = map(int, input().split())
AB = [[0] * 2] * N
for i in range(N):
	AB[i] = list(map(int, input().split()))
AB.sort()
x = []
(ans, j) = (0, 0)
for i in range(1, M + 1):
	while j != N and AB[j][0] <= i:
		heapq.heappush(x, -AB[j][1])
		j += 1
	if len(x) != 0:
		ans -= heapq.heappop(x)
print(ans)


(n, m) = map(int, input().split())
pay = [[] for i in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		pay[a].append(b)
import heapq as hq
ans = 0
stack = []
for i in range(m + 1):
	for j in pay[i]:
		hq.heappush(stack, -j)
	ans -= hq.heappop(stack) if stack else 0
print(ans)


from heapq import *
(n, m) = map(int, input().split())
abab = [list(map(int, input().split())) for _ in range(n)]
bites = [[] for _ in range(m)]
for (a, b) in abab:
	if a <= m:
		bites[a - 1].append(-b)
ans = 0
board = []
for bite in bites:
	for b in bite:
		heappush(board, b)
	if board:
		ans -= heappop(board)
print(ans)


from heapq import *
(N, M, *t) = map(int, open(0).read().split())
v = [[] for _ in '_' * M]
z = i = 0
for a in t[::2]:
	a > M or v[a - 1].append(t[i + 1])
	i += 2
q = [0] * M
for i in v:
	for j in i:
		heappush(q, -j)
	z -= heappop(q)
print(z)


def main():
	import sys
	sys.setrecursionlimit(10 ** 9)
	input = sys.stdin.readline
	from heapq import heappush, heappop
	(N, M) = list(map(int, input().split()))
	AB = [[] for _ in range(100000)]
	for _ in range(N):
		(a, b) = list(map(int, input().split()))
		AB[a - 1].append(-b)
	ans = 0
	heap = []
	for a in range(M):
		for b in AB[a]:
			heappush(heap, b)
		if heap:
			ans -= heappop(heap)
	print(ans)
main()


(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
r = 0
from heapq import heappop, heappush
q = []
l = [[] for i in range(10 ** 5)]
for (a, b) in AB:
	l[a - 1].append(b)
for i in range(M):
	for e in l[i]:
		heappush(q, -e)
	if len(q) > 0:
		r += -heappop(q)
print(r)


(N, M) = map(int, input().split())
B = [tuple(map(int, input().split())) for _ in range(N)]
B.sort(reverse=True)
import heapq
hq = []
ans = 0
for i in range(1, M + 1):
	while B and B[-1][0] == i:
		heapq.heappush(hq, B.pop()[1] * -1)
	if hq:
		ans -= heapq.heappop(hq)
print(ans)


from heapq import heappop, heappush
from collections import defaultdict
(N, M) = map(int, input().split())
D = defaultdict(list)
for _ in range(N):
	(A, B) = map(int, input().split())
	D[A].append(B)
H = []
ans = [0] * (M + 1)
for i in range(1, M + 1):
	for j in D[i]:
		heappush(H, -j)
	if H:
		a = heappop(H)
		ans[i] = -a
print(sum(ans))


def resolve():
	import heapq
	(n, m) = map(int, input().split())
	ab = [list(map(int, input().split())) for _ in range(n)]
	b = [[] for _ in range(10 ** 5 + 1)]
	for (i, j) in ab:
		b[i].append(-j)
	q = []
	ans = 0
	for i in range(1, m + 1):
		for j in b[i]:
			heapq.heappush(q, j)
		if q:
			ans += heapq.heappop(q)
	print(-ans)

def __starting_point():
	resolve()
__starting_point()


import heapq
(N, M) = map(int, input().split())
PJ = [[] for _ in range(M)]
for _ in range(N):
	(a, b) = map(int, input().split())
	if M - a >= 0:
		PJ[M - a].append(b)
q = []
cnt = M - 1
ans = 0
while cnt >= 0:
	for b in PJ[cnt]:
		heapq.heappush(q, -b)
	if len(q) > 0:
		src = heapq.heappop(q)
		ans += src
	cnt -= 1
print(-ans)


import heapq
(n, m) = map(int, input().split())
c = [[] for _ in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		c[a].append(-b)
ans = 0
q = []
for i in range(1, m + 1):
	for v in c[i]:
		heapq.heappush(q, v)
	if q:
		ans -= heapq.heappop(q)
print(ans)


from heapq import *
((N, M), *t) = [map(int, s.split()) for s in open(0)]
q = [0] * 99 * M
v = [[] for _ in q]
z = 0
for (a, b) in t:
	v[a - 1] += [b]
for i in v[:M]:
	for j in i:
		heappush(q, -j)
	z -= heappop(q)
print(z)


import heapq
(n, m) = map(int, input().split())
x = [[] for _ in range(m + 1)]
y = []
heapq.heapify(y)
for i in range(1, m + 1):
	heapq.heapify(x[i])
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		heapq.heappush(x[a], -b)
ans = 0
for i in range(1, m + 1):
	if x[i]:
		s = heapq.heappop(x[i])
		heapq.heappush(y, [s, i])
	if y:
		(s, t) = heapq.heappop(y)
		ans -= s
		if x[t]:
			s = heapq.heappop(x[t])
			heapq.heappush(y, [s, t])
print(ans)


import heapq
(n, m) = map(int, input().split())
ls = [list(map(int, input().split())) for i in range(n)]
ls = sorted(ls, key=lambda x: (x[0], -x[1]))
ans = 0
a = []
j = 0
for i in range(m - 1, -1, -1):
	while j < len(ls) and ls[j][0] == m - i:
		heapq.heappush(a, ls[j][1] * -1)
		j += 1
	if a:
		ans += -1 * heapq.heappop(a)
print(ans)


import heapq
(n, m) = list(map(int, input().split()))
ab = [[] for i in range(m + 1)]
d = []
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if not a > m:
		ab[a - 1].append(-1 * b)
res = 0
cnt = 0
for i in range(m):
	for j in range(len(ab[i])):
		heapq.heappush(d, ab[i][j])
	if len(d):
		res += heapq.heappop(d)
print(-1 * res)


import heapq
(N, M) = map(int, input().split())
AB = [[] for i in range(10 ** 5 + 1)]
for i in range(N):
	(A, B) = map(int, input().split())
	AB[A].append(-B)
hq = []
heapq.heapify(hq)
ans = 0
for i in range(1, M + 1):
	for b in AB[i]:
		heapq.heappush(hq, b)
	if len(hq) > 0:
		ans += -heapq.heappop(hq)
print(ans)


import heapq
(N, M) = map(int, input().split())
jobs = [[] for _ in range(M)]
for _ in range(N):
	(A, B) = map(int, input().split())
	if A > M:
		continue
	jobs[A - 1].append(-B)
total = 0
heap = []
for i in range(M):
	for job in jobs[i]:
		heapq.heappush(heap, job)
	if len(heap) == 0:
		continue
	else:
		total += -heapq.heappop(heap)
print(total)


(N, M) = map(int, input().split())
AB = [[] for _ in range(10 ** 5 + 1)]
for _ in range(N):
	(a, b) = map(int, input().split())
	AB[a].append(b)
import heapq
h = []
heapq.heapify(h)
ans = 0
for a in range(1, M + 1):
	for b in AB[a]:
		heapq.heappush(h, -b)
	heapq.heappush(h, 0)
	ans -= heapq.heappop(h)
print(ans)


import heapq
(N, M) = map(int, input().split())
P = [[] for _ in range(M)]
for _ in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		P[A - 1].append(-B)
ans = 0
hq = []
for i in range(M):
	for d in P[i]:
		heapq.heappush(hq, d)
	if len(hq) != 0:
		ans += heapq.heappop(hq)
print(abs(ans))


import sys
readline = sys.stdin.readline
(N, M) = map(int, readline().split())
from collections import defaultdict
tasks = defaultdict(list)
for i in range(N):
	(a, b) = map(int, readline().split())
	tasks[a].append(b)
import heapq as hq
q = []
ans = 0
for i in range(1, M + 1):
	newtask = tasks[i]
	for t in newtask:
		hq.heappush(q, -t)
	if q:
		ans += -hq.heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
c = [[] for i in range(10 ** 5 + 10)]
for i in range(n):
	(a, b) = map(int, input().split())
	c[a].append(-b)
h = []
heapq.heapify(h)
ans = 0
for i in range(m):
	for x in c[i + 1]:
		heapq.heappush(h, x)
	if h:
		ans += heapq.heappop(h)
print(-ans)


import heapq
(N, M) = map(int, input().split())
J = [[] for k in range(M + 1)]
for k in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		J[A].append(B)
Q = []
ans = 0
for d in range(1, M + 1):
	for e in J[d]:
		heapq.heappush(Q, -e)
	if len(Q) > 0:
		ans += -heapq.heappop(Q)
print(ans)


from collections import defaultdict
import heapq
(n, m) = map(int, input().split())
d = defaultdict(list)
for i in range(n):
	(a, b) = 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)


(n, m) = map(int, input().split())
jobs = []
for i in range(n):
	(a, b) = map(int, input().split())
	jobs.append((a, -b))
jobs.sort(reverse=True)
import heapq
hp = []
ans = 0
for i in range(1, m + 1):
	while jobs and jobs[-1][0] <= i:
		heapq.heappush(hp, jobs.pop()[1])
	if hp:
		ans -= heapq.heappop(hp)
print(ans)


from heapq import *
(n, m) = map(int, input().split())
a = sorted([list(map(int, input().split())) for i in range(n)]) + [[m + 1, 2]]
(b, c, d) = ([], 0, 0)
for i in range(1, m + 1):
	while a[c][0] == i:
		heappush(b, -a[c][1])
		c += 1
	else:
		if len(b) != 0:
			d += -heappop(b)
print(d)


import heapq
(n, m) = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(n)] + [[1000000, 1000000]]
li.sort()
idx = 0
hq = []
ans = 0
for i in range(m):
	while li[idx][0] <= i + 1:
		heapq.heappush(hq, -li[idx][1])
		idx += 1
	if hq:
		ans -= heapq.heappop(hq)
print(ans)


(N, M) = map(int, input().split())
A_B = [[0] for i in range(M)]
for i in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		A_B[A - 1].append(-B)
import heapq
q = []
ans = 0
heapq.heapify(q)
for i in range(M):
	for j in A_B[i]:
		heapq.heappush(q, j)
	ans -= heapq.heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
l = [[] for i in range(m)]
s = []
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		l[a - 1].append(b)
t = 0
heapq.heapify(s)
for i in range(m):
	for j in range(len(l[i])):
		heapq.heappush(s, -l[i][j])
	if len(s) > 0:
		t -= heapq.heappop(s)
print(t)


import heapq
import sys
input = sys.stdin.readline
(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
AB.sort(key=lambda x: x[0])
AB.append([10 ** 5 + 1, 0])
h = []
ans = 0
for i in range(1, M + 1):
	while AB[0][0] <= i:
		heapq.heappush(h, -AB.pop(0)[1])
	if len(h) > 0:
		ans += -heapq.heappop(h)
print(ans)


import sys
from heapq import heappush, heappop
lines = []
for line in sys.stdin:
	lines.append(line.rstrip('\r\n'))
(n, m) = lines[0].split()
n = int(n)
m = int(m)
a = []
for i in range(n):
	(x, y) = lines[i + 1].split()
	x = int(x)
	y = int(y) * -1
	a.append((x, y))
a.sort()
a.append((0, 0))
h = []
cur = 0
summ = 0
for i in range(1, m + 1):
	while cur < n and a[cur][0] <= i:
		heappush(h, a[cur][1])
		cur += 1
	if len(h) > 0:
		summ += heappop(h) * -1
print(summ)


import sys, math
from functools import lru_cache
import numpy as np
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

def input():
	return sys.stdin.readline()[:-1]

def mi():
	return list(map(int, input().split()))

def ii():
	return int(input())

def i2(n):
	tmp = [list(mi()) for i in range(n)]
	return [list(i) for i in zip(*tmp)]

def lcm(a, b):
	return a * b // math.gcd(a, b)

def main():
	(N, M) = mi()
	(A, B) = i2(N)
	ans = 0
	d = defaultdict(list)
	for i in range(N):
		d[A[i]].append(B[i])
	h = []
	heapq.heapify(h)
	for i in range(1, M + 1):
		for v in d[i]:
			heapq.heappush(h, -v)
		if h:
			ans -= heapq.heappop(h)
		else:
			continue
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
ab = [[] for _ in range(m + 1)]
for i in range(n):
	t = [int(inp) for inp in input().split()]
	if t[0] <= m:
		ab[t[0]].append(-t[1])
ans = 0
import heapq
h = ab[1]
heapq.heapify(h)
for i in range(1, m + 1):
	if len(h) > 0:
		ans += heapq.heappop(h)
	if i != m:
		for t in ab[i + 1]:
			heapq.heappush(h, t)
print(-ans)


import heapq
(N, M) = map(int, input().split())
AB = [[] for i in range(10 ** 5 + 1)]
for i in range(N):
	(a, b) = map(int, input().split())
	AB[a].append(-b)
q = []
heapq.heapify(q)
ans = 0
for i in range(1, M + 1):
	for b in AB[i]:
		heapq.heappush(q, b)
	if len(q) > 0:
		ans += -heapq.heappop(q)
print(ans)


import heapq
(N, M) = map(int, input().split())
w = [list(map(int, input().split())) for _ in range(N)]
ans = 0
daylist = [[] for _ in range(10 ** 5)]
for (a, b) in w:
	daylist[a - 1].append(b)
q = []
for i in range(M):
	for b in daylist[i]:
		heapq.heappush(q, -b)
	if q:
		ans += -heapq.heappop(q)
print(ans)


from collections import defaultdict
import heapq
dct = defaultdict(list)
(n, m) = map(int, input().split())
for i in range(n):
	(a, b) = map(int, input().split())
	dct[a].append(-b)
hq = []
val = 0
for i in range(1, m + 1):
	if dct[i]:
		for item in dct[i]:
			heapq.heappush(hq, item)
	if hq:
		val += heapq.heappop(hq)
print(-val)


import heapq
(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
h = []
i = 0
ans = 0
for m in range(M - 1, -1, -1):
	while i < N and AB[i][0] + m <= M:
		heapq.heappush(h, -AB[i][1])
		i += 1
	if len(h) != 0:
		ans -= heapq.heappop(h)
print(ans)


import heapq as hq
import numpy as np
(N, M) = map(int, input().split())
job = [list(map(int, input().split())) for _ in range(N)]
ans = 0
job.sort()
job.append([np.inf, np.inf])
jobhq = []
index = 0
for day in range(1, M + 1):
	while day >= job[index][0]:
		hq.heappush(jobhq, -job[index][1])
		index += 1
	if jobhq:
		ans += -hq.heappop(jobhq)
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = []
for i in range(m + 1):
	ab.append([])
for i in range(n):
	(a, b) = map(int, input().split())
	if a > m:
		continue
	ab[a].append(b)
h = []
s = 0
for i in range(1, m + 1):
	for v in ab[i]:
		heapq.heappush(h, -1 * v)
	if len(h) > 0:
		s += -1 * heapq.heappop(h)
print(s)


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])
x = 0
res = 0
hq = []
heapq.heapify(hq)
for i in range(M):
	for j in range(x, N):
		if AB[j][0] > i + 1:
			x = j
			break
		heapq.heappush(hq, AB[j][1] * -1)
	else:
		x = N
	if hq:
		res += heapq.heappop(hq) * -1
print(res)


import heapq
(N, M) = map(int, input().split())
arubaito = [list(map(int, input().split())) for _ in range(N)]
arubaito.sort()
arubaito2 = [[] for _ in range(M + 1)]
for i in range(N):
	if arubaito[i][0] <= M:
		arubaito2[arubaito[i][0]].append(arubaito[i][1])
for i in range(M):
	arubaito2[i].sort(reverse=True)
target = []
ans = 0
heapq.heapify(target)
for i in range(1, M + 1):
	for k in arubaito2[i]:
		heapq.heappush(target, k * -1)
	if len(target) > 0:
		ans += heapq.heappop(target) * -1
print(ans)


import heapq
(n, m) = map(int, input().split())
l = sorted([list(map(int, input().split())) for i in range(n)])
hp = []
ans = 0
d = 0
for i in range(1, m + 1):
	while d < n:
		if l[d][0] <= i:
			heapq.heappush(hp, -l[d][1])
			d += 1
		else:
			break
	if hp != []:
		ans += -heapq.heappop(hp)
print(ans)


import sys
import heapq
input = sys.stdin.readline
(n, m) = 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)


import heapq as h
(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[0])
hq = []
x = 0
d = 0
for i in range(1, m + 1):
	while d < n and a[d][0] <= i:
		h.heappush(hq, -a[d][1])
		d += 1
	if len(hq):
		x += -h.heappop(hq)
print(x)


import heapq as hp
(n, m) = map(int, input().split())
AB = [[] for i in range(m + 1)]
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		AB[a].append(-b)
ans = 0
hp.heapify(l)
for ab in AB:
	for x in ab:
		hp.heappush(l, x)
	if len(l) > 0:
		ans -= hp.heappop(l)
print(ans)


from heapq import heappush, heappop
(N, M) = map(int, input().split())
AtoB = [[] for _ in range(M + 1)]
for i in range(N):
	(A, B) = map(int, input().split())
	if A > M:
		continue
	AtoB[A].append(B)
result = 0
que = []
for Bs in AtoB:
	for B in Bs:
		heappush(que, -B)
	if que:
		result -= heappop(que)
print(result)


import heapq
(n, m) = map(int, input().split())
ab = []
for i in range(n):
	ab.append(list(map(int, input().split())))
ab.sort()
ans = 0
num = 0
queue = []
heapq.heapify(queue)
for i in range(1, m + 1):
	if num < n:
		while ab[num][0] <= i:
			heapq.heappush(queue, [-ab[num][1], ab[num][0]])
			num += 1
			if num == n:
				break
	if len(queue):
		temp = heapq.heappop(queue)
		ans += -temp[0]
print(ans)


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

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

def resolve():
	(N, M) = LI()
	AB = [LI() for _ in range(N)]
	W = [[] for _ in range(M + 1)]
	for (a, b) in AB:
		if 0 <= M - a:
			W[M - a].append(b)
	ans = 0
	hq = []
	for i in reversed(W):
		for j in i:
			heapq.heappush(hq, -j)
		if hq:
			ans -= heapq.heappop(hq)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


import heapq
(N, M) = map(int, input().split())
job = [[] for _ in range(M + 1)]
q = []
for i in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		job[a].append(b)
ans = 0
for day in range(M + 1):
	for value in job[day]:
		heapq.heappush(q, value * -1)
	if len(q) > 0:
		ans += heapq.heappop(q) * -1
print(ans)


import heapq
(n, m) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort()
pq = []
(res, i) = (0, 0)
for day in range(1, m + 1):
	while i < n and AB[i][0] <= day:
		heapq.heappush(pq, -AB[i][1])
		i += 1
	if pq:
		res += -heapq.heappop(pq)
print(res)


from heapq import *
(n, m) = map(int, input().split())
l = [[] for i in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a < m + 1:
		l[a].append(b)
ans = 0
a = []
for i in range(1, m + 1):
	for j in l[i]:
		heappush(a, -j)
	if a:
		ans -= heappop(a)
print(ans)


import heapq
(n, m) = map(int, input().split())
lis = [[] for _ in range(m + 1)]
for i in range(n):
	(a, b) = 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)


import heapq
(n, m) = map(int, input().split())
ab = [[] for _ in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a < m + 1:
		ab[a - 1].append(-b)
h = []
ans = 0
for i in range(m):
	for j in ab[i]:
		heapq.heappush(h, j)
	if len(h) == 0:
		continue
	ans += heapq.heappop(h) * -1
print(ans)


from collections import defaultdict
from heapq import heappush, heappop
(N, M) = list(map(int, input().split()))
jobs = defaultdict(list)
heap = []
ans = 0
for _ in range(N):
	(a, b) = list(map(int, input().split()))
	if a <= M:
		jobs[a].append(b)
for m in range(1, M + 1):
	for b in jobs[m]:
		heappush(heap, -b)
	if heap:
		maxb = -heappop(heap)
		ans += maxb
print(ans)


import heapq
(N, M) = map(int, input().split())
jobs = [[] for i in range(M)]
for i in range(N):
	(a, b) = map(int, input().split())
	if a > M:
		continue
	else:
		jobs[M - a].append(b)
ans = 0
l = []
for i in range(M - 1, -1, -1):
	for j in jobs[i]:
		heapq.heappush(l, -j)
	if len(l):
		ans += -heapq.heappop(l)
print(ans)


import sys
from heapq import heappush, heappop
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	(n, m) = list(map(int, input().split()))
	B = [[] for _ in range(m)]
	for _ in range(n):
		(a, b) = list(map(int, input().split()))
		if m - a >= 0:
			B[m - a].append(b)
	que = []
	res = 0
	for i in reversed(list(range(m))):
		for b in B[i]:
			heappush(que, -b)
		if que:
			res += heappop(que) * -1
	print(res)

def __starting_point():
	resolve()
__starting_point()


from heapq import heappop, heappush
(N, M) = 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)


import heapq
(n, m) = map(int, input().split())
limit = [[] for i in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a > m:
		continue
	limit[m - a].append(b)
available = []
ans = 0
for i in range(m - 1, -1, -1):
	for item in limit[i]:
		heapq.heappush(available, -item)
	if available:
		ans += -heapq.heappop(available)
print(ans)


import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce

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

def INT():
	return int(input())

def MAP():
	return list(map(int, input().split()))

def LIST():
	return list(map(int, input().split()))

def ZIP(n):
	return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
(N, M) = MAP()
AB = [LIST() for _ in range(N)]
AB.sort(key=lambda x: x[0])
ans = 0
i = 0
B = []
for j in range(1, M + 1):
	while i < N and AB[i][0] <= j:
		heappush(B, -AB[i][1])
		i += 1
	if B:
		ans += -heappop(B)
print(ans)


import heapq
(n, m) = map(int, input().split())
q = [[] for i in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	a -= 1
	if a < m:
		heapq.heappush(q[a], -b)
a = 0
cur = []
for i in range(m):
	for j in q[i]:
		heapq.heappush(cur, j)
	if cur:
		a += heapq.heappop(cur)
print(-a)


import heapq
(n, m) = map(int, input().split())
c = [[] for i in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		c[a].append(-b)
d = []
heapq.heapify(d)
ans = 0
for i in range(1, m + 1):
	for j in range(len(c[i])):
		heapq.heappush(d, c[i][j])
	if d != []:
		ans += heapq.heappop(d)
print(-ans)


import heapq

def main():
	(n, m) = map(int, input().split())
	data = {}
	priorityQ = []
	for _ in range(n):
		(a, b) = map(int, input().split())
		if a <= m:
			if a in data:
				data[a].append(b)
			else:
				data[a] = [b]
	ans = 0
	for i in range(1, m + 1):
		if i in data.keys():
			for k in data[i]:
				heapq.heappush(priorityQ, -k)
		if priorityQ:
			ans -= heapq.heappop(priorityQ)
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


from heapq import heappop, heappush
import sys

def read_str():
	return sys.stdin.readline().strip()

def read_int():
	return int(sys.stdin.readline().strip())

def read_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def read_str_split():
	return list(sys.stdin.readline().strip())

def read_int_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def Main():
	(n, m) = read_ints()
	query = [[] for _ in range(m)]
	for _ in range(n):
		(a, b) = read_ints()
		if m - a < 0:
			continue
		query[m - a].append(b)
	total = 0
	que = []
	for (i, b) in enumerate(query):
		for x in b:
			heappush(que, x)
			total += x
		while len(que) > i + 1:
			x = heappop(que)
			total -= x
	print(total)

def __starting_point():
	Main()
__starting_point()


from collections import defaultdict
import heapq
(N, M) = map(int, input().split())
l = defaultdict(list)
for i in range(N):
	(A, B) = map(int, input().split())
	l[A].append(-B)
h = []
ans = 0
for i in range(M):
	for B in l[i + 1]:
		heapq.heappush(h, B)
	if len(h) != 0:
		ans += -heapq.heappop(h)
print(ans)


import heapq as hp
(n, m) = [int(i) for i in input().split()]
lists = [[] for i in range(m)]
for _ in range(n):
	(a, b) = [int(i) for i in input().split()]
	if a <= m:
		lists[a - 1].append(b)
h = []
ans = 0
for day in range(m):
	for v in lists[day]:
		hp.heappush(h, -v)
	if len(h) > 0:
		ans -= hp.heappop(h)
print(ans)


from heapq import heappop, heappush
(n, m) = map(int, input().split())
X = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])
hq = []
(ans, j) = (0, 0)
for i in range(1, m + 1):
	while j < n and X[j][0] <= i:
		heappush(hq, -X[j][1])
		j += 1
	if len(hq):
		ans += -heappop(hq)
print(ans)


(N, M) = list(map(int, input().split()))
AB = [tuple(map(int, input().split())) for _ in range(N)]
_d = {i: [] for i in range(M + 1)}
for (a, b) in AB:
	_d.setdefault(a, []).append(b)
import heapq
ans = 0
hp = []
for i in range(1, M + 1):
	for b in _d[i]:
		heapq.heappush(hp, -b)
	if hp:
		ans += -heapq.heappop(hp)
print(ans)


from heapq import heapify, heappop, heappush
(N, M) = map(int, input().split())
A = [[] for _ in range(M + 1)]
for i in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		A[a].append(b)
ans = 0
L = []
for day in range(1, M + 1):
	for t in A[day]:
		heappush(L, -t)
	if not L:
		continue
	temp = heappop(L)
	ans += -temp
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = [[] for i in range(10 ** 5)]
for i in range(n):
	(a, b) = map(int, input().split())
	ab[a - 1].append(b * -1)
que = []
ans = 0
for i in range(m):
	for b in ab[i]:
		heapq.heappush(que, b)
	if que:
		ans += heapq.heappop(que)
print(ans * -1)


from heapq import heappush, heappop
R = lambda : map(int, input().split())
(n, m) = R()
c = [[] for _ in range(m)]
for _ in range(n):
	(a, b) = R()
	if a <= m:
		c[a - 1].append(b)
r = 0
h = []
for l in c:
	for x in l:
		heappush(h, -x)
	if h:
		r -= heappop(h)
print(r)


import heapq
(N, M) = 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 = []
ans = 0
for i in range(M):
	for b in day[i + 1]:
		heapq.heappush(q, b)
	if len(q) > 0:
		ans += -1 * heapq.heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
jobs = [[] for i in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a > m:
		continue
	jobs[m - a].append(b)
q = []
ans = 0
for i in range(m, 0, -1):
	for job in jobs[i - 1]:
		heapq.heappush(q, -job)
	if len(q) > 0:
		ans += -heapq.heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
w = [[0] for _ in range(m + 1)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		w[a].append(b)
ans = 0
hq = []
for i in range(1, m + 1):
	for j in w[i]:
		heapq.heappush(hq, -j)
	ans += heapq.heappop(hq)
print(ans * -1)


f = lambda : map(int, input().split())
(n, m) = f()
lt = sorted((list(f()) for _ in range(n)))[::-1]
from heapq import *
(q, s) = ([], 0)
for i in range(m):
	while lt and lt[-1][0] < i + 2:
		(a, b) = lt.pop()
		heappush(q, -b)
	if q:
		s -= heappop(q)
print(s)


import heapq
(n, m) = map(int, input().split())
l = [tuple(map(int, input().split())) for _ in range(n)]
l = sorted(l, key=lambda x: x[0], reverse=True)
res = 0
q = []
for i in range(1, m + 1):
	while l and l[-1][0] <= i:
		heapq.heappush(q, -1 * l.pop()[1])
	if q:
		res += -1 * heapq.heappop(q)
print(res)


import heapq
(N, M) = map(int, input().split())
jobs = {m: [] for m in range(1, M + 1)}
for i in range(N):
	(hi, yen) = map(int, input().split())
	if hi <= M:
		jobs[hi].append(yen)
q = []
ans = 0
for i in range(1, M + 1):
	for j in jobs[i]:
		heapq.heappush(q, -j)
	if len(q) > 0:
		a = heapq.heappop(q)
		ans += -a
print(ans)


import heapq
from collections import deque
(N, M) = 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)


from heapq import *
((N, M), *t) = [map(int, s.split()) for s in open(0)]
(q, z) = ([], 0)
v = [[] for _ in [None] * 10 ** 5]
for (a, b) in t:
	v[a - 1] += (b,)
for i in v[:M]:
	for j in i:
		heappush(q, -j)
	if q:
		z += -heappop(q)
print(z)


import heapq
(n, m) = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)], reverse=True)
c = []
ans = 0
for i in range(m):
	while len(ab) != 0 and ab[-1][0] <= i + 1:
		(_, b) = ab.pop()
		heapq.heappush(c, -b)
	if c:
		ans += -heapq.heappop(c)
print(ans)


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)
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])
		i += 1
	if hq:
		ans -= heapq.heappop(hq)
print(ans)


from heapq import heappop, heappush
(n, nDays) = map(int, input().split())
a = sorted((tuple(map(int, input().split())) for _ in range(n)))
ret = 0
pq = []
pos = 0
for day in range(nDays + 1):
	while pos < n and a[pos][0] <= day:
		heappush(pq, -a[pos][1])
		pos += 1
	if pq:
		ret -= heappop(pq)
print(ret)


import heapq
(n, m) = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n)]
(ans, memo) = (0, 0)
hq = []
AB.sort(key=lambda tup: tup[0])
for day in range(m):
	for i in range(memo, n):
		if AB[i][0] <= day + 1:
			heapq.heappush(hq, -AB[i][1])
			memo += 1
		else:
			break
	if len(hq):
		ans += -heapq.heappop(hq)
print(ans)


(_, m) = map(int, input().split())
work = sorted([tuple(map(int, input().split())) for k in range(_)], key=lambda x: (-x[0], x[1]))
from heapq import heapify, heappop, heappush
now = []
ans = 0
for i in range(1, m + 1):
	while work and work[-1][0] <= i:
		(a, b) = work.pop()
		heappush(now, -b)
	if now:
		ans -= heappop(now)
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)])
i = 0
q = []
ret = 0
for d in range(1, m + 1):
	while i < len(ab) and ab[i][0] <= d:
		heapq.heappush(q, -ab[i][1])
		i += 1
	if len(q) > 0:
		ret -= heapq.heappop(q)
print(ret)


import sys
import heapq
sys.setrecursionlimit(10 ** 8)
ini = lambda : int(sys.stdin.readline())
inl = lambda : [int(x) for x in sys.stdin.readline().split()]
ins = lambda : sys.stdin.readline().rstrip()
(N, M) = inl()

class MinHeap(object):

	def __init__(self):
		self.h = []

	def push(self, x):
		heapq.heappush(self.h, x)

	def pop(self):
		return heapq.heappop(self.h)

	def __getitem__(self, i):
		return self.h[i]

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

	def __bool__(self):
		return bool(self.h)

class MaxHeap(MinHeap):

	class Negator(object):

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

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

		def __eq__(self, other):
			return self.val == other.val

		def __str__(self):
			return str(self.val)

	def push(self, x):
		heapq.heappush(self.h, self.Negator(x))

	def pop(self):
		return heapq.heappop(self.h).val

def solve():
	jobs = []
	for i in range(N):
		(a, b) = inl()
		jobs.append((a, b))
	jobs.sort()
	hp = MaxHeap()
	ans = 0
	j = 0
	for d in range(1, M + 1):
		while j < N and jobs[j][0] == d:
			(a, b) = jobs[j]
			hp.push(b)
			j += 1
		if hp:
			ans += hp.pop()
	return ans
print(solve())


import collections
import heapq
(n, m) = map(int, input().split())
que = []
for _ in range(n):
	(a, b) = 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)


import heapq
(N, M) = map(int, input().split())
W = [tuple(map(int, input().split())) for i in range(N)]
W.sort()
h = []
j = 0
ans = 0
for i in range(M + 1):
	while j < N and W[j][0] < i + 1:
		heapq.heappush(h, (-W[j][1], W[j][1]))
		j += 1
	if h:
		p = heapq.heappop(h)
		ans += p[1]
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: (-x[0], x[1]))
q = []
ans = 0
for i in range(1, m + 1):
	while ab and ab[-1][0] == i:
		(a, b) = ab.pop()
		heapq.heappush(q, -b)
	if q:
		ans -= heapq.heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
work = [[] for _ in range(m)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		work[a - 1].append(b)
ans = 0
q = []
heapq.heapify(q)
for i in range(m):
	for x in work[i]:
		heapq.heappush(q, -x)
	if q != []:
		ans += heapq.heappop(q) * -1
print(ans)


import heapq
(N, M) = map(int, input().split())
l = [tuple(map(int, input().split())) for i in range(N)]
l.sort(reverse=True)
h = []
ans = 0
for i in range(1, M + 1):
	while l and l[-1][0] == i:
		heapq.heappush(h, l.pop()[1] * -1)
	if h:
		ans -= heapq.heappop(h)
print(ans)


import heapq
(n, m) = map(int, input().split())
mm = 10 ** 5 + 5
c = [[0] for i in range(mm)]
for i in range(n):
	(a, b) = map(int, input().split())
	c[a].append(b)
ans = 0
h = []
for i in reversed(range(m)):
	d = m - i
	for j in c[d]:
		heapq.heappush(h, -j)
	ans += -heapq.heappop(h)
print(ans)


import heapq
(n, maxDays) = map(int, input().split())
a = sorted([list(map(int, input().split())) for _ in range(n)])
pq = []
idx = 0
ret = 0
for day in range(maxDays + 1):
	while idx < n and a[idx][0] <= day:
		heapq.heappush(pq, -a[idx][1])
		idx += 1
	if pq:
		ret -= heapq.heappop(pq)
print(ret)


from heapq import *
(N, M, *t) = 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)


import heapq
(N, M) = 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)


import heapq as hq
(n, m) = 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)


import heapq
(n, m) = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
(result, j) = (0, 0)
x = []
heapq.heapify(x)
for i in range(1, m + 1):
	while j < n and ab[j][0] <= i:
		heapq.heappush(x, -ab[j][1])
		j += 1
	if len(x) != 0:
		result -= heapq.heappop(x)
print(result)


from heapq import heappush, heappop
(n, m) = map(int, input().split())
w = [[] for _ in range(10 ** 5)]
for i in range(n):
	(a, b) = map(int, input().split())
	w[a - 1].append(b)
money = 0
l = []
for i in range(m):
	for item in w[i]:
		heappush(l, item * -1)
	if len(l) == 0:
		continue
	money += heappop(l) * -1
print(money)


import heapq
(N, M) = map(int, input().split())
W = [list(map(int, input().split())) for _ in range(N)]
a = [[i, 0] for i in range(1, M + 2)]
W.extend(a)
W.sort()
q = []
ans = 0
for (i, j) in W:
	if i > M + 1:
		break
	if j != 0:
		heapq.heappush(q, -j)
	else:
		heapq.heappush(q, -j)
		ans -= heapq.heappop(q)
print(ans)


from heapq import heappush, heappop
(n, m) = [int(x) for x in input().rstrip().split()]
ab = [[] for i in range(10 ** 5)]
for i in range(n):
	(a, b) = [int(x) for x in input().rstrip().split()]
	if a <= m:
		ab[m - a].append(-b)
ans = 0
l = []
for i in range(m - 1, -1, -1):
	for b in ab[i]:
		heappush(l, b)
	if l:
		ans -= heappop(l)
print(ans)


import heapq
(N, M) = map(int, input().split())
jobs = [[] for _ in range(M + 1)]
for _ in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		jobs[a].append(b)
pq = []
d = 1
res = 0
for _ in range(M - 1, -1, -1):
	for b in jobs[d]:
		heapq.heappush(pq, -b)
	if pq:
		res += -heapq.heappop(pq)
	d += 1
print(res)


from heapq import heappush, heappop
(N, M) = map(int, input().split())
D = {i + 1: [] for i in range(M)}
for _ in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		D[A].append(B)
rew = 0
pq = []
for i in range(M):
	for j in D[i + 1]:
		heappush(pq, -j)
	if pq != []:
		rew += -heappop(pq)
print(rew)


(n, m) = list(map(int, input().split()))
job_list = []
for i in range(n):
	tmp = [int(x) for x in input().split()]
	job_list.append(tmp)
job_list.sort()
import heapq
k = 0
ans = 0
salary_list = []
for i in range(1, m + 1):
	while k < n and job_list[k][0] == i:
		heapq.heappush(salary_list, job_list[k][1] * -1)
		k += 1
	if len(salary_list) > 0:
		ans += heapq.heappop(salary_list)
print(-1 * ans)


import heapq
(N, M) = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)])
option = []
ans = 0
j = 0
for i in range(1, M + 1):
	while j != N and AB[j][0] <= i:
		heapq.heappush(option, -AB[j][1])
		j += 1
	if len(option) != 0:
		ans -= heapq.heappop(option)
print(ans)


import heapq
(N, M) = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
ABs.sort(reverse=True)
works = []
reward = 0
for i in range(1, M + 1):
	while ABs and ABs[-1][0] <= i:
		heapq.heappush(works, -ABs.pop()[1])
	if works:
		reward -= heapq.heappop(works)
print(reward)


from heapq import *
(N, M) = map(int, input().split())
l = [[0] for _ in range(M + 1)]
for _ in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		l[a].append(b)
ans = 0
h = []
for i in range(1, M + 1):
	for j in l[i]:
		heappush(h, -j)
	ans += heappop(h)
ans *= -1
print(ans)


import heapq
(n, m) = map(int, input().split())
b_list = [[] for _ in range(10 ** 5 + 10)]
for i in range(n):
	(a, b) = map(int, input().split())
	b_list[a].append(-b)
candy = []
heapq.heapify(candy)
ans = 0
for i in range(1, m + 1):
	for j in b_list[i]:
		heapq.heappush(candy, j)
	if candy:
		ans += -1 * heapq.heappop(candy)
print(ans)


(n, m) = list(map(int, input().split()))
ab = [[] for _ in range(10 ** 5 + 1)]
for j in range(1, 10 ** 5 + 1):
	ab[j].append(0)
for _ in range(n):
	(a, b) = list(map(int, input().split()))
	ab[a].append(-1 * b)
temp = []
import heapq
ans = 0
for i in range(1, m + 1):
	for j in ab[i]:
		heapq.heappush(temp, j)
	x = heapq.heappop(temp)
	ans += -1 * x
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort(key=lambda x: x[0])
ans = 0
q = []
heapq.heapify(q)
e = 0
for i in range(1, m + 1):
	while e < n and ab[e][0] == i:
		heapq.heappush(q, -ab[e][1])
		e += 1
	if len(q) > 0:
		ans -= heapq.heappop(q)
print(ans)


from heapq import heappush, heappop
(n, m) = map(int, input().split())
B = [[] for _ in range(m + 1)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		B[a] += [-b]
C = []
s = 0
for i in range(m + 1):
	for b in B[i]:
		heappush(C, b)
	if C:
		s += heappop(C)
print(-s)


import sys
from collections import defaultdict
from heapq import heappop, heappush
(N, M) = list(map(int, sys.stdin.readline().split()))
A = defaultdict(list)
for line in sys.stdin:
	(a, b) = list(map(int, line.split()))
	if a <= M:
		A[M - a].append(b)
ans = 0
q = []
for (i, B) in sorted(A.items()):
	for b in B:
		heappush(q, b)
		ans += b
	while len(q) > i + 1:
		b = heappop(q)
		ans -= b
print(ans)


from heapq import *
(n, m) = map(int, input().split())
ab = []
ans = [[] for i in range(m)]
aa = 0
for i in range(n):
	(a, b) = map(int, input().split())
	a -= 1
	b *= -1
	ab.append((a, b))
ab.sort()
t = []
heapify(t)
for (a, b) in ab:
	if a < m:
		ans[a].append(b)
for i in range(m):
	for j in ans[i]:
		heappush(t, j)
	if len(t) > 0:
		tt = heappop(t) * -1
		aa += tt
print(aa)


import heapq
(N, M) = map(int, input().split())
reward = [[] for _ in range(M)]
for i in range(N):
	(A, B) = map(int, input().split())
	if A <= M:
		reward[A - 1].append(B)
q = []
ans = 0
for day in range(M):
	for r in reward[day]:
		heapq.heappush(q, -r)
	if len(q) > 0:
		ans += -heapq.heappop(q)
print(ans)


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

def main():
	MAX_A = 10 ** 5
	(N, M) = list(map(int, input().split()))
	AB = [[] for _ in range(MAX_A + 1)]
	for i in range(N):
		(A, B) = list(map(int, input().split()))
		AB[A].append(-B)
	p_queue = []
	ans = 0
	for m in range(1, M + 1):
		for b in AB[m]:
			heappush(p_queue, b)
		if p_queue:
			x = heappop(p_queue)
			x *= -1
			ans += x
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, K) = list(map(int, input().split()))
l = []
for i in range(N):
	l.append(list(map(int, input().split())))
l.sort()
import heapq
X = []
S = 0
ans = 0
heapq.heapify(X)
for i in range(1, K + 1):
	for j in range(S, N):
		if l[j][0] <= i:
			S += 1
			heapq.heappush(X, l[j][1] * -1)
		else:
			break
	try:
		tmp = heapq.heappop(X)
		ans += tmp * -1
	except IndexError:
		continue
print(ans)


(N, M) = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: (-x[1], -x[0]))
memo = [0] * (10 ** 5 + 1)
work = [float('inf')] * (10 ** 5 + 1)
noMore = float('inf')
ans = 0
for (a, b) in AB:
	if a < noMore:
		flag = 1
		for idx in range(min(M - a, work[a]), -1, -1):
			if memo[idx] == 0:
				ans += b
				memo[idx] = 1
				flag = 0
				work[a] = idx
				break
		if flag == 1:
			noMore = a
print(ans)


import heapq
(N, M) = map(int, input().split())
W = [[] for _ in range(100005)]
for i in range(N):
	(A, B) = 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)


import heapq
INT = lambda : int(input())
INTM = lambda : map(int, input().split())
STRM = lambda : map(str, input().split())
STR = lambda : str(input())
LIST = lambda : list(map(int, input().split()))
LISTS = lambda : list(map(str, input().split()))

def do():
	(n, m) = INTM()
	ptj = [[] for i in range(10 ** 5)]
	ans = 0
	for i in range(n):
		(a, b) = INTM()
		a -= 1
		b *= -1
		ptj[a].append(b)
	h = []
	for i in range(m):
		for p in ptj[i]:
			heapq.heappush(h, p)
		if len(h) != 0:
			ans += heapq.heappop(h)
	print(0 - ans)

def __starting_point():
	do()
__starting_point()


from collections import *
from heapq import *
(n, m, *t) = map(int, open(0).read().split())
d = defaultdict(list)
for (a, b) in zip(t[::2], t[1::2]):
	d[a].append(b)
q = []
for i in sorted(d.keys())[::-1]:
	for b in sorted(d[i])[::-1][:m - i + 1]:
		if len(q) <= m - i:
			heappush(q, b)
		elif q and q[0] < b:
			heappushpop(q, b)
print(sum(q))


def insertHeap(array, val):
	array.append(val)
	n = len(array) - 1
	while n != 0:
		parent = int((n - 1) / 2)
		if array[n] > array[parent]:
			tmp = array[n]
			array[n] = array[parent]
			array[parent] = tmp
			n = parent
		else:
			break
	return array

def desertHeap(array):
	if array == []:
		return None
	(array[0], array[-1]) = (array[-1], array[0])
	res = array.pop(-1)
	n = len(array)
	if n == 0 or n == 1:
		return res
	parent = 0
	while True:
		child = 2 * parent + 1
		if child >= n:
			break
		if child + 1 < n and array[child] < array[child + 1]:
			child += 1
		if array[parent] < array[child]:
			(array[child], array[parent]) = (array[parent], array[child])
			parent = child
		else:
			break
	return res
(n, m) = map(int, input().split())
li = [[] for j in range(10 ** 5 + 1)]
for _ in range(n):
	(a, b) = map(int, input().split())
	li[a].append(b)
ans = 0
jobsHeap = []
for i in range(1, m + 1):
	for val in li[i]:
		jobsHeap = insertHeap(jobsHeap, val)
	if jobsHeap != []:
		res = desertHeap(jobsHeap)
		ans += res
print(ans)


from collections import defaultdict
from heapq import heapify, heappush, heappop
(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)
L = []
heapify(L)
ans = 0
for remain in range(1, m + 1):
	for reward in d[remain]:
		heappush(L, -reward)
	if len(L):
		ans -= heappop(L)
print(ans)


import sys
input = sys.stdin.readline
import heapq
(n, m) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort()
hq = []
tmp = 0
ans = 0
for i in range(1, m + 1):
	while tmp < n and AB[tmp][0] <= i:
		heapq.heappush(hq, -AB[tmp][1])
		tmp += 1
	if hq:
		ans -= heapq.heappop(hq)
print(ans)


from heapq import heappop, heappush
(N, M) = list(map(int, input().split()))
score = 0
AB = [[] for i in range(M)]
for i in range(N):
	(a, b) = list(map(int, input().split()))
	if M - a >= 0:
		AB[M - a].append(b)
queue = []
for i in range(1, M + 1):
	for j in AB[-i]:
		heappush(queue, -j)
	if queue != []:
		score += -1 * heappop(queue)
print(score)


import sys
import queue
input_methods = ['clipboard', 'file', 'key']
using_method = 0
input_method = input_methods[using_method]
IN = lambda : map(int, input().split())
mod = 1000000007

class node:

	def __init__(self, a_val, a_parent=None):
		self.val = a_val
		self.parent = a_parent
		self.right = None
		self.left = None

def main2():
	(n, m) = IN()
	vl = []
	for n in range(n):
		(a, b) = IN()
		vl.append((a, b))
	vl.sort(key=lambda x: -x[1])
	ret = 0
	nwdl = [-1] * (m + 1)
	for (a, b) in vl:
		j = m - a
		while j >= 0:
			if nwdl[j] == -1:
				nwdl[j] = 0
				ret += b
				break
			j -= 1
	print(ret)

def main():
	(n, m) = IN()
	st_day = [[] for _ in range(m + 1)]
	for n in range(n):
		(duration, pay) = IN()
		if duration > m:
			continue
		st_day[m - duration].append(pay)
	pq = queue.PriorityQueue()
	ret = 0
	for di in range(m + 1):
		day = m - di
		for pay in st_day[day]:
			pq.put(-pay)
		if not pq.empty():
			v = pq.get()
			ret -= v
	print(ret)
isTest = False

def pa(v):
	if isTest:
		print(v)

def input_clipboard():
	import clipboard
	input_text = clipboard.get()
	input_l = input_text.splitlines()
	for l in input_l:
		yield l

def __starting_point():
	if sys.platform == 'ios':
		if input_method == input_methods[0]:
			ic = input_clipboard()
			input = lambda : ic.__next__()
		elif input_method == input_methods[1]:
			sys.stdin = open('inputFile.txt')
		else:
			pass
		isTest = True
	else:
		pass
	ret = main()
	if ret is not None:
		print(ret)
__starting_point()


import heapq
(n, m) = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort()
i = 0
works = []
reward = 0
for d in range(m):
	while i < n and jobs[i][0] <= d + 1:
		heapq.heappush(works, -jobs[i][1])
		i += 1
	if works:
		reward -= heapq.heappop(works)
print(reward)


import heapq
(n, m) = map(int, input().split())
A = [[0] * 2 for i in range(n)]
for i in range(n):
	A[i] = list(map(int, input().split()))
A = sorted(A)
ans = 0
s = []
j = 0
for i in range(m + 1):
	while j < n:
		if A[j][0] > i:
			break
		heapq.heappush(s, A[j][1] * -1)
		j = j + 1
	if len(s) == 0:
		continue
	ans = ans - heapq.heappop(s)
print(ans)


import heapq
(N, M) = map(int, input().split())
d = {}
for i in range(M):
	d[i] = [0]
for i in range(N):
	(a, b) = map(int, input().split())
	if 0 <= M - a:
		d[M - a].append(b)
hq = []
ans = 0
for i in range(M)[::-1]:
	[heapq.heappush(hq, -j) for j in d[i]]
	ans -= heapq.heappop(hq)
print(ans)


from heapq import heappush, heappop
(n, m) = map(int, input().split())
li = [[] for i in range(10 ** 5 + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	li[a].append(b)
li2 = []
ans = 0
for i in range(1, m + 1):
	for x in li[i]:
		heappush(li2, -x)
	if li2:
		ans += heappop(li2)
print(-ans)


from heapq import *
(n, m) = map(int, input().split())
a = [[int(i) for i in input().split()] for _ in range(n)]
a.sort(key=lambda x: x[0])
ans = 0
PQ = []
heapify(PQ)
idx = 0
for i in range(1, m + 1):
	while idx < n and a[idx][0] <= i:
		heappush(PQ, -a[idx][1])
		idx += 1
	if PQ:
		ans -= heappop(PQ)
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = [[0] * 2 for i in range(n)]
for i in range(n):
	(a, b) = map(int, input().split())
	(ab[i][0], ab[i][1]) = (a, -1 * b)
heapq.heapify(ab)
b = [0]
heapq.heapify(b)
ans = 0
for i in range(1, m + 1):
	while ab:
		tmp = heapq.heappop(ab)
		if tmp[0] <= i:
			heapq.heappush(b, tmp[1])
		else:
			heapq.heappush(ab, tmp)
			break
	if b:
		ans -= heapq.heappop(b)
print(ans)


from heapq import heappop, heapify, heappush
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)
h = []
ans = 0
for i in range(1, M + 1):
	if len(d[i]) != 0:
		for x in d[i]:
			heappush(h, -x)
	if len(h) != 0:
		ans -= heappop(h)
print(ans)


from heapq import heappop, heappush
(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])
hq = []
ans = 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 len(hq):
		ans += -heappop(hq)
print(ans)


import heapq
(n, m) = map(int, input().split())
al = [[] for i in range(10 ** 5)]
ans = 0
h = []
for i in range(n):
	(a, b) = map(int, input().split())
	al[a - 1] += [-b]
for i in range(m):
	for j in al[i]:
		heapq.heappush(h, j)
	if h:
		ans -= heapq.heappop(h)
print(ans)


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


import sys
f = lambda : map(int, sys.stdin.readline().split())
(n, m) = f()
lt = sorted((tuple(map(int, input().split())) for _ in range(n)))[::-1]
from heapq import *
q = []
s = t = 0
while t < m:
	t += 1
	while lt and lt[-1][0] == t:
		(a, b) = lt.pop()
		heappush(q, -b)
	if q:
		s -= heappop(q)
print(s)


from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
	(N, M) = list(map(int, input().split()))
	AB = [[] for _ in range(10 ** 5)]
	for i in range(N):
		(a, b) = list(map(int, input().split()))
		AB[a - 1].append(b)
	q = []
	ans = 0
	i = 0
	while i < M:
		for ab in AB[i]:
			heappush(q, -ab)
		if q:
			ans -= heappop(q)
		i += 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import heapq
(n, m) = map(int, input().split())
a = sorted([list(map(int, input().split())) for _ in range(n)])
(r, j, x) = (0, 0, [])
heapq.heapify(x)
for i in range(m):
	while j < n and a[j][0] <= i + 1:
		heapq.heappush(x, -a[j][1])
		j += 1
	if x:
		r -= heapq.heappop(x)
print(r)


import heapq
(N, M) = map(int, input().split())
ab = [[] for _ in range(M)]
for i in range(N):
	(b, a) = map(int, input().split())
	if b <= M:
		ab[b - 1].append(a)
sal = 0
poss = []
for i in range(1, M + 1):
	for j in ab[i - 1]:
		heapq.heappush(poss, -j)
	if len(poss) > 0:
		sal += -heapq.heappop(poss)
print(sal)


from heapq import heappop, heappush
(N, M) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
X.sort(reverse=True)
ans = 0
pq = []
for n in range(M + 1):
	while X and X[-1][0] <= n:
		(a, b) = X.pop()
		heappush(pq, -b)
	if pq:
		ans -= heappop(pq)
print(ans)


from heapq import heappop, heappush, heapify
(N, M) = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(reverse=True)
que = []
ans = 0
for d in range(1, M + 1):
	while AB and AB[-1][0] <= d:
		heappush(que, -AB.pop()[1])
	if que:
		ans += heappop(que)
print(-ans)


import heapq
(N, M) = map(int, input().split())
ab = dict()
for _ in range(N):
	(A, B) = map(int, input().split())
	ab.setdefault(A, []).append(B)
q = []
ans = 0
for i in range(1, M + 1):
	for v in ab.get(i, []):
		heapq.heappush(q, v * -1)
	if q:
		ans += heapq.heappop(q) * -1
print(ans)


import heapq
(N, M) = list(map(int, input().split()))
AB = []
can = [[] for i in range(M + 1)]
for i in range(N):
	(a, b) = list(map(int, input().split()))
	AB.append([a, b])
	if M - a >= 0:
		can[M - a].append(-b)
que = []
ans = 0
for i in range(M, -1, -1):
	now_list = can[i]
	for j in range(len(now_list)):
		heapq.heappush(que, now_list[j])
	if que:
		ans += heapq.heappop(que)
print(-ans)


import heapq
(n, m) = list(map(int, input().split()))
tmp = [[] for _ in range(m)]
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if a <= m:
		tmp[a - 1].append(-b)
ans = 0
heap = []
for i in range(m):
	for b in tmp[i]:
		heapq.heappush(heap, b)
	if len(heap) > 0:
		ans -= heapq.heappop(heap)
print(ans)


import heapq
(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
AB = sorted(AB, key=lambda x: x[0])
h = [0] * M
heapq.heapify(h)
out = 0
j = 0
for i in range(1, M + 1):
	while j < N and AB[j][0] <= i:
		heapq.heappush(h, -1 * AB[j][1])
		j += 1
	out += -1 * heapq.heappop(h)
print(out)


from heapq import heappop, heappush, heapify
from collections import deque
(N, M) = map(int, input().split())
(A, B, C) = ([0] * N, [0] * N, [0] * N)
for i in range(N):
	(A[i], B[i]) = map(int, input().split())
	C[i] = [A[i], B[i]]
C.sort()
C = deque(C)
a = []
heapify(a)
ans = 0
for i in range(M, -1, -1):
	while C:
		if C[0][0] <= M - i:
			heappush(a, -1 * C[0][1])
			C.popleft()
		else:
			break
	if len(a) > 0:
		p = heappop(a)
		ans += -1 * p
print(ans)


from heapq import heappop, heappush
(n, m, *AB) = map(int, open(0).read().split())
B = [[] for _ in range(m)]
for (a, b) in zip(AB[::2], AB[1::2]):
	if a <= m:
		B[a - 1].append(b)
ans = 0
heap = []
for b in B:
	for x in b:
		heappush(heap, -x)
	if heap:
		ans -= heappop(heap)
print(ans)


import sys
icase = 0
if icase == 0:
	(n, m) = list(map(int, input().split()))
	ab = [[] for i in range(m)]
	for i in range(n):
		(ai, bi) = list(map(int, input().split()))
		if m - ai >= 0:
			ab[m - ai].append(-bi)
elif icase == 1:
	(n, m) = (3, 4)
	ab = [[-3, -1], [], [-2], []]
elif icase == 2:
	(n, m) = (5, 3)
	ab = [[], [-1, -3], [-2, -3, -4]]
elif icase == 3:
	(n, m) = (1, 1)
	ab = [[]]
from heapq import heappop, heappush
if len(ab) == 0:
	print(0)
	return
n = len(ab)
h = []
asum = 0
for i in range(m - 1, -1, -1):
	if len(ab[i]) > 0:
		for abi in ab[i]:
			heappush(h, abi)
	if len(h) > 0:
		asum += heappop(h)
print(-asum)


from heapq import *
(n, m) = list(map(int, input().split()))
job = [list(map(int, input().split())) for _ in range(n)]
job.sort(reverse=True)
q = []
ans = 0
for i in range(m - 1, -1, -1):
	while job and job[-1][0] + i <= m:
		(a, b) = job.pop()
		heappush(q, -b)
	if q:
		ans -= heappop(q)
print(ans)


import heapq
(N, M) = map(int, input().split())
AB = [[] for _ in range(10 ** 5 + 10)]
for _ in range(N):
	(ta, tb) = map(int, input().split())
	AB[ta].append(-tb)
ans = 0
q = []
for i in range(1, M + 1):
	while AB[i]:
		heapq.heappush(q, AB[i].pop())
	if q:
		ans += heapq.heappop(q)
print(-ans)


import heapq
(n, m) = map(int, input().split())
lb = [[] for _ in range(10 ** 5 + 5)]
for _ in range(n):
	(a, b) = map(int, input().split())
	lb[a].append(b)
res = 0
q = []
heapq.heapify(q)
for i in range(1, m + 1):
	for b in lb[i]:
		heapq.heappush(q, b * -1)
	if q:
		res += heapq.heappop(q) * -1
print(res)


(N, M) = 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)


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)


import heapq
import sys
(N, M) = map(int, input().split())
B = [[] for _ in range(M + 1)]
for _ in range(N):
	(a, b) = map(int, sys.stdin.readline().split())
	if a <= M:
		B[a].append(b)
ans = 0
q = []
for i in range(1, M + 1):
	for b in B[i]:
		heapq.heappush(q, -b)
	if q:
		ans += -heapq.heappop(q)
print(ans)


from math import factorial as fact
import math
import sys
from itertools import product
import numpy as np
from collections import Counter
import datetime
from collections import deque
from bisect import bisect_left, bisect_right
import heapq

def input1():
	return int(input())

def input2():
	return list(map(int, input().split()))

def input_array():
	return list(map(int, input().split()))
(n, m) = input2()
AB = [[] for _ in range(m)]
for i in range(n):
	(a, b) = input2()
	if a - 1 < m:
		AB[a - 1].append(-b)
ans = 0
heap = []
for i in range(m):
	for b in AB[i]:
		heapq.heappush(heap, b)
	if len(heap) > 0:
		MAX = heapq.heappop(heap)
		ans += -MAX
print(ans)


from heapq import *
((n, m), *t) = [map(int, s.split()) for s in open(0)]
q = [0] * 7 ** 6
v = [[] for _ in q]
z = 0
for (a, b) in t:
	v[a - 1] += [b]
for i in v[:m]:
	for j in i:
		heappush(q, -j)
	z -= heappop(q)
print(z)


(n, m) = map(int, input().split())
ab = []
for _ in range(n):
	(tmpa, tmpb) = map(int, input().split())
	ab.append((tmpa, tmpb))
ab.sort(reverse=True)
import heapq
h = []
ans = 0
for i in range(1, m + 1):
	while ab and ab[-1][0] == i:
		heapq.heappush(h, ab.pop()[1] * -1)
	if h:
		ans -= heapq.heappop(h)
print(ans)


import sys
import heapq

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

def main():
	(n, m) = map(int, input().split())
	li = []
	for _ in range(n):
		(b, a) = map(int, input().split())
		if m - b >= 0:
			li.append([a, m - b])
	li.sort(key=lambda x: x[1])
	h = []
	for i in li:
		if len(h) >= i[1] + 1:
			heapq.heappushpop(h, i[0])
		else:
			heapq.heappush(h, i[0])
	print(sum(h))

def __starting_point():
	main()
__starting_point()


import heapq
(N, M) = map(int, input().split())
jobs = [[] for _ in range(M)]
for _ in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		jobs[a - 1].append(b)
h = []
rsl = 0
for i in range(M):
	for j in jobs[i]:
		heapq.heappush(h, -j)
	if len(h) > 0:
		rsl += -heapq.heappop(h)
print(rsl)


import heapq
(n, m) = map(int, input().split())
que = []
for _ in range(n):
	(a, b) = 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))


(n, m) = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
h = []
ans = 0
import heapq
j = 0
for i in range(1, m + 1):
	while j < n and ab[j][0] == i:
		heapq.heappush(h, -1 * ab[j][1])
		j += 1
	if h:
		ans += -1 * heapq.heappop(h)
print(ans)


import heapq
ans = 0
(n, m) = map(int, input().split())
memo = [[] for _ in range(10 ** 5)]
for _ in range(n):
	(a, b) = map(int, input().split())
	memo[a - 1].append(-b)
pq = []
for i in range(m):
	for j in memo[i]:
		heapq.heappush(pq, j)
	if len(pq):
		ans += -heapq.heappop(pq)
print(ans)


import heapq
(n, m) = map(int, input().split())
lis = [[] for i in range(m)]
for i in range(n):
	(a, b) = 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)


from heapq import *
(n, m) = map(int, input().split())
a = sorted([list(map(int, input().split())) for _ in range(n)])
a.append([10 ** 9])
(r, j, x) = (0, 0, [])
heapify(x)
for i in range(m):
	while a[j][0] <= i + 1:
		heappush(x, -a[j][1])
		j += 1
	if x:
		r -= heappop(x)
print(r)


from heapq import heappush, heappop
from collections import defaultdict as dd
(N, M) = map(int, input().split())
arrs = dd(list)
for _ in range(N):
	(a, b) = map(int, input().split())
	arrs[a].append(-b)
q = []
ans = 0
for a in range(1, M + 1):
	for b in arrs[a]:
		heappush(q, b)
	if len(q):
		ans -= heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
x = []
y = []
for i in range(n):
	(day, salary) = map(int, input().split())
	heapq.heappush(x, (-day, -salary))
while x:
	(d, s) = heapq.heappop(x)
	r = m - len(y)
	if r >= -d:
		heapq.heappush(y, -s)
	else:
		heapq.heappush(y, -s)
		heapq.heappop(y)
print(sum(y))


from operator import itemgetter
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].append(-b)
Q = []
heapq.heapify(Q)
ans = 0
for i in range(1, m + 1):
	for j in d[i]:
		heapq.heappush(Q, j)
	if Q:
		ans += -heapq.heappop(Q)
print(ans)


import heapq
(n, m) = map(int, input().split())
w = [[] for i in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	a -= 1
	if a >= m:
		continue
	w[a].append(-b)
h = []
c = 0
for i in range(m):
	for j in w[i]:
		heapq.heappush(h, j)
	try:
		c += heapq.heappop(h)
	except:
		pass
print(-c)


from heapq import heappop, heappush
(n, m) = map(int, input().split())
L = [[] for _ in range(m)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		L[m - a].append(b)
s = 0
h = []
for i in range(m - 1, -1, -1):
	for l in L[i][::-1]:
		heappush(h, -l)
	if len(h) > 0:
		s -= heappop(h)
print(s)


from heapq import *
(n, m) = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m + 1):
	while jobs and jobs[-1][0] <= i:
		heappush(q, -jobs.pop(-1)[1])
	if q:
		ans -= heappop(q)
print(ans)


from heapq import heapify, heappop, heappush
(n, m) = map(int, input().split())
l = [[] for i in range(10 ** 5)]
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	l[a - 1].append(-b)
for i in range(10 ** 5):
	heapify(l[i])
p = []
heapify(p)
for i in range(m):
	if len(l[i]) != 0:
		heappush(p, [heappop(l[i]), i])
	if len(p) != 0:
		x = heappop(p)
		ans += -x[0]
		if len(l[x[1]]) != 0:
			heappush(p, [heappop(l[x[1]]), x[1]])
print(ans)


from heapq import *
(N, M) = map(int, input().split())
P = 0
X = [[] for i in range(10 ** 5 + 1)]
(A, B) = (0, 0)
for i in range(N):
	(A, B) = map(int, input().split())
	if A > M:
		continue
	X[M - A].append(B)
Q = []
heapify(Q)
for i in range(M - 1, -1, -1):
	for j in range(len(X[i])):
		heappush(Q, -X[i][j])
	if len(Q):
		P -= heappop(Q)
print(P)


from heapq import heappop, heapify, heappush

def main():
	(n, m) = list(map(int, input().split()))
	work = [[] for _ in range(10 ** 5 + 1)]
	for _ in range(n):
		(a, b) = list(map(int, input().split()))
		work[a].append(b)
	ans = 0
	q = []
	heapify(q)
	for i in range(1, m + 1):
		for w in work[i]:
			heappush(q, -w)
		if len(q) == 0:
			continue
		ans += heappop(q)
	ans *= -1
	print(ans)

def __starting_point():
	main()
__starting_point()


import heapq
(n, m) = map(int, input().split())
lis = []
for i in range(n):
	(a, b) = map(int, input().split())
	heapq.heappush(lis, [-a, -b])
s = []
while lis:
	(x, y) = heapq.heappop(lis)
	r = m - len(s)
	if -x > r:
		heapq.heappush(s, -y)
		heapq.heappop(s)
	else:
		heapq.heappush(s, -y)
print(sum(s))


import sys
from heapq import heappop, heappush
(N, M) = list(map(int, sys.stdin.readline().split()))
A = [[] for _ in [0] * M]
for line in sys.stdin:
	(a, b) = list(map(int, line.split()))
	if a > M:
		continue
	A[M - a].append(b)
ans = 0
q = []
for (i, B) in enumerate(A):
	for b in B:
		heappush(q, b)
		ans += b
	while len(q) > i + 1:
		b = heappop(q)
		ans -= b
print(ans)


import heapq as hp

def o():
	return map(int, input().split())
(n, m) = o()
c = [[0] for i in range(m)]
for i in range(n):
	(a, b) = o()
	if a <= m:
		c[m - a] += [-b]
d = []
ans = 0
for i in range(m - 1, -1, -1):
	for j in c[i]:
		hp.heappush(d, j)
	ans -= hp.heappop(d)
print(ans)


import heapq
(n, m) = map(int, input().split())
X = [[] for _ in range(10 ** 5 + 5)]
for i in range(n):
	(a, b) = map(int, input().split())
	X[a - 1].append(-b)
ans = 0
p = [0] * m
heapq.heapify(p)
for i in range(m):
	for j in range(len(X[i])):
		heapq.heappush(p, X[i][j])
	a = heapq.heappop(p)
	ans += a * -1
print(ans)


import heapq
(n, m) = map(int, input().split())
d = [[] for _ in range(m)]
for _ in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		d[a - 1].append(-b)
t = []
heapq.heapify(t)
ans = 0
for i in range(m):
	for dd in d[i]:
		heapq.heappush(t, dd)
	if t:
		ans += heapq.heappop(t) * -1
print(ans)


from heapq import *
from collections import defaultdict
(N, M) = map(int, input().split())
data = defaultdict(list)
for _ in range(N):
	(A, B) = map(int, input().split())
	data[A].append(-B)
res = 0
temp = []
for i in range(1, M + 1):
	for a in data[i]:
		heappush(temp, a)
	if temp:
		res -= heappop(temp)
print(res)


import heapq
from typing import List

def main():
	(n, k) = list(map(int, input().split()))
	v = []
	for _ in range(n):
		(a, b) = list(map(int, input().split()))
		v.append((a, b))
	print(sv(v, k))

def sv(v: List[List[int]], k: int) -> int:
	vv = {}
	for (a, b) in v:
		if str(a) in vv:
			vv[str(a)].append(b)
		else:
			vv[str(a)] = []
			vv[str(a)].append(b)
	hp = []
	result = 0
	bk = 1
	while k >= bk:
		if str(bk) in vv:
			for b in vv[str(bk)]:
				heapq.heappush(hp, -b)
		if len(hp) > 0:
			result += -heapq.heappop(hp)
		bk += 1
	return result

def __starting_point():
	main()
__starting_point()


import heapq
(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
ans = 0
q = []
heapq.heapify(q)
idx = 0
for i in range(1, M + 1):
	while idx < N and AB[idx][0] <= i:
		heapq.heappush(q, -AB[idx][1])
		idx += 1
	if q:
		p = -heapq.heappop(q)
		ans += p
print(ans)


from heapq import heappush, heappop, heapify
(n, m) = map(int, input().split())
jobs = [list(map(int, input().split())) for _ in range(n)]
jobs.sort()
cur = 0
ans = 0
q = []
for i in range(1, m + 1):
	while cur < n and jobs[cur][0] <= i:
		heappush(q, -jobs[cur][1])
		cur += 1
	if q:
		ans += -heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)], reverse=True)
que = []
ans = 0
for i in range(1, m + 1):
	while ab and ab[-1][0] <= i:
		t = ab.pop()
		heapq.heappush(que, -t[1])
	if que:
		ans -= heapq.heappop(que)
print(ans)


import heapq
(N, M) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
H = []
iab = 0
answer = 0
for i in range(M + 1):
	while iab <= N - 1 and AB[iab][0] <= i:
		heapq.heappush(H, -AB[iab][1])
		iab += 1
	if len(H):
		answer += heapq.heappop(H)
print(-answer)


import heapq
(n, m) = map(int, input().split())
t = [[] for _ in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a > m:
		continue
	t[a].append(b)
ans = 0
h = []
for l in t:
	for i in l:
		heapq.heappush(h, -i)
	if h:
		ans -= heapq.heappop(h)
print(ans)


import heapq
(n, m) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
l.sort()
l.append([0, 0])
ans = 0
k = []
j = 0
for i in range(1, m + 1):
	while j <= n and l[j][0] <= i:
		heapq.heappush(k, -1 * l[j][1])
		j += 1
	if len(k) != 0:
		ans += -1 * k[0]
		heapq.heappop(k)
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = [[0] * 2 for _ in range(n)]
for i in range(n):
	ab[i] = list(map(int, input().split()))
ab.sort()
hq = []
tmp = 0
ans = 0
for i in range(1, m + 1):
	while tmp < n and ab[tmp][0] == i:
		heapq.heappush(hq, -ab[tmp][1])
		tmp += 1
	if hq:
		ans -= heapq.heappop(hq)
print(ans)


import heapq
(N, M) = map(int, input().split())
x = {}
for i in range(M + 1):
	x[i] = []
for i in range(N):
	(a, b) = map(int, input().split())
	if a > M:
		continue
	x[a].append(-b)
y = []
an = 0
for i in range(M + 1):
	for n in x[i]:
		heapq.heappush(y, n)
	try:
		an -= heapq.heappop(y)
	except:
		pass
print(an)


import heapq
(n, m) = map(int, input().split())
jobs = [[] for _ in range(10 ** 5 + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	jobs[a].append(b)
heap = []
ans = 0
for i in range(m + 1):
	if jobs[i]:
		for x in jobs[i]:
			heapq.heappush(heap, -x)
	if heap:
		ans += -heapq.heappop(heap)
print(ans)


(n, m) = 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)


import heapq
(n, m) = map(int, input().split())
feeindays = [[] for _ in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		feeindays[a - 1] += [b]
q = []
heapq.heapify(q)
pay = 0
for l in feeindays:
	for c in l:
		heapq.heappush(q, -c)
	if len(q) != 0:
		pay += -1 * heapq.heappop(q)
print(pay)


import heapq
(a, b) = 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)


from heapq import heappop, heappush
(n, m) = map(int, input().split())
d = []
for _ in range(n):
	(a, b) = map(int, input().split())
	d.append((m - a, b))
d.sort()
ans = 0
q = []
for i in range(m - 1, -1, -1):
	while d and d[-1][0] == i:
		heappush(q, -d.pop()[1])
	if q:
		ans -= heappop(q)
print(ans)


import heapq
(n, m) = map(int, input().split())
list = [[] for i in range(m)]
h = []
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		list[a - 1].append(-b)
ans = 0
for i in range(m):
	for j in list[i]:
		heapq.heappush(h, j)
	if h:
		ans += -heapq.heappop(h)
	else:
		continue
print(ans)


import heapq
(n, m) = map(int, input().split())
ab = [[] * (m + 1) for i in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if m - a < 0:
		continue
	ab[a].append(b)
q = []
ans = 0
heapq.heapify(q)
for i in range(1, m + 1):
	for j in ab[i]:
		heapq.heappush(q, j * -1)
	if q:
		b = heapq.heappop(q)
		ans += -1 * b
print(ans)


import heapq
(N, M) = map(int, input().split())
A = [[] for _ in range(M)]
Q = []
for _ in range(N):
	(day, money) = map(int, input().split())
	if day <= M:
		A[day - 1].append(money)
ans = 0
for i in range(0, M):
	for money in A[i]:
		heapq.heappush(Q, -money)
	if Q:
		ans += heapq.heappop(Q)
print(-ans)


import sys
from itertools import accumulate
input = sys.stdin.readline

class SegmentTree:

	def __init__(self, N, X, func, init_v):
		N_ = 1
		while N_ < N:
			N_ *= 2
		self.N = N_
		self.func = func
		self.init_v = init_v
		self.__build(X)

	def __build(self, X):
		self.node = [[self.init_v, -1] for _ in range(2 * self.N)]
		for (i, x) in enumerate(X, self.N):
			self.node[i] = [x, i - self.N + 1]
		for i in range(self.N - 1, 0, -1):
			self.node[i] = self.func(self.node[i << 1], self.node[i << 1 | 1])

	def update(self, i, x):
		i += self.N - 1
		self.node[i][0] = x
		while i > 1:
			i >>= 1
			self.node[i] = self.func(self.node[i << 1], self.node[i << 1 | 1])

	def query(self, l, r):
		dst_l = [self.init_v, -1]
		dst_r = [self.init_v, -1]
		l += self.N - 1
		r += self.N - 1
		while l < r:
			if l & 1:
				dst_l = self.func(dst_l, self.node[l])
				l += 1
			if r & 1:
				r -= 1
				dst_r = self.func(self.node[r], dst_r)
			l >>= 1
			r >>= 1
		return self.func(dst_l, dst_r)

def func(a, b):
	return a if a[0] > b[0] else b

def main():
	(N, M) = list(map(int, input().split()))
	AB = [None] * N
	for i in range(N):
		AB[i] = tuple(map(int, input().split()))
	AB.sort(key=lambda x: x[0])
	B = [0] * N
	C = [0] * (1 + 10 ** 5)
	for (i, (a, b)) in enumerate(AB):
		B[i] = b
		C[a] += 1
	C = list(accumulate(C))
	st = SegmentTree(N, B, func, -1)
	ans = 0
	l = 1
	for r in C[1:M + 1]:
		if r == 0:
			continue
		(x, i) = st.query(l, r + 1)
		if x == -1:
			continue
		ans += x
		st.update(i, -1)
	print(ans)

def __starting_point():
	main()
__starting_point()


import heapq
(n, m) = map(int, input().split())
a = [[] for _ in range(m)]
d = []
heapq.heapify(d)
for i in range(n):
	(x, y) = map(int, input().split())
	if x <= m:
		a[x - 1].append(-y)
ans = 0
for j in range(1, m + 1):
	for h in a[j - 1]:
		heapq.heappush(d, h)
	if len(d) > 0:
		c = heapq.heappop(d)
		ans += c
print(ans * -1)


from heapq import heappush, heappop

def readinput():
	(n, m) = list(map(int, input().split()))
	a = []
	b = []
	for i in range(n):
		(ai, bi) = list(map(int, input().split()))
		a.append(ai)
		b.append(bi)
	return (n, m, a, b)

def main(n, m, a, b):
	jobList = []
	for i in range(10 ** 5 + 1):
		jobList.append([])
	for i in range(n):
		jobList[a[i]].append(b[i])
	goodjobs = []
	earn = 0
	for d in range(1, m + 1):
		for job in jobList[d]:
			heappush(goodjobs, -job)
		if len(goodjobs) > 0:
			earn += heappop(goodjobs)
	return -earn

def __starting_point():
	(n, m, a, b) = readinput()
	ans = main(n, m, a, b)
	print(ans)
__starting_point()


import heapq
job = list()
salary = list()
(n, m) = map(int, input().split())
for i in range(n):
	(a, b) = 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))


import heapq
(N, M) = list(map(int, input().split()))
rinsetuList = [[] for _ in range(100001)]
for _ in range(N):
	(A, B) = list(map(int, input().split()))
	rinsetuList[A].append(B)
work_list = []
hpq = heapq.heapify(work_list)
ans = 0
for rinsetu in rinsetuList[:M + 1]:
	for money in rinsetu:
		heapq.heappush(work_list, money * -1)
	if len(work_list) != 0:
		ans += heapq.heappop(work_list) * -1
print(ans)


from heapq import heappush, heappop
import sys
input = sys.stdin.readline
(N, M) = list(map(int, input().split()))
X = sorted([list(map(int, input().split())) for n in range(N)], key=lambda x: x[0])
hq = []
(ans, j) = (0, 0)
for i in range(1, M + 1):
	while j < N and X[j][0] <= i:
		heappush(hq, -X[j][1])
		j += 1
	if len(hq):
		ans += -heappop(hq)
print(ans)


(n, m) = map(int, input().split())
ab = [0] * n
V = [[] for i in range(m + 1)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		V[a].append(-b)
import heapq
q = []
ans = 0
heapq.heapify(q)
for i in range(1, m + 1):
	for v in V[i]:
		heapq.heappush(q, v)
	if len(q) == 0:
		pass
	else:
		ans -= heapq.heappop(q)
print(ans)


from heapq import heappush, heappop
(N, M) = map(int, input().split())
P = [[] for i in range(M + 1)]
for i in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		P[a].append(b)
que = []
ans = 0
for i in range(M + 1):
	for a in P[i]:
		heappush(que, -a)
	if que:
		ans += -heappop(que)
print(ans)


import heapq
(n, m) = map(int, input().split())
kouho = [[] for i in range(m)]
for i in range(n):
	(a, b) = map(int, input().split())
	if a <= m:
		kouho[a - 1].append(-b)
ans = 0
p = []
heapq.heapify(p)
for i in range(m):
	for j in kouho[i]:
		heapq.heappush(p, j)
	if p:
		ans -= heapq.heappop(p)
print(ans)


import heapq
(n, m) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort()
queue = []
(day, i, ans) = (0, 0, 0)
while day <= m:
	while i < n and AB[i][0] <= day:
		heapq.heappush(queue, -AB[i][1])
		i += 1
	if len(queue):
		ans += -heapq.heappop(queue)
	day += 1
print(ans)


from heapq import heapify, heappop, heappush
from collections import deque
(N, M) = list(map(int, input().split()))
A = [0] * N
B = [0] * N
C = [0] * N
for i in range(N):
	(A[i], B[i]) = list(map(int, input().split()))
	C[i] = [A[i], B[i]]
C.sort()
D = deque(C)
reward = 0
h = []
heapify(h)
for i in range(M - 1, -1, -1):
	while D:
		e = D.popleft()
		if e[0] == M - i:
			heappush(h, -1 * e[1])
		else:
			D.appendleft(e)
			break
	if h:
		reward += -1 * heappop(h)
print(reward)


(N, M) = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for i in range(N)]
from heapq import *
h = []
AB = sorted(AB)
result = 0
p = 0
for i in range(1, M + 1):
	while p < len(AB) and AB[p][0] <= i:
		heappush(h, -AB[p][1])
		p += 1
	if h:
		result -= heappop(h)
print(result)


from collections import defaultdict
import heapq
(n, m) = map(int, input().split())
dic = defaultdict(list)
for i in range(n):
	(a, b) = map(int, input().split())
	dic[a] += [-b]
li = []
ans = 0
for i in range(1, m + 1):
	if dic[i]:
		for j in dic[i]:
			heapq.heappush(li, j)
	if li:
		ans += heapq.heappop(li)
print(-ans)


import heapq
(n, m) = map(int, input().split())
pay = sorted([[-int(i) for i in input().split()] for _ in range(n)])
res = 0
pq = []
for i in range(1, m + 1):
	while pay and -pay[-1][0] <= i:
		(a, b) = pay.pop()
		heapq.heappush(pq, b)
	if pq:
		res -= heapq.heappop(pq)
print(res)


import heapq
(N, M) = map(int, input().split())
AB = [[] for _ in range(M + 1)]
for i in range(N):
	(A, B) = map(int, input().split())
	if M >= A:
		AB[A].append(B)
hp = []
ans = 0
for B in AB:
	for b in B:
		heapq.heappush(hp, -1 * b)
	if hp:
		ans += heapq.heappop(hp) * -1
print(ans)


import heapq
(N, M) = map(int, input().split())
AB = [[] for _ in range(M)]
que = []
for _ in range(N):
	(a, b) = map(int, input().split())
	if a <= M:
		AB[a - 1].append(b)
ans = 0
for i in range(M):
	for b in AB[i]:
		heapq.heappush(que, -b)
	if len(que) > 0:
		ans += -heapq.heappop(que)
print(ans)


import heapq
(N, M) = map(int, input().split())
lis = [[] for i in range(10 ** 5 + 10)]
for i in range(N):
	(A, B) = map(int, input().split())
	lis[A - 1].append(-B)
ans = 0
d = []
heapq.heapify(d)
for i in range(M):
	for j in lis[i]:
		heapq.heappush(d, j)
	if len(d) > 0:
		ans += heapq.heappop(d) * -1
print(ans)