Problem:
 Score : 400 points    Problem Statement There are N one-off jobs available. If you take the i -th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.  You can take and complete at most one of these jobs in a day.  However, you cannot retake a job that you have already done.  Find the maximum total reward that you can earn no later than M days from today.  You can already start working today.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq M \leq 10^5  1 \leq A_i \leq 10^5  1 \leq B_i \leq 10^4         Input Input is given from Standard Input in the following format:  N  M  A_1  B_1  A_2  B_2  \vdots  A_N  B_N       Output Print the maximum total reward that you can earn no later than M days from today.        Sample Input 1 3 4
4 3
4 1
2 2      Sample Output 1 5  You can earn the total reward of 5 by taking the jobs as follows:   Take and complete the first job today. You will earn the reward of 3 after four days from today.  Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.        Sample Input 2 5 3
1 2
1 3
1 4
2 1
2 3      Sample Output 2 10       Sample Input 3 1 1
2 1      Sample Output 3 0

Solution (Python):
jobs, limit = map(int, input().split())
fees = [[] for _ in range(limit)]
for _ in range(jobs):
    day, fee = map(int, input().split())
    if day - 1 < limit:
        fees[day - 1] += [fee]
        
import heapq  

res = 0
array = []
for fee_list in fees:
    [heapq.heappush(array, -f) for f in fee_list]
    if len(array) > 0:
        res += -heapq.heappop(array)
print(res)

Solution (Python):
from heapq import heappush, heappop
n,m = map(int,input().split())
work_dict = {}
for i in range(n):
    a,b = map(int,input().split())
    if not a in work_dict.keys():
        work_dict[a] = []
    work_dict[a].append(b)
hq = []
ans = 0
for i in range(1,m+1):
    if i in work_dict.keys():
        for j in range(len(work_dict[i])):
            heappush(hq, -work_dict[i][j])
    if len(hq) > 0:
        work = heappop(hq)
        ans += -work
print(ans)

Solution (Python):
import heapq
n, m = map(int, input().split())
work = [0]*n
for i in range(n):
  work[i] = list(map(int, input().split()))

work.sort()
k = 0
workable = []
heapq.heapify(workable)
ans = 0
for i in range(m):
  while k < n:
    if work[k][0] <= i+1:
      heapq.heappush(workable, -work[k][1])
      k += 1
    else:
      break
  if workable:
    ans += -heapq.heappop(workable)
print(ans)

Solution (Python):
n,m = map(int,input().split())
ab = [[] for _ in range(10**5)]
for i in range(n):
	a,b = map(int,input().split())
	a -= 1
	ab[a].append(b)

import heapq

q = []
heapq.heapify(q)

ans = 0
for i in range(m):
	if len(ab[i]) != 0:
		for j in range(len(ab[i])):
			heapq.heappush(q,-1*ab[i][j])
	if len(q) != 0:
		ans += -1 * heapq.heappop(q)

print(ans)

Solution (Python):
import sys
from heapq import heapify, heappop, heappush
import bisect

N, M = map(int, input().split())
AB = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(N)])
A = [x for x, y in AB]

hq = []
heapify(hq)

count = 0

for i in range(1, M + 1):
    c_1 = bisect.bisect_left(A, i)
    c_2 = bisect.bisect_right(A, i)
    if c_2 - c_1 != 0:
        for j in range(c_1, c_2):
            heappush(hq, -AB[j][1])
    
    if hq:
        count += -heappop(hq)
    
print(count)

Solution (Python):
N, M = map(int, input().split())
jobs = sorted([list(map(int, input().split())) for i in range(N)], reverse=True)

from heapq import *
q = []
heapify(q)
ans = 0
for i in range(1, M+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -1*jobs.pop()[1])
    if q:
        ans += -1 * heappop(q)
print(ans)

Solution (Python):
from heapq import *
n, m = map(int, input().split())
a = sorted([list(map(int, input().split()))for _ in range(n)])
a.append([10**9])
r,j,x=0,0,[]
heapify(x)
for i in range(m):
    while a[j][0]<=i+1:heappush(x,-a[j][1]);j+=1
    if x:r-=heappop(x)
print(r)

Solution (Python):
import heapq
n, m = map(int, input().split())
a = sorted([list(map(int, input().split())) for _ in range(n)])
r,j,x=0,0,[]
heapq.heapify(x)
for i in range(m):
    while j < n and a[j][0] <= i+1:
        heapq.heappush(x, -a[j][1])
        j += 1
    if x:
        r -= heapq.heappop(x)
print(r)

Solution (Python):
import heapq
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
result, j = 0, 0
x = []
heapq.heapify(x)
for i in range(1, m+1):
    while j < n and ab[j][0] <= i:
        heapq.heappush(x, -ab[j][1])
        j += 1
    if len(x)!=0:
        result -= heapq.heappop(x)
print(result)

Solution (Python):

"""
Created on Sun Apr 12 02:06:07 2020

@author: Kanaru Sato
"""

import heapq

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

for i in range(n):
    a,b = list(map(int, input().split()))
    if a <= m:
        albeit[a].append(-1*b)


Heap = [0]
sum = 0
for i in range(m):
    day = i+1
    for money in albeit[day]:
        heapq.heappush(Heap, money)
    if Heap[0] != 0:
        daymax = heapq.heappop(Heap)*-1
        sum += daymax

print(sum)

Solution (Python):



from math import *
from copy import *
from heapq import *
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize
from operator import *				
from itertools import *
from collections import Counter		
from collections import defaultdict 

'''
3 4
4 3
4 1
2 2
'''

def solve(l,m):
	n = len(l)
	L = [[] for i in range(10**5+1)]
	for i in l:
		d,r = i
		L[d].append(r)
	ans = 0
	hp = []
	for i in range(1,m+1):
		for j in L[i]:
			heappush(hp,-j)
		if(hp):
			ans -= heappop(hp)
	return ans

N,M = list(map(int, stdin.readline().rstrip().split()))
l = []
for _ in range(N):
	a,b = list(map(int, stdin.readline().rstrip().split()))
	l.append([a,b])
ans = solve(l,M)
print(ans)

Solution (Python):

from collections import defaultdict
import heapq


def submit():
    n, m = map(int, input().split())
    jobs = defaultdict(list)
    job_q = []

    
    for _ in range(n):
        a, b = map(int, input().split())
        heapq.heappush(jobs[a], -b)

    
    ans = 0
    for i in range(1, m + 1):
        
        
        if jobs[i]: 
            heapq.heappush(job_q, (heapq.heappop(jobs[i]), i))
        
        
        if job_q:
            pay, day = heapq.heappop(job_q)
            ans -= pay
            if jobs[day]: 
                heapq.heappush(job_q, (heapq.heappop(jobs[day]), day))
    print(ans)
            
            
submit()

Solution (Python):
from heapq import heappop, heappush

n, nDays = map(int, input().split())
a = sorted(tuple(map(int, input().split())) for _ in range(n))
ret = 0
pq = []
pos = 0
for day in range(nDays + 1):
    while pos < n and a[pos][0] <= day:
        heappush(pq, -a[pos][1])
        pos += 1
    if pq:
        ret -= heappop(pq)
print(ret)

Solution (Python):
import heapq
from heapq import heappush, heappop

n,m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
l = [[] for i in range(10**5)]

for a,b in ab:
    l[a-1].append(-b)
heap = []

ans = 0
for i in range(m):
    for j in l[i]:
        heappush(heap,j)
    if len(heap) == 0:
        continue
    ans += -(heappop(heap))
    
    
print(ans)

Solution (Python):
import heapq
N,M = map(int,input().split())
work = [[] for i in range(100001)]
for i in range(N):
    A,B = map(int,input().split())
    work[A].append(B)
q = []
ans = 0
heapq.heapify(q)
for m in range(1,M+1):
    

    for b in work[m]:
        heapq.heappush(q,-b)
    if len(q) != 0:
        s = heapq.heappop(q)
        
        ans += -s
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
ab = sorted([list(map(int, input().split())) for _ in range(n)])

i = 0
q = []
ret = 0
for d in range(1, m + 1):
    while i < len(ab) and ab[i][0] <= d:
        heapq.heappush(q, -ab[i][1])
        i += 1
    if len(q) > 0:
        ret -= heapq.heappop(q)
print(ret)

Solution (Python):
n,m=map(int,input().split())

lists=[[] for _ in range(m+1)]


for _ in range(n):
    a,b=map(int,input().split())
    
    if 1<=a<=m:
        lists[a].append(b)
    else:
        pass
    
import heapq


heap=[]
heapq.heapify(heap)

ans=0

for i in range(1,m+1):
    for some in lists[i]:
        
        heapq.heappush(heap,-some)
    if heap:
        x=heapq.heappop(heap)
        x=x*(-1)
        ans+=x
        
print(ans)

Solution (Python):
n,m=map(int,input().split())

lists=[[] for _ in range(m+1)]


for _ in range(n):
    a,b=map(int,input().split())
    
    if 1<=a<=m:
        lists[a].append(b)
    else:
        pass
    
import heapq


heap=[]
heapq.heapify(heap)

ans=0

for i in range(1,m+1):
    for some in lists[i]:
        
        heapq.heappush(heap,-some)
    if heap:
        x=heapq.heappop(heap)
        x=x*(-1)
        ans+=x
        
print(ans)

Solution (Python):
from heapq import *
import math
import io
import sys

n, m = map(int, input().split())
pay = []
for _ in range(n):
    a, b = map(int, input().split())
    pay.append((a, b))
pay = sorted(pay, reverse=True)
q = []
ans = 0
for i in range(1, m+1):
    while pay and pay[-1][0] <= i:
        a, b = pay.pop()
        heappush(q, -b)
    if q:
        b = -heappop(q)
        ans += b
print(ans)

Solution (Python):
from heapq import heappop,heappush
N,M=map(int,input().split())
X=sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x: x[0])
hq=[]
ans,j=0,0
for i in range(1,M+1):
    while(j<N) and(X[j][0]<=i):
        heappush(hq,-X[j][1])
        j+=1
    if len(hq):
        ans+=-heappop(hq)

print(ans)

Solution (Python):
import heapq

N,M = map(int,input().split())
A = []
for i in range(N):
    a,b = map(int,input().split())
    A.append([a,b])

A.sort()
K = []
heapq.heapify(K)

j = 0
ans = 0
for i in range(1,M+1):
    if j == N:
        for i in range(M+1-i):
            ans += heapq.heappop(K)*(-1)
        break
    while(A[j][0] <= i):
        heapq.heappush(K, -A[j][1])
        j += 1
        if j == N:
            break
    if len(K) >= 1:
        ans += heapq.heappop(K)*(-1)
print(ans)

Solution (Python):
n,m=map(int,input().split())
d=[[]for i in range(m+1)]
from heapq import heappush,heappop,heapify
q=[]
for i in range(n):
    a,b=map(int,input().split())
    if a<=m:
        d[-a-1].append(b)
ans=0
for i in d[::-1]:
    for j in i:
        heappush(q,-j)
    if q:
        ans+=-heappop(q)
print(ans)

Solution (Python):


import heapq

n, m = map(int, input().split())

work = [[] for _ in range(10**5+1)]
for i in range(n):
    ta, tb = map(int, input().split())
    work[ta].append(tb)

total = 0
cand = []

for d in range(1, m+1):
    for w in work[d]:
        
        
        heapq.heappush(cand, -w) 
        
    if len(cand) > 0:
        
        total += heapq.heappop(cand)*(-1) 
        
print(total)

Solution (Python):
import heapq

n, m = map(int, input().split())
work = [[] for _ in range(10**5 + 1)]
for i in range(n):
    a, b = map(int, input().split())
    work[a].append(b)

total = 0
candidates = []
for d in range(1, m+1):
    for w in work[d]:
        heapq.heappush(candidates, -w)
    if len(candidates) > 0:
        total += -heapq.heappop(candidates)
print(total)

Solution (Python):
from heapq import heappop,heappush
n,m = map(int,input().split())
lst = [[int(i) for i in input().split()] for _ in range(n)]
PQ = []
idx = 0
ans = 0
lst.sort(key = lambda x:x[0])
for i in range(1,m+1):
    while idx < n and lst[idx][0] <= i:
        heappush(PQ,-(lst[idx][1]))
        idx += 1
    if PQ: ans -= heappop(PQ)
print(ans)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,m=LI()
  l=[LI() for _ in range(n)]
  l.sort(reverse=True)

  q=[]
  i=1
  ans=0
  while True:
    while True:
      if len(l)>0:
        if l[-1][0]<=i:
          a,b=l.pop()
          heapq.heappush(q,-b)
        else:
          break
      else:
        break
    if len(q)>0:
      c=heapq.heappop(q)
      ans-=c
    i+=1

    if i>m:
      break

  return ans


print(main())

Solution (Python):

n,m = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(n)]
ab.sort()


from collections import deque

ab = deque(ab)
from heapq import heappush, heappop, heapify
hq = []






jobs = []

ret = 0
for mi in range(1,m+1):
    while len(ab)>0 and ab[0][0]<=mi:
        a,b = ab.popleft()
        heappush(jobs,[-b,a])
    if len(jobs)>0:
        ret+=heappop(jobs)[0]
print(-ret)

Solution (Python):
N,M = map(int, input().split())



tasks = [[] for _ in range(M)]
for _ in range(N):
    A,B = map(int, input().split())
    if A <= M:
        tasks[M - A].append(B)

from heapq import *

que = []
for day in range(M):
    for task in tasks[day]:
        if len(que) < day + 1:
            heappush(que, task)
        elif len(que) == day + 1:
            heappushpop(que, task)

print(sum(que))

Solution (Python):
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
jobs = [[] for _ in range(M+1)]
for _ in range(N):
    a, b = lr()
    if a > M:
        continue
    jobs[a].append(b)

answer = 0
candidate = []
for i in range(1, M+1):
    for job in jobs[i]:
        heappush(candidate, -job)
    if candidate:
        answer += heappop(candidate)

answer *= -1
print(answer)

Solution (Python):
import sys
from heapq import heapify, heappop, heappush

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
jobs = [[] for _ in range(M+1)]
for _ in range(N):
    a, b = lr()
    if a > M:
        continue
    jobs[a].append(b)

answer = 0
candidate = []
for i in range(1, M+1):
    for job in jobs[i]:
        heappush(candidate, -job)
    if candidate:
        answer += heappop(candidate)

answer *= -1
print(answer)

Solution (Python):
import heapq

N, M = map(int, input().split(" "))
work_que = []
can_work = []
ans = 0
for i in range(N):
    a, b = map(int, input().split(" "))
    heapq.heappush(work_que, (a, -b))

for i in range(M, -1, -1):
    while len(work_que) > 0 and work_que[0][0] + i <= M:
        top_work = heapq.heappop(work_que)
        heapq.heappush(can_work, top_work[1])
    
    if len(can_work) > 0:
        ans += -heapq.heappop(can_work)
print(ans)

Solution (Python):
mod = 10 ** 9 + 7
from collections import deque
import heapq


def main():
    N, M = iip(False)
    A = []
    for i in range(N):
        A.append(iip(True))

    ret = solve(N, M, A)
    print(-ret)


def solve(N, M, A):
    A = deque(sorted(A, key=lambda x: x[0], reverse=False))

    jobs = []
    heapq.heapify(jobs)

    ret = 0
    for day in range(M):
        nokori = day + 1

        dn = 0
        
        
        for job in A:
            if job[0] == nokori:
                heapq.heappush(jobs, -job[1])
                dn += 1
            else:
                break
        for i in range(dn):
            A.popleft()

        
        if jobs:
            
            ret += heapq.heappop(jobs)

        
        
        

    return ret




def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))

def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r-l > 1:
        x = (l+r)//2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l

def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p - 1)) % mod


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
from heapq import heapify, heappop, heappush

def main():
    N, M = input_nums()
    jobs = {}
    for i in range(1, 10**5+1):
        jobs[i] = [0]
    for i in range(N):
        A, B = input_nums()
        jobs[A].append(B)
    res = 0
    hq = [0]
    heapify(hq)
    for i in range(1, M+1):
        for c in jobs[i]:
            heappush(hq, -c)
        if not hq:
            continue
        res -= heappop(hq)
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq
n,m=map(int,input().split())
work=[[] for _ in range(m)]
for _ in range(n):
    a,b=map(int,input().split())
    if a<=m:
        work[a-1].append(b)
ans=0
q=[]
heapq.heapify(q)
for i in range(m):
    for x in work[i]:
        heapq.heappush(q,-x)
    if q!=[]:
        ans+=heapq.heappop(q)*(-1)
print(ans)

Solution (Python):
n,m = map(int,input().split())
db = []

for i in range(n):
    a,b = map(int,input().split())
    if(a<=m):
        db.append([m-a,-1*b])

db.sort(reverse=True)


import heapq
a=[]
heapq.heapify(a)

ans = 0
pnt = 0
day = m-1
while(day>=0):
    if(pnt < len(db)):
        while(db[pnt][0] == day):
            heapq.heappush(a,db[pnt][1])
            pnt += 1
            if(pnt == len(db)):
                break
    day -= 1

    if(len(a) > 0):
        ans -= heapq.heappop(a)

print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    n,m = map(int, input().split())
    
    cost = [[] for _ in range(10**5+10)]
    for _ in range(n):
        a,b = map(int, input().split())
        cost[a].append(b)

    res = 0
    hq = [0]
    heapq.heapify(hq)
    for i in range(1, m+1):
        for c in cost[i]:
            heapq.heappush(hq, -c)
        if not hq:
            continue
        res -= heapq.heappop(hq)
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    n,m = map(int, input().split())
    
    cost = [[] for _ in range(10**5+10)]
    for _ in range(n):
        a,b = map(int, input().split())
        cost[a].append(b)

    res = 0
    hq = [0]
    heapq.heapify(hq)
    for i in range(1, m+1):
        for c in cost[i]:
            heapq.heappush(hq, -c)
        if not hq:
            continue
        res -= heapq.heappop(hq)
    print(res)
            





if __name__ == '__main__':
    main()

Solution (Python):
import heapq

N, M = map(int, input().split())
work = [[] for _ in range(M)]
for _ in range(N):
    a, b = map(int, input().split())
    if a <= M:
        work[a-1].append(b)
ans = 0
q = []
heapq.heapify(q)
for i in range(M):
    for x in work[i]:
        heapq.heappush(q, -x)
    if q != []:
        ans += heapq.heappop(q)*(-1)
print (ans)

Solution (Python):


import heapq

def main():
    n, m = map(int, input().split())
    ab = []
    for i in range(n):
        a, b = map(int, input().split())
        ab.append((a, b))
    ab.sort(key=lambda x: x[0])
    doable = []
    next = 0
    res = 0
    for dt in range(m + 1):
        while next < n:
            if ab[next][0] <= dt:
                a, b = ab[next]
                heapq.heappush(doable, (-b, a))
                next += 1
            else:
                break
        if doable:
            mb, a = heapq.heappop(doable)
            b = -mb
            res += b
    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

import bisect





from operator import attrgetter, itemgetter





readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

import heapq

class Reverse:
    def __init__(self, val):
        self.val = val

    def __lt__(self, other):
        return self.val > other.val

    def __repr__(self):
        return repr(self.val)


class PriorityQueue:

    def __init__(self, x=[], desc=False):
        if desc:
            for i in range(len(x)):
                x[i] = Reverse(x[i])
        self._desc = desc
        self._container = x
        heapq.heapify(self._container)

    @property
    def is_empty(self):
        return not self._container

    def pop(self):
        if self._desc:
            return heapq.heappop(self._container).val
        else:
            return heapq.heappop(self._container)

    def push(self, item):
        if self._desc:
            heapq.heappush(self._container, Reverse(item))
        else:
            heapq.heappush(self._container, item)

    def top(self):
        if self._desc:
            return self._container[0].val
        else:
            return self._container[0]

    def sum(self):
        return sum(self._container)

    def __len__(self):
        return len(self._container)


def main():
    n, m = list(map(int, readline().split()))

    ab = [list(map(int, readline().split())) for _ in range(n)]

    ab.sort(key=itemgetter(0))
    a = [ab[i][0] for i in range(n)]
    b = [ab[i][1] for i in range(n)]
    pq = PriorityQueue(desc=True)
    ans = 0

    idxprev = 0

    for day in range(1, m + 1):
        idxcur = bisect.bisect_right(a, day)
        if idxcur > idxprev:
            for i in range(idxprev, idxcur):
                pq.push(b[i])
                idxprev = idxcur
        if pq:
            ans += pq.pop()
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq

n, m = map(int, input().split())

w_list = []
pq = []

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

ptr = 0
ret = 0
for i in range(m):
    while True:
        if ptr < n and w_list[ptr][0] <= i + 1:
            heapq.heappush(pq, -w_list[ptr][1])
            ptr += 1
        else:
            break
    if pq:
        ret -= heapq.heappop(pq)

print(ret)

Solution (Python):
from heapq import heappop,heappush,heapify
n,m=map(int,input().split())
work=[[] for _ in range(m+1)]
for _ in range(n):
    a,b=map(int,input().split())
    if a<=m: work[a].append(b)

ans=0
q=[]
heapify(q)
for i in range(1,m+1):
    for x in work[i]:
        heappush(q,-x)
    if q!=[]: ans+=heappop(q)
print(-ans)

Solution (Python):

from heapq import heappop
from heapq import heappush
def main():
    n, m = map(int, input().split())
    works = [[] for _ in range(m)]
    for _ in range(n):
        a, b = map(int, input().split())
        if a > m:
            continue
        works[m-a].append(b)
    s = 0
    h = [] 
    for i in reversed(range(m)):
        for b in works[i]:
            heappush(h, -b)
        if len(h) != 0:
            s += -heappop(h)
    print(s)
main()

Solution (Python):
import sys
import heapq

input=sys.stdin.readline

N,M=map(int,input().split())
job={i:[] for i in range(1,M+1)}
for i in range(0,N):
    a,b=map(int,input().split())
    if M>=a:
        job[a].append(b)

ans=0
ablejob=[]
heapq.heapify(ablejob)
for i in range(1,M+1):
    for j in job[i]:
        heapq.heappush(ablejob,-j)
    if ablejob:
        m=-heapq.heappop(ablejob)
        ans+=m

print(ans)

Solution (Python):
import heapq
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(key=lambda x:x[0])
ans=0
q=[]
heapq.heapify(q)
e=0
for i in range(1,m+1):
  while e<n and ab[e][0]==i:
    heapq.heappush(q,-ab[e][1])
    e+=1
  if len(q)>0: 
    ans-=heapq.heappop(q)
print(ans)

Solution (Python):
import heapq as hq
n,m=map(int,input().split())
J=[[] for i in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    if a<=m:
        J[a-1].append(b) 

ans=0
Q=[]
hq.heapify(Q)
for j in J: 
    for k in j:
        hq.heappush(Q,-k) 
    if Q:
        ans-=hq.heappop(Q) 
print(ans)

Solution (Python):
from heapq import heappop, heappush
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
AB = [[int(x) for x in readline().split()] for _ in range(N)]

AB.sort()

STOCK = []
ans = 0

idx = 0
for d in range(1, M+1):
    for i in range(idx, N):
        a, b = AB[i]
        if a <= d:
            heappush(STOCK, -b)
            idx += 1
        else:
            break

    if STOCK:
        p = heappop(STOCK)
        ans -= p


print(ans)

Solution (Python):
from heapq import heappop, heappush
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
AB = [[int(x) for x in readline().split()] for _ in range(N)]

AB.sort()

STOCK = []
ans = 0

idx = 0
for d in range(1, M+1):
    for i in range(idx, N):
        a, b = AB[i]
        if a <= d:
            heappush(STOCK, -b)

            if i == N - 1:
                idx = N
        else:
            idx = i
            break

    if STOCK:
        p = heappop(STOCK)
        p = -p
        ans += p


print(ans)

Solution (Python):
from heapq import *
n,m = map(int,input().split())
a = [[int(i) for i in input().split()] for _ in range(n)]
a.sort(key=lambda x:x[0])
ans = 0
PQ = []
heapify(PQ)
idx = 0
for i in range(1,m+1):
    while idx < n and a[idx][0] <= i:
        heappush(PQ,-a[idx][1])
        idx += 1
    if PQ: ans -= heappop(PQ)
print(ans)

Solution (Python):
from heapq import *
n,m = map(int,input().split())
a = [[int(i) for i in input().split()] for _ in range(n)]
a.sort(key=lambda x:x[0])
ans = 0
PQ = []
idx = 0
for i in range(1,m+1):
    while idx < n and a[idx][0] <= i:
        heappush(PQ,-a[idx][1])
        idx += 1
    if PQ: ans -= heappop(PQ)
print(ans)

Solution (Python):
from heapq import heappop, heappush
n, m, *AB = map(int, open(0).read().split())
B = [[] for _ in range(m)]
for a, b in zip(AB[::2], AB[1::2]):
    if a <= m:
        B[a-1].append(b)

ans = 0
heap = []
for b in B:
    for x in b:
        heappush(heap, -x)
    if heap:
        ans -= heappop(heap)
print(ans)

Solution (Python):
from heapq import heappop, heappush
n, m, *AB = map(int, open(0).read().split())
AB = [(a, b) for a, b in zip(AB[::2], AB[1::2]) if a <= m]
AB_ = [[] for _ in range(m)]
for a, b in AB:
    AB_[a-1].append(b)

ans = 0
heap = []
for ab in AB_:
    for x in ab:
        heappush(heap, -x)
    if heap:
        ans -= heappop(heap)
print(ans)

Solution (Python):
import bisect
import heapq
N, M = map(int, input().split())
X = [[] for _ in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    if a <= M:
        X[a-1].append(-b)
ans = 0
heap = []
for i in range(M):
    for b in X[i]:
        heapq.heappush(heap, b)
    if len(heap) > 0:
        ans -= heapq.heappop(heap)
print(ans)

def TLE():
    X.sort(key=lambda x: (-x[1], x[0]))
    days = [i for i in range(1, M+1)]
    ans = 0
    for a, b in X:
        if len(days) == 0:
            break
        if days[-1] < a:
            continue
        index = bisect.bisect_right(days, a)
        if index > 0 and days[index-1] >= a:
            days.pop(index-1)
        elif index > 0 and days[index-1] <a:
            days.pop(index) 
        elif index == 0 and days[0] >= a:
            days.pop(0)
        else:
            continue
        ans += b
    print(ans)

Solution (Python):
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
job = sorted(AB)
reward_list = []
ans = 0
j = 0
for i in range(1, M + 1):
    while j < N and job[j][0] <= i:
        heapq.heappush(reward_list, -job[j][1])
        j += 1
    if len(reward_list) > 0:
        ans -= heapq.heappop(reward_list)
print(ans)

Solution (Python):
import heapq
n, m = map(int, input().split())
work = [list(map(int, input().split())) for _ in range(n)]
work.sort()

cd = []
heapq.heapify(cd)
cnt = 0
ans = 0


for k in range(m+1):
    

    for i in range(cnt, n):
        a, b = work[i]

        if a == k:
            heapq.heappush(cd, -b)
        else:
            cnt = i
            break

        if i == n-1:
            cnt = n-1

    if not cd:
        continue
    ans -= heapq.heappop(cd)
print(ans)

Solution (Python):
import heapq
n, m = map(int, input().split())
work = [list(map(int, input().split())) for _ in range(n)]
work.sort()

cd = []
heapq.heapify(cd)
cnt = 0
ans = 0


for k in range(m+1):
    

    for i in range(cnt, n):
        a, b = work[i]

        if a == k:
            heapq.heappush(cd, [-b, a])
        else:
            cnt = i
            break

        if i == n-1:
            cnt = n-1

    if not cd:
        continue
    ans -= heapq.heappop(cd)[0]
print(ans)

Solution (Python):
import heapq
N,M = map(int,input().split())
AB = sorted([list(map(int,input().split())) for _ in range(N)])

option = []
ans = 0
j = 0
for i in range(1,M+1):
    while j != N and AB[j][0] <= i: 
        heapq.heappush(option,-AB[j][1])
        j += 1
    if len(option) != 0:
        ans -= heapq.heappop(option)
print(ans)

Solution (Python):
import heapq

N, M = map(int, input().split())
AB = [[0] * 2] * N
for i in range(N):
	AB[i] = list(map(int, input().split()))
AB.sort()

x = []
ans, j = 0, 0
for i in range(1, M + 1):
	while j != N and AB[j][0] <= i:
		heapq.heappush(x, -AB[j][1])
		j += 1
	if len(x) != 0:
		ans -= heapq.heappop(x)

print(ans)