Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[1],reverse=True)
used=[False]*n
ans=0
for i in range(m,-1,-1):
  left=m-i
  for j in range(len(tasks)):
    if used[j]==False and tasks[j][0]<=left:
      ans+=tasks[j][1]
      used[j]=True
      break

print(ans)


Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[1],reverse=True)
ans=0
for i in range(m,-1,-1):
  left=m-i
  for j in range(len(tasks)):
    if tasks[j][0]<=left:
      ans+=tasks.pop(j)[1]
      break

print(ans)


Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

def solve():
  total=0
  for i in range(m,-1,-1):
#  print("i:"+str(i))
    max=0
    max_j=0
    found=False
    for j in range(len(w)):
      if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
        break
      elif max < w[j][1]:
#        print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
        max=w[j][1]
        max_j=j
        found=True
#  print(str(max) + "の仕事をやる")
    total = total + max
    if found:
#    print(str(max_j)+"番目の仕事を削除")
      w.pop(max_j)
#    print(w)
  return total
      
    
print(solve())

Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

def solve():
  total=0
  for i in range(m,-1,-1):
#  print("i:"+str(i))
    max=0
    max_j=0
    found=False
    for j in range(len(w)):
      if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
        break
      elif max < w[j][1]:
#        print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
        max=w[j][1]
        max_j=j
        found=True
#  print(str(max) + "の仕事をやる")
    total = total + max
    if found:
#    print(str(max_j)+"番目の仕事を削除")
      w.pop(max_j)
#    print(w)
  return total
      
    
print(solve())

Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

total=0
for i in range(m,-1,-1):
#  print("i:"+str(i))
  max=0
  max_j=0
  found=False
  for j in range(len(w)):
    if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
      break
    elif max < w[j][1]:
#      print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
      max=w[j][1]
      max_j=j
      found=True
#  print(str(max) + "の仕事をやる")
  total = total + max
  if found:
#    print(str(max_j)+"番目の仕事を削除")
    w.pop(max_j)
#    print(w)
      
      
print(total)


Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

total=0
for i in range(m,-1,-1):
#  print("i:"+str(i))
  max=0
  max_j=0
  found=False
  for j in range(len(w)):
    if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
      break
    elif max < w[j][1]:
#      print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
      max=w[j][1]
      max_j=j
      found=True
#  print(str(max) + "の仕事をやる")
  total = total + max
  if found:
#    print(str(max_j)+"番目の仕事を削除")
    w.pop(max_j)
#    print(w)
      
      
print(total)

Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

total = 0
for i in range(m):
  max=0
  max_j=0
  max_day=0
  #残り日数 m-iより支払い日が短いもので最大の値をとる
  for j in range(len(w)):
    if w[j][0] <= m-i and w[j][1] > max: # 最大値が来たときは置き換え
      max = w[j][1]
      max_j=j
      max_day=w[j][0]
    elif w[j][0] <= m-i and w[j][1] == max:#最大値と同じ値のときは、日数が長いものを採用
      if w[j][0] > max_day:
        max = w[j][1]
        max_j=j
        max_day=w[j][0]
        
  total = total+max
  if(len(w)>0):
    w.remove(max_j)

print(total)

Fixed submission:
n,m=map(int,input().split())
tasks=[None]*n
for i in range(n):
  a,b=map(int,input().split())
  tasks[i]=[a,b]

tasks=sorted(tasks,key=lambda x:x[0])
import heapq
que=[]
ans=0
heapq.heapify(que)
j=0
for i in range(m,-1,-1):
  left=m-i
  while j<len(tasks):
    if tasks[j][0]<=left:
      heapq.heappush(que, tasks[j][1]*(-1))
    else:
      break
    j+=1
  if len(que)>0:
    val=heapq.heappop(que)*(-1)
    ans+=val
      
print(ans)