Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect



def main():
    num, max_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x:(-x[1]))
    # for


    visit = [0 for i in range(max_num + 1)]

    ans = 0
    # print(data)
    for i in range(num):
        day, value = data[i]
        ind_lim = day
        try:
            ind_insert = visit[ind_lim:].index(0)
            visit[ind_insert + ind_lim] = 1
            ans += value
        except:
            pass
        # print(visit)

    print(ans)







if __name__ == '__main__':
    main()


Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect


visit = []

def aaa(ind_lim, dame_num):
    global visit
    if ind_lim >= dame_num:
        return -1
    elif visit[ind_lim] == -1:
        return -1
    elif visit[ind_lim] == 0:
        return ind_lim
    else:
        visit[ind_lim] = aaa(visit[ind_lim], dame_num)
        return visit[ind_lim]

def main():
    num, max_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x:(-x[1]))
    # for

    global visit
    visit = [0 for i in range(max_num + 1)]

    ans = 0

    for i in range(num):
        day, value = data[i]
        ind_lim = day
        # print(i, num, visit)
        ind_insert = aaa(ind_lim, max_num + 1)
        if ind_insert != -1:
            visit[ind_insert] = ind_insert + 1
            ans += value

    # print('uuu')
    print(ans)







if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect


from math import factorial
# 組み合わせ
def combinations_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


def check(left, right, num, max_num, data):
    half = (left + right) // 2
    kari = 0


def main():
    num, max_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x: (-x[1]))

    visit = [0 for i in range(max_num + 1)]

    ans = 0
    left = 0
    # print(data)
    for i in range(num):
        day, value = data[i]
        for j in range(left, max_num + 1 - day)[::-1]:
            if visit[j] == 0:
                ans += value
                visit[j] = 1
                break
        else:
            left = max_num - day
    # print(visit)
    print(ans)







if __name__ == '__main__':
    main()


Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect


visit = []

def aaa(ind_lim, dame_num):
    global visit
    if ind_lim >= dame_num:
        return -1
    elif visit[ind_lim] == -1:
        return -1
    elif visit[ind_lim] == 0:
        return ind_lim
    else:
        visit[ind_lim] = aaa(visit[ind_lim], dame_num)
        return visit[ind_lim]

def main():
    num, max_num = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    data.sort(key=lambda x:(-x[1]))
    # for

    global visit
    visit = [0 for i in range(max_num + 1)]

    ans = 0

    for i in range(num):
        day, value = data[i]
        ind_lim = day
        # print(i, num, visit)
        ind_insert = aaa(ind_lim, max_num + 1)
        if ind_insert != -1:
            visit[ind_insert] = ind_insert + 1
            ans += value

    # print('uuu')
    print(ans)







if __name__ == '__main__':
    main()