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

sys.setrecursionlimit(4100000)

B = 10


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    jobs = list(map(string_to_int, inputs))
    jobs.sort(key=lambda x: (x[0]))
    jobs = deque(jobs)

    can_select_jobs = []
    money = 0

    for day in range(1, M + 1):
        while len(jobs) != 0:
            if jobs[0][0] > day:
                break
            else:
                can_select_jobs.append(jobs.popleft())

        if len(can_select_jobs) != 0:
            can_select_jobs.sort(key=lambda x: x[1])
            best_job = can_select_jobs.pop()
            money += best_job[1]

        new_can_select_jobs = []

        while len(new_can_select_jobs) <= M and len(can_select_jobs) != 0:
            new_can_select_jobs.append(can_select_jobs.pop())
        can_select_jobs = new_can_select_jobs
    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)


Fixed submission:
import sys
from pprint import pprint
import heapq

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    jobs = list(map(string_to_int, inputs))
    jobs.sort(key=lambda x: x[0], reverse=True)
    selectable = []
    money = 0
    for day in range(1, M + 1):
        while len(jobs) != 0 and jobs[-1][0] <= day:
            heapq.heappush(selectable, jobs.pop()[1] * -1)
        if len(selectable) != 0:
            money += heapq.heappop(selectable) * -1
    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)

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

sys.setrecursionlimit(4100000)

B = 10


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    jobs = list(map(string_to_int, inputs))
    jobs.sort(key=lambda x: (x[0]))

    money = 0
    for day in range(1, M + 1):
        max_money = 0
        max_i = -1
        for i, j in enumerate(jobs):
            if j[0] > day:
                break
            if max_money < j[1]:
                max_money = j[1]
                max_i = i
        if max_i != -1:
            money += max_money
            jobs.pop(max_i)

    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)


Fixed submission:
import sys
from pprint import pprint
import heapq

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, M, inputs):
    jobs = list(map(string_to_int, inputs))
    jobs.sort(key=lambda x: x[0], reverse=True)
    selectable = []
    money = 0
    for day in range(1, M + 1):
        while len(jobs) != 0 and jobs[-1][0] <= day:
            heapq.heappush(selectable, jobs.pop()[1] * -1)
        if len(selectable) != 0:
            money += heapq.heappop(selectable) * -1
    return money


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, M] = string_to_int(input())
    ret = solve(N, M, inputs(N))
    print(ret)