CodeEff/ECCO huggingface dataset:
# Original solution
import bisect
from collections import deque
 
N, M = list(map(int,input().split()))
 
work = [list(map(int,input().split())) for _ in range(N)]
 
wq = deque(sorted(work))
 
paytree = []
 
ans = 0
 
for day in range(M + 1):
    while len(wq) > 0 and wq[0][0] <= day:
        bisect.insort_left(paytree,wq.popleft()[1])
    if len(paytree) > 0:
        ans += paytree.pop(len(paytree) - 1)
 
print(ans)

# Optimized solution
import heapq
from collections import deque

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

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

wq = deque(sorted(work))

paytree = []

ans = 0

for day in range(M + 1):
    while len(wq) > 0 and wq[0][0] <= day:
        heapq.heappush(paytree, wq.popleft()[1] * (-1))
    if len(paytree) > 0:
        ans -= heapq.heappop(paytree)

print(ans)
# Original solution
N, M = [int(x) for x in input().split()]

AB = [0] * N
for i in range(N):
    A, B = [int(x) for x in input().split()]
    AB[i] = (A, B)

# 期間が短い報酬が高い
def key(ab):
    a, b = ab
    return -b, a
AB = sorted(((a, b) for a, b in AB if a <=M), key=key)
#print(BA)

def calc(jobs, M):
    m = 0
    for d, (a, b) in enumerate(jobs):
        if d + a <= M:
            m += b
    return m

def check(N, M, AB):
    from itertools import permutations
    return max(calc(jobs, M) for jobs in permutations(AB))

def solve(N, M, AB):
    pass

def solve(N, M, AB):
    # a = 1 -> M - a
    d = M
    i = 0
    score = 0
    scheduled = set()
    for d in range(0, M+1)[::-1]:
        for i, (a, b) in enumerate(AB):
            if i in scheduled:
                continue
            #print("d=%d"%d,(a,b),d+a,end="")
            if d + a <= M:
                score += b
                #print(" score=%d"%score)
                d -= 1
                scheduled.add(i)
                break
    return score

#print("check", check(N, M, AB))
print((solve(N, M, AB)))


# Optimized solution
import heapq

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

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

AB = sorted(AB)

jobs = set()
answer = 0
nextpush = 0
candidates = []

for day in range(M)[::-1]:
    while nextpush < N:
        a, b = AB[nextpush]
        if day + a <= M:
            heapq.heappush(candidates, (-b, a))
            nextpush+=1
        else:
            break
    if candidates:
        b, a = heapq.heappop(candidates)
        answer -= b
print(answer)
# Original solution
import heapq

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

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

AB = sorted(AB)

jobs = set()
answer = 0
nextpush = 0
candidates = []

for day in range(M)[::-1]:
    while nextpush < N:
        a, b = AB[nextpush]
        if day + a <= M:
            heapq.heappush(candidates, (-b, a))
            nextpush+=1
        else:
            break
    if candidates:
        b, a = heapq.heappop(candidates)
        answer -= b
print(answer)





# Optimized solution
# 優先度キューを使う
import heapq

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

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

# 毎日、候補となる集合を再計算すると間に合わない
# M-1日後から当日に向けて計算してけば、
# day 日時点で day + a = M となる仕事だけを候補に追加することで
# day + 1日までに追加した候補を再利用できる。
# さらに、候補からpopした要素は次回から考慮しないため、設問の条件とも一致する。

# 仕事の候補、徐々に追加していく
candidates = []
# nextpush 次に候補としてpush するか判定するcandidates の添え字
nextpush = 0

# 期間の短いものから候補に入れていくので期間でソートしておく
# AB[i][0] が期間なので、キー指定不要

AB = sorted(AB)

# 回答
answer = 0

for day in range(M)[::-1]:
    while nextpush < N:
        a, b = AB[nextpush]
        if day + a == M:
            # 給料が高い順ものを優先して取り出したいので、-b をキューにいれる。
            heapq.heappush(candidates, -b)
            nextpush += 1
        else:
            # AB はソート済みなので、これ以降の要素に候補に追加できるものはない
            # 次回は nextpush から再開する。
            break
    if candidates: # 仕事が出来ない日もあるかも・・・
        b = heapq.heappop(candidates)
        # マイナスで入れてるので、符号を逆にして戻す
        answer -= b

print(answer)
# Original solution
import heapq

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

baitos = sorted(baitos)
# print(sorted(baitos))
# print(baitos)

money = 0
avail = []
heapq.heapify(avail)
for day in range(m):
    while baitos:
        if baitos[0][0] > day + 1:
            break
        else:
            heapq.heappush(avail, -baitos[0][1])
            baitos.remove(baitos[0])
    # for baito in baitos:
    #     print(baito)
    #     if baito[0] > day + 1:
    #         print("break")
    #         break
    #     else:
    #         heapq.heappush(avail, -baito[1])
    #         baitos.remove(baito)
    # print("baitos", baitos)
    # print("avail", avail)
    if avail:

        money += -heapq.heappop(avail)
        # print(day, money)
    # money += max[1]
    # baitos.remove(max)

print(money)


# Optimized solution
import heapq

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

baitos = sorted(baitos)
# print(sorted(baitos))
# print(baitos)

money = 0
avail = []
# heapq.heapify(avail)
index = 0
for day in range(m):
    while index < n and baitos[index][0] <= day + 1:
        heapq.heappush(avail, -baitos[index][1])
        index += 1
        # baitos.remove(baitos[0])
    # for baito in baitos:
    #     print(baito)
    #     if baito[0] > day + 1:
    #         print("break")
    #         break
    #     else:
    #         heapq.heappush(avail, -baito[1])
    #         baitos.remove(baito)
    # print("baitos", baitos)
    # print("avail", avail)
    if avail:

        money += -heapq.heappop(avail)
        # print(day, money)
    # money += max[1]
    # baitos.remove(max)

print(money)
# Original solution
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = []
    for i in range(n):
        a, b = list(map(int, input().rstrip('\n').split()))
        ab.append([a, b])
    heapq.heapify(ab)
    s_q = []
    heapq.heapify(s_q)
    d = m
    r = 0
    while True:
        while True:
            if len(ab) != 0:
                p = heapq.heappop(ab)
                if p[0] + d <= m:
                    heapq.heappush(s_q, -p[1])
                else:
                    heapq.heappush(ab, p)
                    break
            else:
                break
        if len(s_q) != 0:
            mr = -heapq.heappop(s_q)
            r += mr
        d -= 1
        if d == -1:
            break
    print(r)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    heapq.heapify(ab)
    s_q = []
    heapq.heapify(s_q)
    r = 0
    for d in range(1, m + 1):
        while True:
            if len(ab) != 0:
                p = heapq.heappop(ab)
                if p[0] <= d:
                    heapq.heappush(s_q, -p[1])
                else:
                    heapq.heappush(ab, p)
                    break
            else:
                break
        if len(s_q) != 0:
            mr = -heapq.heappop(s_q)
            r += mr
        d -= 1
    print(r)


if __name__ == '__main__':
    slove()
# Original solution
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    heapq.heapify(ab)
    s_q = []
    heapq.heapify(s_q)
    r = 0
    for d in range(1, m + 1):
        while True:
            if len(ab) != 0:
                p = heapq.heappop(ab)
                if p[0] <= d:
                    heapq.heappush(s_q, -p[1])
                else:
                    heapq.heappush(ab, p)
                    break
            else:
                break
        if len(s_q) != 0:
            mr = -heapq.heappop(s_q)
            r += mr
        d -= 1
    print(r)


if __name__ == '__main__':
    slove()


# Optimized solution
def slove():
    import sys
    import heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    l = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    r = []
    heapq.heapify(l)
    heapq.heapify(r)
    t = 0
    for i in range(m + 1):
        while True:
            if len(l) != 0:
                a, b = heapq.heappop(l)
                if a <= i:
                    heapq.heappush(r, -b)
                else:
                    heapq.heappush(l, [a, b])
                    break
            else:
                break
        if len(r) != 0:
            t += -heapq.heappop(r)
    print(t)


if __name__ == '__main__':
    slove()
# Original solution
def slove():
    import sys
    import heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ls =[]
    v = []
    heapq.heapify(v)
    for i in range(n):
        a, b = list(map(int, input().rstrip('\n').split()))
        ls.append([a, b])
    ls.sort(reverse=True)
    r = 0
    for i in range(1, m + 1):
        while True:
            if len(ls) != 0:
                a, b = ls.pop()
                if a <= i:
                    heapq.heappush(v, -b)
                else:
                    ls.append([a, b])
                    break
            else:
                break
        if len(v) != 0:
            r += -heapq.heappop(v)
    print(r)


if __name__ == '__main__':
    slove()


# Optimized solution
import sys
import heapq


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    ab.sort(reverse=True)
    ls = []
    heapq.heapify(ls)
    t = 0
    for i in range(1, m + 1):
        while True:
            if len(ab) != 0:
                if ab[-1][0] <= i:
                    heapq.heappush(ls, -ab.pop()[1])
                else:
                    break
            else:
                break
        if len(ls) != 0:
            t -= heapq.heappop(ls)
    print(t)


if __name__ == '__main__':
    solve()
# Original solution
import heapq
from collections import defaultdict

def main():
    N, M = list(map(int, input().split()))
    ans = 0
    d = defaultdict(list)
    for _ in range(N):
        A, B = list(map(int, input().split()))
        d[A].append(-B)
    cand = []
    heapq.heapify(cand)
    for day in range(M-1,-1,-1):
        diff = M - day
        if d[diff]:
            cand += d[diff]
            heapq.heapify(cand)
        if cand:
            ans -= heapq.heappop(cand)

    print(ans)



if __name__ == "__main__":
    main()

# Optimized solution
import heapq
from collections import defaultdict

def main():
    N, M = list(map(int, input().split()))
    ans = 0
    d = defaultdict(list)
    for _ in range(N):
        A, B = list(map(int, input().split()))
        d[A].append(-B)
    cand = []
    heapq.heapify(cand)
    for day in range(M-1,-1,-1):
        diff = M - day
        for money in d[diff]:
            heapq.heappush(cand, money)
        if cand:
            ans -= heapq.heappop(cand)

    print(ans)



if __name__ == "__main__":
    main()
# Original solution
from collections import defaultdict

n, m = list(map(int, input().split()))
ab = sorted([list(map(int, input().split())) for i in range(n)])
c = [[]] * m
a, b = ab[0]
last = a
ad = [b]
for i in range(1, n):
    a, b = ab[i]
    if a == last:
        ad.append(b)
    else:
        if last <= m:
            c[last - 1] = ad
        ad = [b]
    last = a
if a <= m:
    c[a - 1] = ad
choice = defaultdict(int)
ans = 0
for i in range(m):
    for j in c[i]:
        choice[j] += 1
    if choice:
        use = max(choice)
        ans += use
        choice[use] -= 1
        if choice[use] <= 0:
            del choice[use]
print(ans)


# Optimized solution
import heapq

n, m = list(map(int, input().split()))
ab = sorted([list(map(int, input().split())) for i in range(n)])
c = [[]] * m
a, b = ab[0]
last = a
ad = [b]
for i in range(1, n):
    a, b = ab[i]
    if a == last:
        ad.append(b)
    else:
        if last <= m:
            c[last - 1] = ad
        ad = [b]
    last = a
if a <= m:
    c[a - 1] = ad
a = [3, 0, 2, 6]
choice = []
heapq.heapify(choice)
ans = 0
for i in range(m):
    for j in c[i]:
        heapq.heappush(choice, -j)
    if choice:
        ans -= heapq.heappop(choice)
print(ans)
# Original solution
import bisect
n,m=list(map(int,input().split()))
l=[[] for _ in range(m)]
for _ in range(n):
  a,b=list(map(int,input().split()))
  if a<=m:
    l[a-1].append(b)
ans=0
w=[]
for i in range(m):
  for j in l[i]:
    bisect.insort_right(w,j)
  if w!=[]:
    ans+=w.pop(-1)
print(ans)

# Optimized solution
import heapq
n,m=list(map(int,input().split()))
l=[[] for _ in range(m)]
for _ in range(n):
  a,b=list(map(int,input().split()))
  if a<=m:
    l[a-1].append(b)
ans=0
w=[]
for i in range(m):
  for j in l[i]:
    heapq.heappush(w,-j)
  if w!=[]:
    ans-=heapq.heappop(w)
print(ans)
# Original solution
# -*- coding: utf-8 -*-
"""
D - Summer Vacation
https://atcoder.jp/contests/abc137/tasks/abc137_d

"""
import sys


from heapq import heapify, heappop
from bisect import bisect_left

def solve(N, M, jobs):
    days = [d for d in range(1, M+1)]
    heapify(jobs)
    ans = 0
    while jobs:
        B, A = heappop(jobs)
        i = bisect_left(days, -A)
        if i != len(days):
            days.pop(i)
            ans -= B
    return ans


def main(args):
    N, M = list(map(int, input().split()))
    jobs = []
    for _ in range(N):
        A, B = list(map(int, input().split()))
        jobs.append([-B, -A])
    ans = solve(N, M, jobs)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


# Optimized solution
# -*- coding: utf-8 -*-
"""
D - Summer Vacation
https://atcoder.jp/contests/abc137/tasks/abc137_d

"""
import sys


from heapq import heapify, heappop, heappush

def solve(N, M, jobs):
    heapify(jobs)
    availables = []
    ans = 0
    for day in range(1, M+1):
        while jobs and jobs[0][0] <= day:
            A, B = heappop(jobs)
            heappush(availables, (-B))
        if availables:
            ans -= heappop(availables)
    return ans


def main(args):
    N, M = list(map(int, input().split()))
    jobs = []
    for _ in range(N):
        A, B = list(map(int, input().split()))
        jobs.append((A, B))
    ans = solve(N, M, jobs)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in reversed(list(range(M))):
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = list(map(int, readline().split()))
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = list(map(int, readline().split()))
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s-=heappop(h)
    print(s)
if __name__ == '__main__':
    main()
# Original solution
from operator import itemgetter

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

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

res = [0 for _ in range(M)]

for i, (a, b) in enumerate(AB):
    p = M - a
    while p >= 0 and res[p] != 0:
        p -= 1
    if p >= 0:
        res[p] = b

print((sum(res)))


# Optimized solution
import heapq

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

AB = [[] for _ in range(M)]

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

res = 0
cand = []

for i in range(M):
    AB[i].sort(reverse=True)
    for b in AB[i]:
        heapq.heappush(cand, b)
    if cand:
        mb = heapq.heappop(cand)
        res -= mb

print(res)
# Original solution
n,m = list(map(int,input().split()))
from collections import defaultdict
dic = defaultdict(list)
l = []
for i in range(n):
    a,b=list(map(int,input().split()))
    dic[a].append(b)
s = 0
t = [0]
for i in range(m):
    t += dic[i+1]
    if len(t) ==0 : M = 0
    else:
        M = max(t)
        s +=M
        t.remove(M)
print(s)

# Optimized solution
n,m = list(map(int,input().split()))
import heapq
from collections import defaultdict
dic = defaultdict(list)
l = []
heapq.heapify(l)
for i in range(n):
    a,b=list(map(int,input().split()))
    b = -b
    dic[a].append(b)
s = 0
t = [0]
for i in range(m):
    for i in dic[i+1]:
        heapq.heappush(l,i)
    if len(l) ==0 : M = 0
    else:
        # print(l)
        M = heapq.heappop(l)
        s += -M
print(s)
# Original solution
from operator import itemgetter
n, m = list(map(int, input().split()))
abab = [list(map(int, input().split())) for _ in range(n)]
abab = sorted(sorted(abab, key=itemgetter(0), reverse=True), key=itemgetter(1), reverse=True)

gyara = [0 for _ in range(m+1)]

for a, b in abab :
    for i in range(a, m+1) :
        if gyara[i] == 0 :
            gyara[i] = b
            break

print((sum(gyara)))

# Optimized solution
from heapq import *

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

# Optimized solution
from heapq import heappush, heappop
n, m = list(map(int, input().split()))
jobs = {}
for _ in range(n):
  a, b = list(map(int, input().split()))
  if a > m:
    continue
  if a in jobs:
    jobs[a].append(b)
  else:
    jobs[a] = [b]
result = 0
candidates = []
for a in range(1, m + 1):
  if a in jobs:
    for b in jobs[a]:
      heappush(candidates, -b)
  else:
    if len(candidates) == 0:
      continue
  result += -heappop(candidates)
print(result)
# Original solution
from heapq import heappush, heappop
n, m = list(map(int, input().split()))
jobs = {}
for _ in range(n):
  a, b = list(map(int, input().split()))
  if a > m:
    continue
  if a in jobs:
    jobs[a].append(b)
  else:
    jobs[a] = [b]
result = 0
candidates = []
for a in range(1, m + 1):
  if a in jobs:
    for b in jobs[a]:
      heappush(candidates, -b)
  else:
    if len(candidates) == 0:
      continue
  result += -heappop(candidates)
print(result)

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

def calculate(N,M,arr):
    ret = 0

    j = 0

    res = []

    # 1,2,3,4,5
    #         ↑
    # 4,3,2,1

    # M = 4
    # range(1,5) => 1,2,3,4

    for i in range(1,M+1):
        # i = 1,2,3
        # 比如M=4的话，第三天(最多1天),第二天(最多2天),第一天(最多3天)

        # 记下来循环打工信息资源,以j为标记
        # j为资源编号
        # arr[j][0]为该编号资源的天数


        # 候补的资源，满足所有天数，因为是满足后续的天数来算的

        while (j < N) and (arr[j][0] <= i):

                heappush(res, -arr[j][1])
                j = j + 1 #j找到的话就往上顶一格，一共这一天，或者前一天寻找的时候以下一个资源天为开始

        if len(res) > 0:
            ret -= heappop(res)

    return ret


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


# 后面的逻辑是基于前面的从小到大的排序的
list = [list(map(int,input().split())) for i in range(N)]

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

result = calculate(N,M,arr)

print(result)
# Original solution
n, m = list(map(int, input().split(" ")))
xys = [tuple(map(int, input().split(" "))) for i in range(n)]
xys = sorted(xys, key=lambda x: (-x[0], x[1]))

dp = [[0 for x in range(m)] for y in range(n)]

for i in range(n):
    a, b = xys[i]
    # print(i, a, b)
    if i == 0:
        for j in range(0, m - a + 1):
            dp[i][j] = b
    else:
        dp[i][0] = max(dp[i - 1][0], b)
        for j in range(1, m - a + 1):
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + b)
    # print(dp[i])

# print(dp)
print((max(max(x) for x in dp)))


# Optimized solution
n, m = list(map(int, input().split(" ")))
xys = [tuple(map(int, input().split(" "))) for i in range(n)]
xys = sorted(xys, key=lambda x: (x[0], -x[1]))

ans = 0

for i in range(1, m + 1):
    cm = -1
    cmi = -1
    for j in range(len(xys)):
        if xys[j][0] <= i and xys[j][1] > cm:
            cm = xys[j][1]
            cmi = j
    if cmi >= 0:
        ans += cm
        xys.pop(cmi)

print(ans)