Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  h+=list(map(lambda x:-x, c[d]))
  heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Runtime Error):
import heapq

n,m=map(int,input().split())
mm=10*5+1
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  h+=list(map(lambda x:-x, c[d]))+[0]
  heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Runtime Error):
import heapq

n,m=map(int,input().split())
mm=10*5+1
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  h+=list(map(lambda x:-x, c[d]))
  heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
from collections import deque

n,m=map(int,input().split())
dic=dd(list)
for i in range(1,m+1):
    dic[i]=[0]


for i in range(n):
    a,b=map(int,input().split())
    dic[a].append(b)


for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
max2nd=[0]
for i in reversed(range(m)):
    d=m-i
    a=dic[d][0]
    max2nd=deque(sorted(max2nd,reverse=True))
    b=max2nd[0]
    if a>=b:
        ans+=dic[d].popleft()
        if len(dic[d])==0: dic[d]=[0];
    else:
        ans+=max2nd.popleft()
        if len(max2nd)==0: max2nd=[0];
    max2nd+=dic[d]
        

print(ans)


Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
from collections import deque

n,m=map(int,input().split())
dic=dd(list)
for i in range(1,m+1):
    dic[i]=[0]


for i in range(n):
    a,b=map(int,input().split())
    dic[a].append(b)


for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
max2nd=[0]
for i in reversed(range(m)):
    d=m-i
    a=dic[d][0]
    max2nd=deque(sorted(max2nd,reverse=True))
    b=max2nd[0]
    if a>=b:
        ans+=dic[d].popleft()
        if len(dic[d])==0: dic[d]=[0];
    else:
        ans+=max2nd.popleft()
        if len(max2nd)==0: max2nd=[0];
    max2nd+=dic[d]
        

print(ans)


Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
from collections import deque

n,m=map(int,input().split())
dic=dd(list)
for i in range(1,m+1):
    dic[i]=[0]*m

for i in range(n):
    a,b=map(int,input().split())
    dic[a].append(b)


for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
max2nd=[0]*m
for i in reversed(range(m)):
    d=m-i
    a=dic[d][0]
    max2nd=deque(sorted(max2nd,reverse=True))
    b=max2nd[0]
    if a>=b:
        ans+=dic[d].popleft()
    else:
        ans+=max2nd.popleft()
    max2nd+=dic[d]
        

print(ans)


Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict as dd
from collections import deque

n,m=map(int,input().split())
dic=dd(list)
for i in range(n):
    a,b=map(int,input().split())
    dic[m-a].append(b)

for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
for i in range(m):
    tv=0
    j=i
    tk=-1
    while i<=j<=m:
        if len(dic[j])==0: j+=1; continue;
        if tv<dic[j][0]:
            tv=dic[j][0]
            tk=j
        j+=1
    if tk!=-1:
        dic[tk].popleft()
        ans+=tv

print(ans)

Fixed submission:
import heapq

n,m=map(int,input().split())
mm=10**5+5
c=[[0] for i in range(mm)]

for i in range(n):
  a,b=map(int,input().split())
  c[a].append(b)

#print(c)

ans=0
h=[]
for i in reversed(range(m)):
  d=m-i
  for j in c[d]:
    heapq.heappush(h,-j)
  #heapq.heapify(h)
  ans+=-heapq.heappop(h)

print(ans)