Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys
import heapq

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(-bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur: cur= ref
        else: cur+= ref
        heapq.heapify(cur)
    #
    if not cur: continue
    tot+= heapq.heappop(cur)
#
print( -tot )


Fixed submission:
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
for i in range(m):
    if baito[i]: baito[i].sort()
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur:
            cur= ref
        else:
            if ref[-1]<=cur[0]:
                cur= ref + cur
            elif cur[-1]<=ref[0]:
                cur= cur + ref
            else:
                cur+= ref
                cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys
import heapq

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(-bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur: cur= ref
        else: cur+= ref
    #
    if not cur: continue
    heapq.heapify(cur)
    tot+= heapq.heappop(cur)
#
print( -tot )


Fixed submission:
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
for i in range(m):
    if baito[i]: baito[i].sort()
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur:
            cur= ref
        else:
            if ref[-1]<=cur[0]:
                cur= ref + cur
            elif cur[-1]<=ref[0]:
                cur= cur + ref
            else:
                cur+= ref
                cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        cur+= ref
        cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )


Fixed submission:
#!/usr/bin/env python

import sys

n,m= [ int(x) for x in sys.stdin.readline().split() ]
baito=[ [] for _ in range(m) ]
for _ in range(n):
    ai,bi = [ int(x) for x in sys.stdin.readline().split() ]
    ai-=1
    if ai<m: baito[ai].append(bi)
#
for i in range(m):
    if baito[i]: baito[i].sort()
#
tot=0
cur=[]
for i in range(m):
    ref= baito[i]
    if ref:
        if not cur:
            cur= ref
        else:
            if ref[-1]<=cur[0]:
                cur= ref + cur
            elif cur[-1]<=ref[0]:
                cur= cur + ref
            else:
                cur+= ref
                cur.sort()
    #
    if not cur: continue
    tot+= cur.pop()
#
print( tot )