Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
absort=sorted(ab,key=lambda x:(x[0],-x[1]))

ans=0
temp=[]
for i in range(1,m+1):
    while absort:
        if absort[0][0]<=i:
            temp.append(absort[0][1])
            del absort[0]
        else:
            break
    if temp:
        tmpmax=max(temp)
        ans+=tmpmax
        temp.remove(tmpmax)

print(ans)


Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
absort=sorted(ab,key=lambda x:(x[0],-x[1]))

ans=0
temp=[]
for i in range(1,m+1):
    while absort:
        if absort[0][0]<=i:
            temp.append(absort[0][1])
            del absort[0]
        else:
            break
    if temp:
        tmpmax=max(temp)
        ans+=tmpmax
        temp.remove(tmpmax)

print(ans)


Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
absort=sorted(ab,key=lambda x:(x[0],-x[1]))

ans=0
temp=[]
for i in range(1,m+1):
    while absort:
        if absort[0][0]<=i:
            temp.append(absort[0][1])
            del absort[0]
        else:
            break
    if temp:
        tmpmax=max(temp)
        ans+=tmpmax
        temp.remove(tmpmax)

print(ans)


Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
absort=sorted(ab,key=lambda x:(x[0],-x[1]))

ans=0
temp=[]
for i in range(1,m+1):
    while absort:
        if absort[0][0]<=i:
            temp.append(absort.pop(0))
        else:
            break
    temp=sorted(temp,key=lambda x:x[1], reverse=True)
    if temp:
        ans+=temp[0][1]
        temp.pop(0)

print(ans)


Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
absort=sorted(ab,key=lambda x:(x[0],-x[1]))

ans=0
for i in range(1,m+1):
    maxa=0
    temp=[]
    while absort:
        if absort[0][0]<=i:
            maxa=max(maxa,absort[0][1])
            temp.append(absort.pop(0))
        else:
            break
    ans+=maxa
    temp=sorted(temp,key=lambda x:(-x[1]))
    if len(temp)>0:
        del temp[0]
    temp+=absort
    absort=sorted(temp,key=lambda x:(x[0],-x[1]))


print(ans)

Fixed submission:
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)