Buggy python submission (verdict: Time Limit Exceeded):
n, m = list(map(int, input().split()))
ab = []
for _ in range(n):
    ai, bi = list(map(int, input().split()))
    ab.append([ai,bi])
    
ab.sort(key=lambda x: x[0])
ab.sort(key=lambda x: x[1], reverse=True)

dayworked = set()
cnt = 0
for abi in ab:
    if abi[0] <= m:
        daycanwork = set(range(m-abi[0]+1))
        days = daycanwork - dayworked
        if len(days) == 0:
            continue
        day = max(days)
        dayworked.add(day)
        cnt += abi[1]   

print(cnt)

Fixed submission:
import heapq

n, m = list(map(int, input().split()))
tasks = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    tasks.append((-bi, i, ai))
tasks.sort(key=lambda x: x[2])    
prtasks = []
money = 0
j = 0
for i in range(1,m+1):
    for k in range(j,len(tasks)):
        if tasks[k][2] <= i:
            heapq.heappush(prtasks, tasks[k])
            j += 1
        else:
            break
    if not prtasks:
        continue
    work = heapq.heappop(prtasks)
    money += work[0]

print(-money)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 10 21:26:22 2019
ABC137D
@author: maezawa
"""

n, m = list(map(int, input().split()))
ab = []
for _ in range(n):
    ai, bi = list(map(int, input().split()))
    ab.append([ai,bi])
    
ab.sort(key=lambda x: x[0])
ab.sort(key=lambda x: x[1], reverse=True)

day = [0]*(m+1)
cnt = 0
for abi in ab:
    if abi[0] <= m:
        i = 0
        while m-abi[0]-i>=0:
            if day[m-abi[0]-i] == 0:
                day[m-abi[0]-i] = 1
                cnt += abi[1]
                break
            i += 1
print(cnt)

    


Fixed submission:
import heapq

n, m = list(map(int, input().split()))
tasks = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    tasks.append((-bi, i, ai))
tasks.sort(key=lambda x: x[2])    
prtasks = []
money = 0
j = 0
for i in range(1,m+1):
    for k in range(j,len(tasks)):
        if tasks[k][2] <= i:
            heapq.heappush(prtasks, tasks[k])
            j += 1
        else:
            break
    if not prtasks:
        continue
    work = heapq.heappop(prtasks)
    money += work[0]

print(-money)

Buggy python submission (verdict: Time Limit Exceeded):

n, m = list(map(int, input().split()))
ab = []
for _ in range(n):
    ai, bi = list(map(int, input().split()))
    ab.append([ai,bi])
    
ab.sort(key=lambda x: x[0])
ab.sort(key=lambda x: x[1], reverse=True)

day = [0]*(m+1)
cnt = 0
for abi in ab:
    if abi[0] <= m:
        if day[m-abi[0]] == 0:
            day[m-abi[0]] = 1
            cnt += abi[1]
        else:
            i = 1
            while m-abi[0]-i>=0:
                if day[m-abi[0]-i] == 0:
                    day[m-abi[0]-i] = 1
                    cnt += abi[1]
                    break
                i += 1
print(cnt)


Fixed submission:
import heapq

n, m = list(map(int, input().split()))
tasks = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    tasks.append((-bi, i, ai))
tasks.sort(key=lambda x: x[2])    
prtasks = []
money = 0
j = 0
for i in range(1,m+1):
    for k in range(j,len(tasks)):
        if tasks[k][2] <= i:
            heapq.heappush(prtasks, tasks[k])
            j += 1
        else:
            break
    if not prtasks:
        continue
    work = heapq.heappop(prtasks)
    money += work[0]

print(-money)