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):
import heapq

n, m = map(int, input().split())
lst = [[] for i in range(m)]
for i in range(n):
  a, b = map(int, input().split())
  if a <= m:
    lst[a-1].append(b)
q = []
res = 0
for i in range(m):
  while lst[i]:
    b = lst[i].pop()
    heapq.heappush(q, -b)
  if len(q) > 0:
    b = heapq.heappop(q)
    b = -b
    res += b
print(res)

Solution (Python):
from heapq import heappop, heappush

def getlist():
	return list(map(int, input().split()))

N, M = getlist()
task = []
for i in range(N):
	A, B = getlist()
	heappush(task, [A, B])

Q = []
ans = 0
for i in range(M):
	while True:
		if task != [] and task[0][0] ==1 + i:
			a, b = heappop(task)
			heappush(Q, [-b, a])
		else:
			break
	if Q != []:
		x, y = heappop(Q)
		ans += -x


print(ans)

Solution (Python):
import heapq
from collections import defaultdict



def main():
    N,M = [int(n) for n in input().split()]
    
    jobs = defaultdict(list)
    v = 0
    for i in range(N):
        a,b = [int(n) for n in input().split()]
        if a <= M:
            jobs[a].append(-b)
    cand = []
    ans = 0
    for i in range(1,M+1):
        for e in jobs[i]:heapq.heappush(cand,e) 
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from sys import exit, stderr, stdin
input = stdin.readline
import heapq



def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N,M = [int(n) for n in input().split()]
    
    jobs = [[]]*N
    v = 0
    for i in range(N):
        jobs[i] = [int(n) for n in input().split()]
    
    jobs = sorted(jobs,key=lambda x:-x[1])
    jobs = sorted(jobs,key=lambda x:x[0])
    cur = 0
    ans = 0
    cand = []
    for i in range(1,M+1):
        while(cur < N and i >= jobs[cur][0]):
            heapq.heappush(cand,-jobs[cur][1])
            cur += 1
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappop, heappush
from collections import defaultdict
N, M = map(int, input().split())
H = []
D = defaultdict(list)
for _ in range(N):
    a, b = map(int, input().split())
    if a > M:
        continue
    else:
        D[a].append(b)

ans = 0
for a in range(1, M+1):
    for b in D[a]:
        heappush(H, -b)
    if H:
        ans -= heappop(H)

print(ans)

Solution (Python):
import heapq
import sys
input = sys.stdin.readline

def main():
    N,M = map(int,input().split())
    paylist = [[] for _ in range(M)]
    for _ in range(N):
        a,b = map(int,input().split())
        if M >= a:
            heapq.heappush(paylist[a-1],-b)
    work = []
    payment = 0
    for i in range(M):
        for j in paylist[i]:
            heapq.heappush(work,j)
        if work:
            payment += heapq.heappop(work)

    print(-payment)

if __name__ == "__main__":
    main()

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

Solution (Python):

import heapq
N,M = map(int,input().split())
AB = []
for i in range(N):
    ab = list(map(int,input().split()))
    heapq.heappush(AB,ab)
    
ans = 0
cand = []
for i in range(M):
    while AB:
        ab = heapq.heappop(AB)
        if ab[0] <= (i+1):
            heapq.heappush(cand,10**5-ab[1])
        else:
            heapq.heappush(AB,ab)
            break
    
    if len(cand) >= 1:
        cd = heapq.heappop(cand)
        ans+=(10**5-cd)
    
print(ans)

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

for i in range(N):
    d, r = list(map(int, input().split()))
    if d-1 < M:
        jobs[d-1].append(-r)
rewords = 0
heap = []
for i in range(M):
    for reword in jobs[i]:
        heapq.heappush(heap, reword)
    if len(heap) >0:
        reword = heapq.heappop(heap)
        rewords += -reword

print(rewords)

Solution (Python):
import heapq

N, M = map(int, input().split())


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

ans = 0
q = [] 


for i in range(M):
    for b in Blist[i]: 
        heapq.heappush(q, b)
    if len(q) > 0: 
        ans -= heapq.heappop(q)

print(ans)

Solution (Python):
import heapq
 
N, M = map(int, input().strip().split())
 
A = {}
 
for i in range(N):
  a, b = map(int, input().strip().split())
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = [] 
for i in range(1, M + 1): 
  if i in A: 
    for a in A[i]:
      heapq.heappush(days, -a) 
  if len(days) > 0:
    total += - heapq.heappop(days) 
                                   
 
print(total)

Solution (Python):
from collections import defaultdict
from heapq import heappush, heappop
n, m = map(int, input().split())
baito_dict = defaultdict(list)
for _ in range(n):
    day, weight = map(int, input().split())
    baito_dict[day].append(weight)


max = 0

available_works = []

for day in range(1, m+1):
    for weight in baito_dict[day]:
        heappush(available_works, -weight)
    if available_works:
        max += heappop(available_works)

print(-max)

Solution (Python):
from heapq import heappush, heappop
N,M = map(int, input().split())
a = [[int(i) for i in input().split()] for i in range(N)]

a = sorted(a, key=lambda x:(x[0]))

hq = []
ans = 0
j = 0
for i in range(1,M+1):
	if j < N:
		while a[j][0] == i:
			heappush(hq, -a[j][1])
			j += 1
			if j == N:
				break
	if hq:
	 	ans -= heappop(hq)
    
print(ans)

Solution (Python):
from heapq import heappush, heappop
N,M = map(int, input().split())
a = [[int(i) for i in input().split()] for i in range(N)]

a = sorted(a, key=lambda x:(x[0]))

hq = []
ans = 0
j = 0
for i in range(1,M+1):
	if j < N:
		while a[j][0] == i:
			heappush(hq, -a[j][1])
			j += 1
			if j == N:
				break
	if hq:
	 	ans -= heappop(hq)
    
print(ans)

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

Solution (Python):
from collections import defaultdict
import heapq

N, M = [int(x) for x in input().split()]

dict = defaultdict(list)
for _ in range(N):
    A, B = [int(x) for x in input().split()]
    dict[A].append(B)

h = []
total = 0
for x in range(1, M + 1):
    for y in dict[x]:
        heapq.heappush(h, -y)
    if h:
        total += heapq.heappop(h)

print(-total)

Solution (Python):
from heapq import heappush, heappop
from collections import deque
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for i in range(n)]
ab = deque(sorted(ab, key=lambda x: x[0]))

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

Solution (Python):
import heapq

N, M = list(map(int,input().split()))
A = [[] for _ in range(M)]

for i in range(N):
    a, b = list(map(int,input().split()))
    if a <= M:
        A[a-1].append(-b)
    
ans = 0
cand = []

for i in range(M):
    
    for j in A[i]:
        heapq.heappush(cand, j)
        
    if cand:
        ans -= heapq.heappop(cand)
    
print(ans)

Solution (Python):
import heapq
n,m=map(int,input().split())
work=[[] for i in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    a-=1
    if a>=m:continue
    heapq.heappush(work[a],-b)

ans=0
nowwork=[]
for i in range(m):
    for j in work[i]:
        heapq.heappush(nowwork,j)
    
    if nowwork==[]:
        continue
    ans+=heapq.heappop(nowwork)
print(-ans)

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

ans = 0
hq = []
n,m = inpl()
ab = [[] for i in range(m)]
for i in range(n):
    a,b = inpl()
    if a <= m:
        ab[a-1].append(b)

heapq.heapify(hq)
for i in range(m):
    for j in ab[i]:
        heapq.heappush(hq,-j)
    if hq:
        ans -= heapq.heappop(hq)
print(ans)

Solution (Python):

import heapq
from collections import defaultdict

N, M = map(int, input().split())

a_b_dict = defaultdict(list)
for _ in range(N):
    a, b = tuple(map(int, input().split()))
    a_b_dict[a].append(b)

result = []
h = []
num_days = 0
for i in range(1, 1+M):
    for b in a_b_dict.get(i, []):
        heapq.heappush(h, -b)
    if len(h) > 0:
        result.append(-1*heapq.heappop(h))
print(sum(result))

Solution (Python):
from heapq import heappush, heappop
n,m=map(int,input().split())
works = []
for _ in range(n):
	a,b=map(int,input().split())
	if a>m:
		continue
	works.append((a,b))
works.sort(reverse=True)
q=[]
ans=0

for dayleft in range(1,m+1):
	while works and works[-1][0]<=dayleft:
		a,b=works.pop()
		heappush(q,-b)
	if q:
		b = -heappop(q)
		ans+=b
print(ans)

Solution (Python):
from heapq import heappush, heappop

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

works = []
for i in range(0, n):
    a, b = map(int, input().split())
    if a > m:
        continue
    works.append((a, b))

works.sort(reverse=True)

q = []
ans = 0

for i in range(1, m + 1):
    while works and works[-1][0] <= i:
        a, b = works.pop()
        heappush(q, -b)
    if q:
        b = -heappop(q)
        ans += b

print(ans)

Solution (Python):
import sys
import heapq
from collections import defaultdict
n, m = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])


L =[]
money = 0
for d in range(1, m+1):
    for e in dic[d]:
        heapq.heappush(L, -e)
    if L:
        money +=heapq.heappop(L)
print(-money)

Solution (Python):
from heapq import heappush,heappop
ab=[]
n,m=map(int,input().split())
for i in range(n):
	x,y=map(int,input().split())
	ab.append([x,y])
ab.sort()
k=0
ans=0
hq=[]
cnt=0
for i in range(1,m+1):
	if k>n-1:
		break
	while ab[k][0]<=i:
		heappush(hq,-ab[k][1])
		k+=1
		if k>n-1:
			break
	if len(hq)!=0:
		ans+=heappop(hq)
	cnt+=1
if cnt<m:
	for i in range(m-cnt):
		ans+=heappop(hq)
print(-ans)

Solution (Python):
import heapq as hp

n,m =[int(i) for i in input().split()]
lists = [[] for i in range(m)]

for _ in range(n):
  a,b = [int(i) for i in input().split()]
  if a<=m:
  	lists[a-1].append(b)
  
h = []
ans = 0
for day in range(m):
  for v in lists[day]:
    hp.heappush(h, -v)
  if len(h)>0:
    ans -= hp.heappop(h)
print(ans)

Solution (Python):
import sys
import heapq

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(-a_dash)
	b.append(-b_dash)

c = zip(a,b)
c = sorted(c,reverse=True)
a,b = zip(*c)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if -a[j] + i == m:
			heapq.heappush(l,b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		maximum = heapq.heappop(l)
		ans += maximum

print(-ans)

Solution (Python):
import sys
import heapq

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(-a_dash)
	b.append(-b_dash)

c = zip(a,b)
c = sorted(c,reverse=True)
a,b = zip(*c)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if -a[j] + i == m:
			heapq.heappush(l,b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		maximum = heapq.heappop(l)
		ans += maximum

print(-ans)

Solution (Python):
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)]
    heapq.heapify(ab)
    s_q = []
    heapq.heapify(s_q)
    r = 0
    for d in range(1, m + 1):
        while True:
            if len(ab) != 0:
                p = heapq.heappop(ab)
                if p[0] <= d:
                    heapq.heappush(s_q, -p[1])
                else:
                    heapq.heappush(ab, p)
                    break
            else:
                break
        if len(s_q) != 0:
            mr = -heapq.heappop(s_q)
            r += mr
        d -= 1
    print(r)


if __name__ == '__main__':
    slove()

Solution (Python):
import sys
from collections import defaultdict
from heapq import *
sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N,M = map(int, input().split())
    job_list = defaultdict(list)
    for _ in range(N):
        a, b = map(int, input().split())
        job_list[a].append(-b)

    pool = []
    ans = 0
    for i in range(1, M+1):
        for reward in job_list[i]:
            heappush(pool, reward)
        if 0 < len(pool):
            ans -= heappop(pool)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappush, heappop

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

ab.sort()
ans = 0
hq = []
idx = 0
for i in range(1, m+1):
    for j in range(idx, n):
        if ab[j][0] == i:
            heappush(hq, (-ab[j][1], ab[j][0]))
            idx += 1
        else:
            break
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)

Solution (Python):
import heapq
N, M = map(int, input().split())
reward = [[] for _ in range(M)]
for i in range(N):
  A, B = map(int, input().split())
  if A <= M:
    reward[A - 1].append(B)
q = []
ans = 0
for day in range(M):
  for r in reward[day]:
    heapq.heappush(q, -r)
  if len(q) > 0:
    ans += -heapq.heappop(q)
print(ans)

Solution (Python):
import heapq
N,M = map(int,input().split())
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l,reverse=True)
heap = []
ans = 0
for i in range(1,M+1):
        while l and l[-1][0] == i:
            heapq.heappush(heap, -(l.pop()[1]))
        if heap :
            ans -= (heapq.heappop(heap))
print(ans)

Solution (Python):
import heapq
N,M = map(int,input().split())
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l,reverse=True)
heap = []
ans = 0
for i in range(1,M+1):
        while l and l[-1][0] == i:
            heapq.heappush(heap, (l.pop()[1]*(-1)))
        if heap :
            ans -= (heapq.heappop(heap))
print(ans)

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

x = []
y = []

for i in range(n):
  day, salary = map(int, input().split())
  heapq.heappush(x, (-day,-salary))
  
while x:
  d, s = heapq.heappop(x)
  r = m-len(y)
  if r >= -d:
    heapq.heappush(y, -s)
  else:
    heapq.heappush(y, -s)
    heapq.heappop(y)

print(sum(y))

Solution (Python):
from heapq import heappush, heappop

def solve():
    N, M = map(int , input().split())
    arbeit_A_B = list(list(map(int , input().split())) for _ in range(N))
    arbeit_A_B = sorted(arbeit_A_B, key=lambda x: (x[0], -x[1]))
    
    day = 1
    result = 0
    index_arbeit = 0
    heap_q = []
    while day <= M:
        while index_arbeit < N:
            A,B = arbeit_A_B[index_arbeit]
            if A <= day:
                heappush(heap_q, (-1)*B)
            else: break
            index_arbeit += 1
            
        if heap_q:
            result -= heappop(heap_q)
        day += 1
            
    print(result)
    
solve()

Solution (Python):
import heapq
N, M = map(int,input().split())
reward = [[] for _ in range(M)]
for i in range(N):
  A,B = map(int,input().split())
  if A <= M:
    reward[A-1].append(B)
q = []
ans = 0
for day in range(M):
  for r in reward[day]:
    heapq.heappush(q,-r)
  if len(q) >0:
    ans += -heapq.heappop(q)
print(ans)

Solution (Python):
from heapq import heappush, heappop

N, M = map(int, input().split())
job = []

for i in range(N):
  a, b = map(int, input().split())
  job += [(a,b)]
job.sort(key=lambda x:x[0])
ls = []
ans = 0
j = 0
a = -1
for i in range(1,M+1):
  while a<=i and j<len(job):
    a, b = job[j]
    if a>i:
      break
    heappush(ls, -b)
    j += 1
  if ls:
    x = (-1)*heappop(ls)

    ans += x
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
ab = []
for i in range(n):
    a, b = map(int, input().split())
    if a > m:
        continue
    ab.append((a, b))
ab = sorted(ab, key=lambda a: a[0])
q = []
day = 0
index = 0
total = 0
while day <= m:
    while index < len(ab) and day >= ab[index][0]:
        heapq.heappush(q, - ab[index][1])
        index += 1
    if len(q) > 0:
        total += - q[0]
        heapq.heappop(q)
    day += 1
print(total)

Solution (Python):
import heapq

n,m=map(int,input().split())
works=[[] for i in range(m+1)]
for i in range(n):
    day,pay=map(int,input().split())
    try:
        works[day].append(pay)
    except IndexError:
        pass


heapls=[]
day=1
ans=0

for i in range(m):
    day=i+1
    for j in works[day]:
        heapq.heappush(heapls,-j)
    if heapls!=[]:
        tmp=heapq.heappop(heapls)
        ans=ans+tmp
print(-ans)

Solution (Python):
n,m = map(int,input().split())
can = []
for _ in range(n):
  can.append(list(map(int,input().split())))
can.sort()
pool = []
import heapq
here = 0
ans = 0
for i in range(1,m+1):
  while can[here][0] == i:
    heapq.heappush(pool,-can[here][1])
    here += 1
    if here == n:
      here -= 1
      break
  if pool != []:
    ans += heapq.heappop(pool)
print(-ans)

Solution (Python):
n, m = map(int,input().split())
pay = [[] for i in range(m+1)]
for i in range(n):
    a, b = map(int,input().split())
    if a <= m:
        pay[a].append(b)


import heapq as hq 
ans = 0
stack = []
for i in range(m+1):
    for j in pay[i]:
        hq.heappush(stack, -j)
    ans -= hq.heappop(stack) if stack else 0

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from heapq import heappush, heappop


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

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

for _ in range(n):
    a, b = map(int, input().split())

    if a <= m:
        m_list[a].append(b)


all_r = 0
hq = []

for i in range(1, m+1):
    for v in m_list[i]:
        heappush(hq, -1*v)

    if len(hq) != 0:
        max_v = -1 * heappop(hq)
        all_r += max_v

print(all_r)

Solution (Python):

from heapq import *
n, m = map(int, input().split())

j = []
for _ in range(n):
    a,b = map(int, input().split())
    if a <= m :
        j.append((a,b))
j.sort(reverse=True)

ans = 0
q = []
for i in range(1, m+1):
    while j and j[-1][0] <= i:
        a,b = j.pop()
        heappush(q, -b)
    if q:
        ans -= heappop(q)
print(ans)

Solution (Python):
from queue import PriorityQueue


def main():
    N, M = map(int, input().split(' '))
    jobs = [[] for _ in range(M + 1)]
    for i in [input() for i in range(N)]:
        A, B = map(int, i.split(' '))
        if A > M:
            continue
        jobs[A].append(B)

    pq = PriorityQueue()
    reward = 0

    for sameJobs in jobs:
        for job in sameJobs:
            pq.put(-job)
        if not pq.empty():
            reward += -(pq.get())

    print(reward)

if __name__ == "__main__":
    main()

Solution (Python):
from queue import PriorityQueue
 
 
def main():
    N, M = map(int, input().split(' '))
    jobs = [[] for _ in range(M + 1)]
    for i in [input() for i in range(N)]:
        A, B = map(int, i.split(' '))
        if A > M:
            continue
        jobs[A].append(B)
 
    pq = PriorityQueue()
    reward = 0
 
    for sameJobs in jobs:
        for job in sameJobs:
            pq.put(-job)
        if not pq.empty():
            reward += -(pq.get())
 
    print(reward)
 
if __name__ == "__main__":
    main()

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

ab.sort()

import heapq
heap = []
ans = 0
i = 0
for day in reversed(range(m)):
    while i<n and ab[i][0]==(m-day):
        heapq.heappush(heap,-ab[i][1])
        i += 1
    if len(heap) > 0:
        ans += -heapq.heappop(heap)
    
print(ans)

Solution (Python):
from heapq import heappush, heappushpop

N, M = map(int, input().split())
tasks = [[] for _ in range(M+1)]
for _ in range(N):
  a, b = map(int, input().split())
  if a <= M:
    tasks[M-a].append(b)

q = []
for day in range(M+1):
  for v in tasks[day]:
    if len(q) < day+1:
      heappush(q, v)
    else:
      heappushpop(q,v)
print(sum(q))

Solution (Python):
import heapq

N, M = map(int, input().split())

A = [[] for _ in range(M)]
Q = []

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

ans = 0

for i in range(0, M):
	for money in A[i]:
		heapq.heappush(Q, -money)
	
	if Q:
		ans += heapq.heappop(Q)
		
print(-ans)

Solution (Python):
import heapq
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
ABList = [[] for _ in range(m)]
for i in range(n):
    a, b = map(int, input().split())
    if a-1>m-1:
        continue
    ABList[a-1].append(-b) 

ans = 0
heap = []
for i in range(m):
    for j in ABList[i]:
        heapq.heappush(heap, j)
    if len(heap)>0:
        B = heapq.heappop(heap)
        ans += -B       

print(ans)

Solution (Python):
import heapq

n,m = list(map(lambda x: int(x), input().split()))

jobs = {}
for i in range(n):
    a,b = list(map(lambda x: int(x), input().split()))
    if a in jobs.keys():
        jobs[a].append(b)
    else:
        jobs[a] = [b]



ans = 0
day = m

possible_job = []

for i in range(m+1):
    if i in jobs.keys():
        for x in jobs[i]:
            heapq.heappush(possible_job, -1 * x)
    if len(possible_job)>0:
        day_work = heapq.heappop(possible_job)
        ans += -1 * day_work
    
print (ans)

Solution (Python):
import heapq
N,M=map(int,input().split())
x={}
for i in range(M+1):
    x[i]=[]
for i in range(N):
    a,b=map(int,input().split())
    if a>M:
        continue
    x[a].append(-b)
y=[]
an=0
for i in range(M+1):
    for n in x[i]:
        heapq.heappush(y,n)
    try:
        an-=heapq.heappop(y)
    except:
        pass
print(an)

Solution (Python):
import sys
import heapq
input = sys.stdin.readline
n, m = map(int, input().split())
q = [0]
heapq.heapify(q)
to = [[] for _ in range(m)]
count = 0
for i in range(n):
    a, b = map(int, input().split())
    if a > m:
        continue
    to[a-1].append(b)
for i in range(m):
    for j in to[i]:
        heapq.heappush(q, -j)
    if len(q) != 0:
        count += heapq.heappop(q) * -1
print(count)

Solution (Python):
from heapq import heappush, heappop
from collections import defaultdict
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]

cand = defaultdict(list)
for a, b in X:
    cand[a].append(b)

pq = []
ans = 0
for i in range(1, M + 1):
    for c in cand[i]:
        heappush(pq, -c)
    if pq:
        ans -= heappop(pq)
    
print(ans)

Solution (Python):
from heapq import heappush, heappop
N, M = map(int, input().split())

Data = [list(map(int, input().split())) for _ in range(N)]
Data.sort(reverse = True)

heap = [0] * M
ans = 0
for i in range(1, M+1):
    N = len(Data)
    for j in range(N-1, -1, -1):
        if Data[j][0] > i:
            break
        if (Data[j][0] <= i):
            heappush(heap, -(Data.pop(j)[1]))

    ans += -(heappop(heap))

print(ans)

Solution (Python):
import heapq
N, M = map(int,input().split())
reward = [[] for _ in range(M)]
for i in range(N):
  A,B = map(int,input().split())
  if A <= M:
    reward[A-1].append(B)
q = []
ans = 0
for day in range(M):
  for r in reward[day]:
    heapq.heappush(q,-r)
  if len(q) >0:
    ans += -heapq.heappop(q)
print(ans)

Solution (Python):
n,m = map(int,input().split())
can = []
for i in range(n):
  a,b = map(int,input().split())
  can.append([a,-b])
can.sort()

here = 0
import heapq
pool = []
ans = 0
for i in range(1,m+1):
  while here < n:
    if can[here][0] == i:
      heapq.heappush(pool,can[here][1])
      here += 1
    else:
      break
  if pool != []:
    ans -= heapq.heappop(pool)
print(ans)

Solution (Python):
import heapq
N, M = list(map(int, input().split()))
jobs = [[] for _ in range(M)]
for i in range(N):
    d, r = list(map(int, input().split()))
    if d-1 < M:
        jobs[d-1].append(-r)

m = 0
heap = []
for i in range(M):
    for a in jobs[i]:
        heapq.heappush(heap, a)
    if len(heap) > 0:
        b = heapq.heappop(heap)
        m += -b
print(m)

Solution (Python):
import heapq

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

q=[]
ans=0
j=0
for i in range(m):
    while j <= n-1:
        if ab[j][0]<=i+1:
            heapq.heappush(q,-ab[j][1])
        else:
            break
        j+=1
    if len(q)!=0:
        ans-=heapq.heappop(q)
print(ans)

Solution (Python):
import heapq

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

a = [[] for i in range(100000)]
d = []
heapq.heapify(d)

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



ans = 0
for i in range(m):
    for j in range(len(a[i])):
        heapq.heappush(d,a[i][j])
    if d != []:
        ans -= heapq.heappop(d)

print(ans)

Solution (Python):
import heapq

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

a_b_list.sort()
task_list = [0] * (M+1)

heap = []
index = 0
for day in range(1, M+1):
    while index < N:
        if a_b_list[index][0] == day:
            heapq.heappush(heap, a_b_list[index][1] * -1)
            index += 1
        else:
            break
    if len(heap) != 0:
        task_list[day] = heapq.heappop(heap)


print(sum(task_list) * -1)

Solution (Python):

N, M = map(int, input().split())

jobs = {}
for _ in range(N):
    A, B = map(int, input().split())
    if A > M :
        continue
    if A in jobs:
        jobs[A].append(B)
    else:
        jobs[A] = [B]


import heapq


point = 0
hq = []

for A in range(1, M+1):
    if A in jobs:
        for B in jobs[A]:
            heapq.heappush(hq, -B)
    else:
        if len(hq) == 0:
            continue
    point += -heapq.heappop(hq)

print(point)

Solution (Python):
import heapq
import sys

h = []
n,m = list(map(int,input().split(" ")))



nums = []
for i in range(n):
    a,b = list(map(int,input().split(" ")))
    nums.append((a,b))
nums.sort(key=lambda x:x[0])

d = 1
ans = 0
st = 0
flag = True
for d in range(1,m+1):
    for i in range(st,n ):
        if nums[i][0] > d:
            st = i
            break
        else:
            if flag:
                heapq.heappush(h,-nums[i][1])
            if st == n-1:
                flag = False

    if h:
        ans -= heapq.heappop(h)


print(ans)

Solution (Python):
import heapq

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

c = [[]for i in range(10**5+10)]

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


h = []
heapq.heapify(h)
ans = 0

for i in range(m):
    for x in c[i+1]:
        heapq.heappush(h,x)
    if h:
        ans += heapq.heappop(h)

print(-ans)

Solution (Python):

"""
Created on Wed Aug 21 23:12:22 2019

@author: Yamazaki Kenichi
"""
import heapq

N, M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]

AB.sort()

q = [0 for _ in range(max(M,N))]
j = 0
ans = 0
for i in range(1,M+1):
    while j < N and AB[j][0] == i:
        heapq.heappush(q,-AB[j][1])
        j += 1
    ans += (-heapq.heappop(q))

print(ans)

Solution (Python):
from heapq import heappop, heappush
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
table = [list(map(int, input().split())) for i in range(N)]
table.sort(key=lambda x:x[0])
Q = []
ans = 0
j = 0
for i in range(1,M+1):
  while j <= N-1:
    A, B = table[j]
    if A <= i:
      heappush(Q,-B)
      j += 1
    else:
      break
  if Q:
    ans += -1*heappop(Q)
print(ans)

Solution (Python):
import heapq

MAX = 10**5+1
N, M = map(int, input().split())
works = [[] for _ in range(MAX)]
for _ in range(N):
  day, reward = map(int, input().split())
  works[day].append(reward)
hp = []
ans = 0
for day in range(1, M+1):
  if len(works[day]) > 0:
    for reward in works[day]:
      heapq.heappush(hp, -reward)
  if len(hp) > 0:
    ans += -heapq.heappop(hp)
print(ans)

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*10**5
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=[b]
for i in v[:M]:
	for j in i:heappush(q,-j)
	z-=heappop(q)
print(z)

Solution (Python):
import heapq
from operator import itemgetter

N,M = map(int,input().split())
AB = []
hq = []
heapq.heapify(hq)
ans = 0

for i in range(N):
    tmpA,tmpB = map(int,input().split())
    if tmpA <= M:
        AB.append([tmpA,tmpB])

AB2 = sorted(AB,key = itemgetter(0))  
maxlen = len(AB2)
last = 0

for i in range(1,M+1):
    for j in range(last,maxlen):
        if AB2[j][0] == i:
            heapq.heappush(hq,-AB2[j][1])

            if j == maxlen -1:
                if len(hq) >= 1:
                    ans += -heapq.heappop(hq)

        else:
            last = j
            if len(hq) >= 1:
                ans += -heapq.heappop(hq)
            break

print(ans)