Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  pool.sort()
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

Fixed submission:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = [0 for _ in range(10**4+1)]
maxi = 0
cnt = 0
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      add = ab[here][1]
      pool[add] += 1
      if add > maxi:
        maxi = add
      here += 1
      cnt += 1
    else:
      break
  if cnt != 0:
    cnt -= 1
    for j in range(10**4):
      '''
      if pool[10**4-j] != 0:
        ans += 10**4-j
        print(10**4-j,i)
        pool[10**4-j] -= 1
        break
      '''
    ans += maxi
    pool[maxi] -= 1
    if pool[maxi] == 0:
      if cnt == 0:
        maxi = 0
      else:
        while True:
          maxi -= 1
          if pool[maxi] != 0:
            break
print(ans)

Fixed submission:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = [0 for _ in range(10**4+1)]
#maxi = 0
cnt = 0
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      add = ab[here][1]
      pool[add] += 1
      '''
      if add > maxi:
        maxi = add
      '''
      here += 1
      cnt += 1
    else:
      break
  if cnt != 0:
    cnt -= 1
    for j in range(10**4):
      if pool[10**4-j] != 0:
        ans += 10**4-j
        #print(10**4-j,i)
        pool[10**4-j] -= 1
        break
    '''
    ans += maxi
    pool[maxi] -= 1
    if pool[maxi] == 0:
      if cnt == 0:
        maxi = 0
      else:
        while True:
          maxi -= 1
          if pool[maxi] != 0:
            break
    '''
print(ans)

Fixed submission:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import bisect
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      add = ab[here][1]
      point = bisect.insort_left(pool,add)
      here += 1
    else:
      break
  try:
    ans += pool.pop(-1)
  except:
    pass
print(ans)

Fixed submission:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      pool.append(ab[here][1])
      here += 1
    else:
      break
  pool.sort()
  try:
    ans += pool[-1]
    del pool[-1]
  except:
    pass
print(ans)

Fixed submission:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
import heapq
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      heapq.heappush(pool,-ab[here][1])
      here += 1
    else:
      break
  try:
    #print(pool)
    ans -= heapq.heappop(pool)
    #print(pool)
  except:
    pass
print(ans)