Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop, merge, heapify

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    if a <= M:
        works[a].append(-b)

cand = []
answer = 0
for i in range(1, M+1):
    cand = list(merge(cand, works[i]))
    heapify(cand)
    if cand:
        answer -= heappop(cand)

print(answer)


Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
#Bが報酬
AB = [(a, b) for a, b in AB if a<=M]
AB.sort(reverse=True, key=lambda x: x[1])
AB = deque(AB)
answer = 0
used = set()
out = set()
while AB:
    x, y = AB.popleft()
    if x > M or x in out:
        continue
    else:
        z = x
        while z in used:
            if z in out:
                z = M+1
            z += 1
        if z > M:
            out = out.union(set(range(x, z)))
            continue
        used.add(z)
        answer += y

print(answer)
        


Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
#Bが報酬
AB = [(a, b) for a, b in AB if a<=M]
AB.sort(reverse=True, key=lambda x: x[1])
AB = deque(AB)
answer = 0
used = set()
out = set()
while AB:
    x, y = AB.popleft()
    if x > M or x in out:
        continue
    else:
        z = x
        while z in used:
            z += 1
        if z > M:
            out = out.union(set(range(x, z)))
            continue
        used.add(z)
        answer += y

print(answer)
        


Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Runtime Error):
import sys
from heapq import heappush, heappop

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    works[a].append(b)

cand = []
answer = 0
for i in range(1, M+1):
    for work in works[i]:
        heappush(cand, -work)
    if cand:
        answer -= heappop(cand)

print(answer)


Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Runtime Error):
import sys
from heapq import heappush, heappop

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    works[a].append(b)

cand = []
answer = 0
for i in range(1, M+1):
    for work in works[i]:
        heappush(cand, -work)
    answer -= heappop(cand)

print(answer)


Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
#Bが報酬
AB = [(a, b) for a, b in AB if a<=M]
AB.sort(reverse=True, key=lambda x: x[1])
AB = deque(AB)
answer = 0
used = set()
out = set()
while AB:
    x, y = AB.popleft()
    if x > M or x in out:
        continue
    else:
        z = x
        while z in used:
            z += 1
        if z > M:
            out.add(x)
            continue
        used.add(z)
        answer += y

print(answer)
        

Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
AB = [lr() for _ in range(N)]
#Bが報酬
AB = [(a, b) for a, b in AB if a<=M]
AB.sort(reverse=True, key=lambda x: x[1])
AB = deque(AB)
answer = 0
used = set()
while AB:
    x, y = AB.popleft()
    if x > M:
        continue
    else:
        while x in used:
            x += 1
        if x > M:
            continue
        used.add(x)
        answer += y

print(answer)
        

Fixed submission:
import heapq

n,m=map(int,input().split())
jobs=[[] for _ in range(m)]
for i in range(n):
    d,r=map(int,input().split())
    if d-1<m:
        jobs[d-1].append(-r)
rewards=0
heap=[]
for i in range(m):
    for reward in jobs[i]:
        heapq.heappush(heap,reward)
    if len(heap)>0:
        rewards+=-heapq.heappop(heap)

print(rewards)