Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=sorted(c[i],reverse=True)
    if l:
      if max(j)>max(l):
        k+=max(j)
        if len(j)>1:
          l=l+j[1:]
      else:
        k+=max(l)
        del l[l.index(max(l))]
        l=l+j
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=max(l)
      del l[l.index(max(l))]
  if len(l)>m:
    l=l[:m]
print(k)


Fixed submission:
import heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)

Buggy python submission (verdict: Runtime Error):
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=sorted(c[i],reverse=True)
    if l:
      if max(j)>l[0]:
        k+=max(j)
        if len(j)>1:
          l=l+j[1:]
      else:
        k+=max(l)
        del l.index(max(l))
        l=l+j
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=l[0]
      del l.index(max(l))
  if len(l)>m:
    l=l[:m]
print(k)


Fixed submission:
import heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=sorted(c[i],reverse=True)
    if l:
      if j[0]>l[0]:
        k+=j[0]
        if len(j)>1:
          l=sorted(l+j[1:],reverse=True)
      else:
        k+=l[0]
        l=sorted(l[1:]+j,reverse=True)
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=l[0]
      l=l[1:]
  if len(l)>m:
    l=l[:m]
print(k)


Fixed submission:
import heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=list(reversed(sorted(c[i])))
    if l:
      if j[0]>l[0]:
        k+=j[0]
        if len(j)>1:
          l=list(reversed(sorted(l+j[1:])))
      else:
        k+=l[0]
        l=list(reversed(sorted(l[1:]+j)))
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=l[0]
      l=l[1:]
  if len(l)>m:
    l=l[:m]
print(k)

Fixed submission:
import heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=list(reversed(sorted(c[i])))
    if l:
      if j[0]>l[0]:
        k+=j[0]
        if len(j)>1:
          l=list(reversed(sorted(l+j[1:])))
      else:
        k+=l[0]
        l=list(reversed(sorted(l[1:]+j)))
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=l[0]
      l=l[1:]
print(k)

Fixed submission:
import heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)