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):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,m = mi()
A = []
for _ in range(n):
    a,b = mi()
    A.append([a,b])
A.sort()

import heapq
a = []
ima = 0
ans = 0
for i in range(1,m+1):
    while True:
        if ima == n:
            break
        if A[ima][0] <= i:
            heapq.heappush(a,-A[ima][1])
            ima += 1
        else:
            break
    if a:
        ans += heapq.heappop(a)*-1
print(ans)

Solution (Python):
from heapq import*
(n,m),*t=[map(int,s.split())for s in open(0)]
q=[0]*7**6
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):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*7**6
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 sys
input = sys.stdin.readline
n,m = map(int,input().split())
chk = []
for i in range(n):
  a,b =  map(int,input().split())
  if a <= m:
    chk.append((a,b))

from heapq import heappop, heappush
from operator import itemgetter
chk = sorted(chk,key=itemgetter(0),reverse=True)

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

Solution (Python):
N, M = [int(s) for s in input().split()]
rewards = list(filter(lambda x: x[0] <= M
                , sorted([[int(s) for s in input().split()] for _ in range(N)])))[::-1]

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

sums = 0
for day in reversed(range(M)):
    while(rewards):
        if M-day >= rewards[-1][0]:
            r = rewards.pop()
            heapq.heappush(a, -1 * r[1])
        else:
            break
    
    if a:
        sums += -1 * heapq.heappop(a)

print(sums)

Solution (Python):
from operator import itemgetter
import heapq

N, M = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(N)]
pairs.sort(key = itemgetter(0))

heap = []
reward = 0
index_of_pairs = 0   
for i in range(1, M+1):
    while index_of_pairs < N and pairs[index_of_pairs][0] <= i:
        heapq.heappush(heap, -pairs[index_of_pairs][1])
        index_of_pairs += 1
    if heap:
        reward -= heapq.heappop(heap)

print(reward)

Solution (Python):

def main():
    import sys
    import heapq as h
    from collections import defaultdict
    N, M = map(int, sys.stdin.readline().split())
    L = tuple([tuple(map(int, sys.stdin.readline().split())) for _ in range(N)])
    
    D = defaultdict(list)
    for i in range(N):
        D[L[i][0]-1].append(-L[i][1])
    
    
    ans = 0
    tmp=[]
    h.heapify(tmp)
    for d in range(M):
        for b in D[d]:
            h.heappush(tmp, b)
        
        if tmp:
            ans += h.heappop(tmp)
        
    print(-ans)



if __name__=='__main__':
    main()

Solution (Python):
import heapq
from collections import deque


def main():
    N, M = list(map(int, input().split(' ')))
    tasks = list()
    for _ in range(N):
        req_day, reward = list(map(int, input().split(' ')))
        latest_start_day = M - req_day
        tasks.append((-reward, latest_start_day))
    
    tasks.sort(key=lambda t: t[1], reverse=True)
    tasks = deque(tasks)
    task_que = []
    neg_reward = 0
    for d in range(M, -1, -1):
        while len(tasks) > 0 and tasks[0][1] >= d:
            heapq.heappush(task_que, tasks.popleft())
        if len(task_que) == 0:
            continue
        
        task = heapq.heappop(task_que)
        neg_reward += task[0]
    print(-neg_reward)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
day = {i+1: [] for i in range(M)}
for a, b in ab:
  if a <= M:
    day[a] += [-b]
q = []
heapq.heapify(q)
pay = 0
for i in range(M):
  for b in day[i+1]:
    heapq.heappush(q, b)
  if len(q) > 0:
    pay += -heapq.heappop(q)

print(pay)

Solution (Python):
from heapq import heappush, heappop
N, M = map(int, input().split())
D = {i+1: [] for i in range(M)}
for _ in range(N):
  A, B = map(int, input().split())
  if A <= M:
    D[A].append(B)
rew = 0
pq = []
for i in range(M):
  for j in D[i+1]:
    heappush(pq, -j)
  if pq != []:
    rew += -heappop(pq)
print(rew)

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)
from heapq import heapify,heappop,heappush

def main():
    N,M = map(int,input().split())
    works = [tuple(map(int,input().split())) for _ in range(N)]
    works.sort(key = lambda x:x[0])
    q = []
    ans = 0
    j = 0
    for i in range(M):
        while j < N and works[j][0] <= i + 1:
            heappush(q,-works[j][1])
            j += 1
        if q:
            p = heappop(q)
            ans -= p
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import heapq
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    N, M = map(int, input().split())

    D = defaultdict(list)
    for i in range(N):
        A, B = map(int, input().split())
        D[A].append(-1 * B)
    R = []
    answer = 0
    for i in range(1, M + 1):
        if len(D[i]) > 0:
            d = D[i]
            for t in d:
                heapq.heappush(R, t)
        if R:
            answer += heapq.heappop(R)

    print(-1 * answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, M = map(int, input().split())
    d = {i: [] for i in range(M + 1)}
    for _ in range(N):
        a, b = map(int, input().split())
        if a > M:
            continue
        d[a].append(b)

    ans = 0
    q = []
    heapq.heapify(q)
    for i in range(M, 0, -1):
        a = M - i + 1
        for b in d[a]:
            heapq.heappush(q, -b)
        if q:
            b = heapq.heappop(q)
            ans += -b

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq
N, M = map(int,input().split())
L = [[] for _ in range(M)]
for _ in range(N):
    A, B = map(int,input().split())
    try:
        L[A-1].append(-B)
    except:
        pass
Q = []
ans = 0
for i in range(M):
    for j in L[i]:
        heapq.heappush(Q, j)
    try:
        ans -= heapq.heappop(Q)
    except:
        pass
print(ans)

Solution (Python):
import heapq

n,m=map(int,input().split())
task=[[0] for _ in range(m+1)]

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

task=task[::-1]

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

for i in range(m,-1,-1):
    cnt=0
    for j in task[i]:
        heapq.heappush(tmp,-j)
    cnt=heapq.heappop(tmp)
    ans-=cnt
print(ans)

Solution (Python):
def main():
    import heapq
    n,m = map(int,input().split())
    jobs = {}
    for i in range(n):
        a,b = map(int,input().split())
        if a not in jobs.keys():
            jobs[a] = [b]
        else:
            jobs[a] += [b]
    ob = []
    ans = 0
    for i in range(1,m+1):
        if i in jobs.keys():
            for j in range(len(jobs[i])):
                heapq.heappush(ob,-1*jobs[i][j])
        if len(ob)>0:
            ans += heapq.heappop(ob)*-1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
f=lambda:map(int,input().split())
n,m=f()
lt=sorted(list(f()) for _ in range(n))[::-1]
from heapq import *
q,s=[],0
for i in range(m):
  while lt and lt[-1][0]<i+2: a,b=lt.pop(); heappush(q,-b)
  if q: s-=heappop(q)
print(s)

Solution (Python):
import sys
f=lambda:map(int,sys.stdin.readline().split())
n,m=f()
lt=sorted(tuple(map(int,input().split())) for _ in range(n))[::-1]
from heapq import *
q=[]
s=t=0
while t<m:
  t+=1
  while lt and lt[-1][0]==t:
    a,b=lt.pop()
    heappush(q,-b)
  if q: s-=heappop(q)
print(s)

Solution (Python):
import heapq


N,M=map(int,input().split())
 
i=0
inp ={}
while i<N:
  readTime,inCome=map(int,input().split())
  if readTime in inp:
    inp[readTime].append(-inCome)
  else:
    inp[readTime]=[-inCome]
  
  i=i+1
 

 
result=0
pool=[]
heapq.heapify(pool)
i=1
while i<=M:
  if i in inp:
    for k in inp[i]:
      heapq.heappush(pool,k)
  if len(pool)>0:
    result=result-heapq.heappop(pool)
  i=i+1
 
print(result)

Solution (Python):
import heapq


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

AB.sort()
ans = 0 
q = []
heapq.heapify(q)
idx = 0
for i in range(1,M+1):
    while idx<N and AB[idx][0]<=i:
        heapq.heappush(q, -AB[idx][1])
        idx += 1
    if q:
        p = -heapq.heappop(q)
        ans += p
print(ans)

Solution (Python):

from heapq import heappush, heappop
import sys    
input = sys.stdin.readline    
n, m = map(int, input().split())
    
vmax = pow(10, 5)
jobs = {i:[] for i in range(1,vmax + 1)}
for _ in range(n):
    a, b = map(int, input().split())
    jobs[a].append(-b)
    

ans =0
hq = []
for i in range(1, m + 1):
    for j in jobs[i]:
        heappush(hq, j)
    if hq:
        v = heappop(hq)
        v *= -1
        ans += v
print(ans)

Solution (Python):

from heapq import heappush, heappop
import sys    
input = sys.stdin.readline    
n, m = map(int, input().split())
    
vmax = pow(10, 5)
jobs = {i:[] for i in range(1,vmax + 1)}
for _ in range(n):
    a, b = map(int, input().split())
    jobs[a].append(-b)
    

ans =0
hq = []
for i in range(1, m + 1):
    for j in jobs[i]:
        heappush(hq, j)
    if hq:
        v = heappop(hq)
        v *= -1
        ans += v
print(ans)

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

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

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

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

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

Solution (Python):
import sys
from heapq import *

readline = sys.stdin.readline
N, M = map(int, readline().split())
days = [[] for _ in range(M)]
for _ in range(N):
    a, b = map(int, readline().split())
    if a <= M:
        days[a-1].append(b)
ans = 0
hq = []
heapify(hq)
for day in days:
    for pay in day:
        heappush(hq, -pay)
    if hq:
        ans += -heappop(hq)
print(ans)

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


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

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

available_jobs = []
total_pay = 0
for day in range(1, M + 1):
    for pay in jobs[day]:
        heappush(available_jobs, pay)
    if available_jobs:
        total_pay -= heappop(available_jobs)
print(total_pay)

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

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

ablist=[]
for i in range(N):
  A,B=map(int,input().split())
  ablist.append((A,B))
ablist.sort(reverse=True)
  
hq=[]
answer=0
for i in reversed(range(M)):
  while ablist:
    a,b=ablist[-1]
    if i+a<=M:
      ablist.pop()
      heapq.heappush(hq,((-b,a)))
    else:
      break
  
  if hq:
    mb,a=heapq.heappop(hq)
    answer-=mb
    
    
print(answer)

Solution (Python):
import heapq
N, M = map(int, input().split())
jobs = [[] for _ in range(M)]
for _ in range(N):
    A, B = map(int, input().split())
    if A > M:
        continue
    jobs[M-A].append(B)
ans = 0
q = []
heapq.heapify(q)
for i in range(M-1, -1, -1):
    for item in jobs[i]:
        heapq.heappush(q, item * (-1))
    if q:
        ans += heapq.heappop(q) * (-1)
print(ans)

Solution (Python):
from heapq import *
N,M = map(int,input().split())
l = [[0] for _ in range(M+1)] 
for _ in range(N):
    a, b = map(int, input().split())
    if a<=M:
        l[a].append(b)

ans = 0
h = []

for i in range(1,M+1):
  for j in l[i]:
    heappush(h, -j)
  ans += heappop(h)
ans *= -1
print(ans)

Solution (Python):
import heapq
N,M=map(int,input().split())
W=[list(map(int,input().split())) for _ in range(N)]
a=[[i,0] for i in range(1,M+2)]
W.extend(a)
W.sort()
q=[]
ans=0
for i,j in W:
    if i>M+1:
        break
    if j!=0:
        heapq.heappush(q,-j)
    else:
        heapq.heappush(q,-j)
        ans-=heapq.heappop(q)
print(ans)

Solution (Python):


import heapq

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

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

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

ans = 0
hq = []
for i in range(1, m+1):
    for j in w[i]:
        heapq.heappush(hq, -j)
    ans += heapq.heappop(hq)

print(ans * -1)

Solution (Python):
import heapq

n, m = map(int, input().split())
lis = [[] for _ in range(m + 1)]

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

cnd = []
res = 0

for j in range(1, m + 1):
    for e in lis[j]:
        heapq.heappush(cnd, e)
    if cnd:
        res += -heapq.heappop(cnd)
print(res)

Solution (Python):
import heapq

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

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

cnd = []
heapq.heapify(cnd)
res = 0

for j in range(1, m + 1):
    for e in lis[j]:
        heapq.heappush(cnd, e)
    if cnd:
        res += -heapq.heappop(cnd)
print(res)

Solution (Python):
import heapq

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

for i in range(N):
  a,b=map(int,input().split())
  if a>M:
    continue
  else:
    jobs[M-a].append(b)
ans=0
l=[]
for i in range(M-1,-1,-1):
  for j in jobs[i]:
    heapq.heappush(l,-j)
  if len(l):
    ans+=-heapq.heappop(l)
print(ans)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

N,M = LI()
data = [LI() for _ in range(N)]































data.sort()
p = 0
q = queue.PriorityQueue()
ans = 0
for i in range(1,M+1):
    while p < N:
        if data[p][0] > i : break
        q.put(-data[p][1])
        p += 1
    if not q.empty():
        ans -= q.get()
print (ans)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

N,M = LI()
data = [LI() for _ in range(N)]































data.sort()
p = 0
q = queue.PriorityQueue()
ans = 0
for i in range(1,M+1):
    while p < N:
        if data[p][0] > i : break
        q.put(-data[p][1])
        p += 1
    if not q.empty():
        ans -= q.get()
print (ans)

Solution (Python):

import heapq
n, m = map(int, input().split())
jobs = [[] for i in range(m)]
for i in range(n):
    a, b = map(int, input().split())
    if a > m:
        continue
    jobs[m-a].append(b)
ans = 0
l = []
for i in range(m-1, -1, -1):
    for j in jobs[i]:
        heapq.heappush(l, -j)
    if len(l):
        ans += -heapq.heappop(l)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

import heapq

n, m = map(int, readline().split())
ab = [[] for _ in range(10 ** 5 + 1)]
for _ in range(n):
    a, b = map(int, readline().split())
    ab[a - 1].append(-b)
ans = 0
q = []
for mm in range(m):
    while ab[mm]:
        heapq.heappush(q, ab[mm].pop())
    if q:
        ans += heapq.heappop(q)
print(-ans)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
LI = lambda : [int(x) for x in sys.stdin.readline().split()]

N,M = LI()
data = [LI() for _ in range(N)]
dp = [0] * (M+1)
data.sort(key=lambda x:x[1], reverse=True)

N0 = 2 ** M.bit_length()
seg = [0] * (2*N0)

def seg_set(i,x):
    i = i + N0- 1
    seg[i] = x
    while i > 0:
        i = (i-1) // 2
        seg[i] = max(seg[i*2+1],seg[i*2+2])

def seg_query(i):
    i = i + N0 - 1
    ret = seg[i]
    while i ^ (i+1) < i+1:
        i = (i-2) // 2
        ret = max(ret,seg[i])
    return ret

for i in range(M+1):
    seg_set(i,i)

for a,b in data:
    if a <= M:
        i = seg_query(M-a+1)
        if i > 0:
            dp[i] = b
            seg_set(i,0)
print(sum(dp))

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

Solution (Python):
import heapq
n, m = map(int, input().split())
M = [[] for _ in range(m + 1)]
q = []
for i in range(n):
    a, b = map(int, input().split())
    if a <= m:
        M[m - a] += [b]
for day in range(m + 1):
    for value in M[day]:
        if len(q) <= day:
            heapq.heappush(q, value)
        else:
            heapq.heappushpop(q, value)
print(sum(q))

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

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

for i in range(1, m+1):
    for item in albeits[i]:
        heapq.heappush(queue, -item)
    try:
        ans += -heapq.heappop(queue)
    except:
        pass

print(ans)

Solution (Python):
import heapq
N, M = map(int, input().split())
AB = [[] for _ in range(10**5+10)]
for _ in range(N):
	ta, tb = map(int, input().split())
	AB[ta].append(-tb)

ans = 0
q = []
for i in range(1,M+1):
	while AB[i]:
		heapq.heappush(q, AB[i].pop())

	if q:
		ans += heapq.heappop(q)

print(-ans)

Solution (Python):
from heapq import heappush, heappop

n, m = [int(x) for x in input().split()]

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

h = []
ans = 0
for i in range(1, m + 1):
    for b in temp_list[i]:
        heappush(h, b)

    if len(h) == 0:
        continue
    
    ans -= heappop(h)
print(ans)

Solution (Python):
def main():
    import heapq
    n,m = map(int,input().split())
    alb = {}
    for i in range(n):
        a,b = map(int,input().split())
        if a not in alb.keys():
            alb[a] = [b]
        else:
            alb[a] += [b]
    h = []
    s = 0
    for i in range(1,m+1):
        if i in alb.keys():
            for j in range(len(alb[i])):
                heapq.heappush(h, -1*alb[i][j])
        if len(h)>0:
            m = heapq.heappop(h)
            s += -1*m
    print(s)

if __name__ == "__main__":
    main()

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

N, M = map(int, input().split())
d = defaultdict(list)

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

pq = []
ans = 0

for i in range(M-1, -1, -1):
    for v in d[i]:
        heappush(pq, -v)
    
    if len(pq)>0:
        ans += -heappop(pq)

print(ans)

Solution (Python):


import sys





from heapq import heapify, heappush, heappop 













def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n, m = mi()
    L = sorted([lmi() for _ in range(n)], key=lambda x: x[0], reverse=True)
    
    h = []
    ans = 0
    for day in range(1, m+1):
        while len(L) != 0 and L[-1][0] == day:
            latency, money = L.pop()
            heappush(h, [-money, latency])
        
        if len(h) != 0:
            minus_money, latency = heappop(h)
            ans += - minus_money
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque

from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [LI()for i in range(n)]



n,m=MI()
Q=[]
heapify(Q)
ar=[[] for i in range(m)]
for i in range(n):
    a,b=MI()
    if a<=m:
        ar[a-1].append(b)

ans=0
for i in range(m):
    for j in ar[i]:
        heappush(Q,j*(-1))
    if Q:
        s=heappop(Q)*(-1)
        ans+=s
print(ans)

Solution (Python):
import heapq

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

a = []
ans = 0

for k in range(m - 1, -1, -1):
    i = m - k - 1
    for j in l[i]:
        heapq.heappush(a, (-1) * j)
    if a:
        ans += heapq.heappop(a) * (-1)
        
print(ans)

Solution (Python):
import heapq

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

a = []
ans = 0

for i in range(m):
    for j in l[i]:
        heapq.heappush(a, (-1) * j)
    if a:
        ans += heapq.heappop(a) * (-1)
        
print(ans)

Solution (Python):
import heapq

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

a = []
ans = 0

for i in range(m):
    for j in l[i]:
        heapq.heappush(a, (-1) * j)
    if a:
        ans += heapq.heappop(a) * (-1)
        
print(ans)

Solution (Python):
import heapq

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

a = []
ans = 0

for i in range(m):
    for j in l[i]:
        heapq.heappush(a, (-1) * j)
    if a:
        ans += heapq.heappop(a) * (-1)
        
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


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

heap = []
for day in range(M + 1):
    for job in jobs[day]:
        if len(heap) <= day:
            heappush(heap, job)
        else:
            heappush(heap, job)
            heappop(heap)

print(sum(heap))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


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

heap = []
for day in range(M + 1):
    for job in jobs[day]:
        if len(heap) < day + 1:
            heappush(heap, job)
        else:
            heappush(heap, job)
            heappop(heap)

print(sum(heap))

Solution (Python):
from collections import deque
from heapq import heappop, heappush, heapify
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7


def main():
  N, M = map(int, readline().split())
  AB = [[int(x) for x in readline().split()] for _ in range(N)]
  
  days = [[0] for _ in range(M)]
  for ab in AB:
    if ab[0]-1>=M:
      continue
    else:
      days[ab[0]-1].append(ab[1])
  stack = deque(days)
  q = []
  ans = 0
  while stack:
    x = stack.popleft()
    for y in x:
      heappush(q,-y)
    z = heappop(q)
    ans += z
  print(-ans)
  
  
      
if __name__ == '__main__':
  main()

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

Q = []
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)

Solution (Python):
from heapq import heapify, heappop, heappush
N,M = map(int, input().split())
AB = []
for i in range(N):
    a,b = map(int, input().split())
    if a<=M:
        AB.append((a,-b))
    else:
        N-=1
AB.sort()

Q = []
heapify(Q)
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)

Solution (Python):
from heapq import heapify, heappop, heappush
N,M = map(int, input().split())
AB = []
for i in range(N):
    a,b = map(int, input().split())
    if a<=M:
        AB.append((a,-b))
    else:
        N-=1
AB.sort()

Q = []
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)

Solution (Python):
from heapq import heapify, heappop, heappush
N,M = map(int, input().split())
AB = []
for i in range(N):
    a,b = map(int, input().split())
    if a<=M:
        AB.append((a,-b))
    else:
        N-=1
AB.sort()

Q = []
heapify(Q)
ans = 0
i = 0

for day in range(M):
    while i<N and AB[i][0] == day+1:
        heappush(Q, AB[i][1])
        i+=1
    if Q:
        ans -= heappop(Q)
print(ans)

Solution (Python):
from collections import defaultdict
import heapq
N,M = map(int,input().split())
D = defaultdict(list)
for i in range(N):
    A,B = map(int,input().split())
    A = M - A
    D[A].append(B)

h = []

su = 0
hlen = 0
for i in range(M-1,-1,-1):
    for b in D[i]:
        heapq.heappush(h,-b)
        hlen += 1
    if hlen:
        su -= heapq.heappop(h)
        hlen -= 1

print(su)

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]


N,M = inputlist()
li = []
for i in range(N):
    A,B = inputlist()
    B *= -1
    li.append([A,B])
ans = 0
from heapq import *
li.sort()
k = []
heapify(k)
flag = 0
for i in range(1,M+1):
    if flag == N:
        for t in range(M+1-i):
            ans -= heappop(k)
        break
    while li[flag][0] <= i:
        heappush(k,li[flag][1])
        flag = flag + 1
        if flag == N:
            break
    if len(k)>=1:
        ans -= heappop(k)
print(ans)

Solution (Python):

def inputlist(): return [int(k) for k in input().split()]


N,M = inputlist()
li = []
for i in range(N):
    A,B = inputlist()
    B *= -1
    li.append([A,B])
ans = 0
from heapq import *
li.sort()
k = []
heapify(k)
flag = 0
for i in range(1,M+1):
    if flag == N:
        for i in range(M+1-i):
            ans -= heappop(k)
        break
    while li[flag][0] <= i:
        heappush(k,li[flag][1])
        flag = flag + 1
        if flag == N:
            break
    if len(k)>=1:
        ans -= heappop(k)
print(ans)

Solution (Python):
import heapq as hq
n,m = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int,input().split())
    ab.append((a,-b))
ab.sort()
ans = 0
idx = 0
q = []
for i in range(1,m+1):
    while idx<n and ab[idx][0] ==i:
        a,mb = ab[idx]
        hq.heappush(q,mb)
        idx+=1
    if q!=[]:
        ans += -1*hq.heappop(q)

print(ans)

Solution (Python):
import heapq as hq
n,m = map(int,input().split())
ab = []
for i in range(n):
    a,b = map(int,input().split())
    ab.append((a,-b))
ab.sort() 
ans = 0
idx = 0
q = []
for i in range(1,m+1):
    while idx<n and ab[idx][0] ==i:
        a,mb = ab[idx]
        hq.heappush(q,mb)
        idx+=1
    if q!=[]:
        ans += -1*hq.heappop(q)

print(ans)

Solution (Python):
import heapq
import queue

n,m=map(int,input().split())
L=[]
for inp in range(n):
    a,b=map(int,input().split())
    L.append((a,b))
L.sort(reverse=True)

pq=[]
heapq.heapify(pq)

    
ans=0
for i in range(1,m+1):
    while len(L)>0 and L[-1][0]==i:
        heapq.heappush(pq,L.pop()[1]*-1)
    
    if len(pq)==0:
        continue
    else:    
        ans-=heapq.heappop(pq)

print(ans)

Solution (Python):
from heapq import heappush, heappop
 
N,M = map(int,input().split())
A = [0]*N
B = [0]*N
for i in range(N):
    A[i],B[i] = map(int,input().split())
AB = [[] for _ in range(M+1)] 
for a,b in zip(A,B):
    if a <= M:
        AB[a].append(b)
        
q = []
answer = 0

for day in AB:
    
    for pay in day:
        heappush(q,-pay)
    if len(q) > 0:
        answer += -heappop(q)
        
print(answer)

Solution (Python):
import heapq

N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
kouho = []
ans = 0

AB = sorted(AB)


n = 0
heapq.heapify(kouho)
for i in range(1, M+1):
    if n < N:
        while AB[n][0] <= i:
            heapq.heappush(kouho, AB[n][1] * (-1))
            n += 1
            if n >= N:
                break
    
    if kouho:
        b = heapq.heappop(kouho)
        ans += b*(-1)
    
print(ans)