Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import heapq

n, m = map(int, input().split())

dict = {}
for i in range(n):
    ta, tb = map(int, input().split())
    #後でheapqから最大値を取り出したいので-1倍して追加
    if dict.get(ta) == None:
        dict[ta] = []
        dict[ta].append(tb * -1) #新しいAiを追加
    else:
        dict[ta].append(tb * -1) #既存のAiにBiを追加
#print(dict)

cand = []
ans = 0
for i in range(1, m+1):
    #１　要素の追加
    if dict.get(i) == None:
        pass
    else:
        add = dict[i]
        cand.extend(add)
        heapq.heapify(cand) #ヒープ化
        #print(cand)

    #２　heapから最大値を取り出す
    try:
        max_val = heapq.heappop(cand)
        #print(max_val)
        ans += max_val * (-1) #最大値に直す
    except: #heapに何も入っていない
        pass
#print("----")
print(ans)



Fixed submission:
# -*- coding: utf-8 -*-

import heapq

n, m = map(int, input().split())

work = [[] for _ in range(10**5+1)]
for i in range(n):
    ta, tb = map(int, input().split())
    work[ta].append(tb)

total = 0
cand = []

for d in range(1, m+1):
    for w in work[d]:
        #後で最大値を取り出したいので-1倍して追加
        #（複数の要素も一気にpush）
        heapq.heappush(cand, -w) 
        
    if len(cand) > 0:
        #-1倍して最大値として取得
        total += heapq.heappop(cand)*(-1) 
        
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import numpy as np

n, m=map(int, input().split())
ab = [["",""] for _ in range(n)]
for i in range(n):
    ab[i]=list(map(int, input().split()))

sec_sort = lambda val: val[1]
ab.sort(key = sec_sort, reverse=True)
#print(ab)

abs = [[] for _ in range(4)]

for i in range(n):
    abs[0].append(i) 
    abs[1].append(ab[i][0]) 
    abs[2].append(ab[i][1]) 
    abs[3].append(0) 

np_abs = np.array(abs)
#print(np_abs)

sum = 0
for dayLeft in range(1,m+1):

    CanWork = (np_abs[1] <= dayLeft) #日数がA以内
    isWorkLeft = (np_abs[3] == 0)
    isCandidate = np.logical_and(CanWork, isWorkLeft)
#    print(isCandidate)

    #候補が1以降ならば
    if np.any(isCandidate == True):
        index = np.min(np_abs[0][isCandidate]) #日数がA以内かつソート昇順最初のindex
#        print(index)
        sum += np_abs[2][index] #indexのBを取得して足しこむ
        np_abs[3][index] = 1 #0→1に

    #候補がなければ次のdayLeftへ
    else:
        continue

print(sum)

Fixed submission:
# -*- coding: utf-8 -*-

import heapq

n, m = map(int, input().split())

work = [[] for _ in range(10**5+1)]
for i in range(n):
    ta, tb = map(int, input().split())
    work[ta].append(tb)

total = 0
cand = []

for d in range(1, m+1):
    for w in work[d]:
        #後で最大値を取り出したいので-1倍して追加
        #（複数の要素も一気にpush）
        heapq.heappush(cand, -w) 
        
    if len(cand) > 0:
        #-1倍して最大値として取得
        total += heapq.heappop(cand)*(-1) 
        
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import numpy as np

n, m=map(int, input().split())
ab = [["" for _ in range(2)] for _ in range(n)]
for i in range(n):
    temp=list(map(int, input().split()))
    ab[i] = [temp[0],temp[1],False]

sortsecond = lambda val: val[1]

ab.sort(key = sortsecond, reverse = True)
#print(ab)

sum = 0
for dayLeft in range(1, m+1):
#    print(str(dayLeft)+"スタート")
    for i in range(n):
        if((ab[i][0] <= dayLeft) and (ab[i][2] == False)):
            sum += ab[i][1]
#            print(str(ab[i][1])+"を追加")
            ab[i][2] = True
#            print(ab)
            break

print(sum)


Fixed submission:
# -*- coding: utf-8 -*-

import heapq

n, m = map(int, input().split())

work = [[] for _ in range(10**5+1)]
for i in range(n):
    ta, tb = map(int, input().split())
    work[ta].append(tb)

total = 0
cand = []

for d in range(1, m+1):
    for w in work[d]:
        #後で最大値を取り出したいので-1倍して追加
        #（複数の要素も一気にpush）
        heapq.heappush(cand, -w) 
        
    if len(cand) > 0:
        #-1倍して最大値として取得
        total += heapq.heappop(cand)*(-1) 
        
print(total)