Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
# /usr/bin/python3


def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      top.sort()
      if len(top) > a-k:
        s += sum(top[-(a-k):])
        del top[-(a-k):]
      else:
        s += sum(top)
        top = []
      k = a

    top.append(b)


  top.sort()
  s += sum(top[-(m-k+1):])
  print(s)

if __name__=='__main__':
  main()


Fixed submission:
# -*- coding:utf-8 -*-
# /usr/bin/python3

from heapq import heappush, heappop

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      s += (-sum([heappop(top) for _ in range(a-k) if len(top) != 0]))
      k = a
    heappush(top, -b)

  s += (-sum([heappop(top) for _ in range(m-k+1) if len(top) != 0]))
  print(s)

if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
# /usr/bin/python3


def main():
  n, m = map(int, input().strip().split(' '))
  liAB = sorted([tuple(map(int, input().strip().split(' '))) for _ in range(n)], key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      top.sort()
      if len(top) > a-k:
        s += sum(top[-(a-k):])
        del top[-(a-k):]
      else:
        s += sum(top)
        top = []
      k = a

    top.append(b)


  top.sort()
  s += sum(top[-(m-k+1):])
  print(s)

if __name__=='__main__':
  main()


Fixed submission:
# -*- coding:utf-8 -*-
# /usr/bin/python3

from heapq import heappush, heappop

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      s += (-sum([heappop(top) for _ in range(a-k) if len(top) != 0]))
      k = a
    heappush(top, -b)

  s += (-sum([heappop(top) for _ in range(m-k+1) if len(top) != 0]))
  print(s)

if __name__=='__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding:utf-8 -*-
# /usr/bin/python3

from bisect import insort

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = sorted([tuple(map(int, input().strip().split(' '))) for _ in range(n)], key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      if len(top) > a-k:
        s += sum(top[-(a-k):])
        del top[-(a-k):]
      else:
        s += sum(top)
        top = []
      k = a

    insort(top, b)


  s += sum(top[-(m-k+1):])

  print(s)
  
  pass

if __name__=='__main__':
  main()


Fixed submission:
# -*- coding:utf-8 -*-
# /usr/bin/python3

from heapq import heappush, heappop

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      s += (-sum([heappop(top) for _ in range(a-k) if len(top) != 0]))
      k = a
    heappush(top, -b)

  s += (-sum([heappop(top) for _ in range(m-k+1) if len(top) != 0]))
  print(s)

if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-
# /usr/bin/python3

from bisect import insort
from collections import deque

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = sorted([tuple(map(int, input().strip().split(' '))) for _ in range(n)], key=lambda x:x[0])

  k = 1
  top = deque()
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      for i in range(len(top)):
        if i < a-k:
          s += top[-1]
          del top[-1]
      k = a

    insort(top, b)


  s += top[-1]
  for i in range(m - k):
    s += top[-(i+1)]

  print(s)
  
  pass

if __name__=='__main__':
  main()


Fixed submission:
# -*- coding:utf-8 -*-
# /usr/bin/python3

from heapq import heappush, heappop

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      s += (-sum([heappop(top) for _ in range(a-k) if len(top) != 0]))
      k = a
    heappush(top, -b)

  s += (-sum([heappop(top) for _ in range(m-k+1) if len(top) != 0]))
  print(s)

if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-
# /usr/bin/python3

from collections import defaultdict
from bisect import insort
from collections import deque

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = sorted([tuple(map(int, input().strip().split(' '))) for _ in range(n)], 
                key=lambda x:x[0])

  k = 1
  top = deque()
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      for i in range(len(top)):
        if i < a-k:
          s += top[-1]
          del top[-1]
      k = a

    insort(top, b)


  s += top[-1]
  for i in range(m - k):
    s += top[-(i+1)]

  print(s)
  
  pass

if __name__=='__main__':
  main()



Fixed submission:
# -*- coding:utf-8 -*-
# /usr/bin/python3

from heapq import heappush, heappop

def main():
  n, m = map(int, input().strip().split(' '))
  liAB = [tuple(map(int, input().strip().split(' '))) for _ in range(n)]
  liAB.sort(key=lambda x:x[0])

  k = 1
  top = []
  s = 0
  for a, b in liAB:
    if a >= m+1:
      break

    if k < a:
      s += (-sum([heappop(top) for _ in range(a-k) if len(top) != 0]))
      k = a
    heappush(top, -b)

  s += (-sum([heappop(top) for _ in range(m-k+1) if len(top) != 0]))
  print(s)

if __name__=='__main__':
  main()