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 sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,M= MI()
    works = []
    for _ in range(N):
        date,money = map(int,input().split())
        works.append((-money,date))
    works.sort(key = itemgetter(1),reverse = True)

    ans =0

    pri_queue = []
    for date in range(M):
        while works and works[-1][1] -1 <= date:
            heappush(pri_queue,works.pop())
        if pri_queue:
            ans += -heappop(pri_queue)[0]

    print(ans)
    
    
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):
import sys
def input():
    return sys.stdin.readline()[:-1]
import heapq
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(reverse=True)
h=[]

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


print(ans)

Solution (Python):
import sys
from collections import defaultdict
import heapq
input = sys.stdin.readline
n, limit = map(int, input().split())
dic = defaultdict(list)

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

L = []
ans = 0
for i in range(limit+1):
    for val in dic[i]:
        heapq.heappush(L, -val)
    if len(L):
        ans -= heapq.heappop(L)
print(ans)

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

N, M = map(int,input().split())
work_list = [[] for j in range(M+1)]
for i in range(N):
    temp = list(map(int,input().split()))
    if temp[0] > M:
        continue
    work_list[temp[0]].append(temp[1])

sum = 0
works = []

for i in work_list:
    for j in i:
        heappush(works, -1*j)
    if len(works) == 0:
        continue
    sum -= (heappop(works))

print(sum)

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

N, M = map(int,input().split())
work_list = [[] for j in range(M+1)]
for i in range(N):
    temp = list(map(int,input().split()))
    if temp[0] > M:
        continue
    work_list[temp[0]].append(temp[1])

sum = 0
works = []

for i in work_list:
    for j in i:
        heappush(works, -1*j)
    if len(works) == 0:
        continue
    sum -= (heappop(works))

print(sum)

Solution (Python):
from queue import PriorityQueue
N, M = map(int, input().split())  

jobs_list = [[] for _ in range(M+1)]

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

pq = PriorityQueue()

ans = 0

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

print(ans)

Solution (Python):
from heapq import heappush,heappop

N,M = (int(i) for i in input().split())
a = [0] *N
b = [0] *N
T = []
list = [[] for i in range(M+1)]

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

k = 0
ans = 0
for j in range(1,M+1):
    for i in list[j]:
        heappush(T,-1 * i)
    if T != []:
        ans += -1 * heappop(T)

print(ans)

Solution (Python):
from queue import PriorityQueue
N, M = map(int, input().split())  

jobs = [[] for _ 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
pq = PriorityQueue()

for i in range(M-1, -1, -1):  
    for b in jobs[i]:
        pq.put(-b)  

    if not pq.empty():
        ans += -(pq.get())

print(ans)

Solution (Python):
from queue import PriorityQueue


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

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

pq = PriorityQueue()
ans = 0
for i in range(M-1, -1, -1):  
    for b in jobs[i]:  
        pq.put(-b)  
    
    if not pq.empty():  
        ans += -(pq.get())

print(ans)

Solution (Python):
from heapq import *

n, m = map(int, input().split())
abab = [list(map(int, input().split())) for _ in range(n)]
bites = [[] for _ in range(m)]
for a, b in abab :
    if a <= m : bites[a-1].append(-b)
    
ans = 0
board = []
for bite in bites:
    for b in bite : heappush(board, b)
    if board : ans -= heappop(board)
print(ans)

Solution (Python):
import heapq

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

job = [[] for i in range(M)]

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

heap = []
ans = 0
for i in range(M):
    [heapq.heappush(heap, r) for r in job[i]]
    
    if len(heap) > 0:
        ans -= heapq.heappop(heap) 
        

print(ans)

Solution (Python):
import heapq

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

res = 0
heap = []

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

print(res)

Solution (Python):
import heapq

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

a = [0] * n
b = [0] * n
info = [[] for _ in range(m+1)]

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

l = []
count = 0

for j in range(1,m+1):
    for x in info[j]:
        heapq.heappush(l,-x)
    if l != []:
        count += -heapq.heappop(l)

print(count)

Solution (Python):
import heapq

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

a = [0] * n
b = [0] * n
info = [[] for _ in range(m+1)]

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

l = []
count = 0

for j in range(1,m+1):
    for x in info[j]:
        heapq.heappush(l,-x)
    if l != []:
        count += -heapq.heappop(l)

print(count)

Solution (Python):
import heapq

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

hq = []

ans = 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)

Solution (Python):
import heapq

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

hq = []

ans, ct, idx = 0, 0, 0
for A in range(1, M+1):
    for B in W[A]:
        heapq.heappush(hq, -B)
    if hq:
        B = heapq.heappop(hq)
        ans -= B
print(ans)

Solution (Python):
import sys
from collections import deque, defaultdict
from heapq import heappop, heappush
sys.setrecursionlimit(20000000)
input = sys.stdin.readline


def main():
    n, limit = map(int, input().split())
    
    jobs = defaultdict(deque)

    for _ in range(n):
        d, v = map(int, input().split())
        jobs[d].append(v)

    money = 0
    available_jobs = []

    for d in range(1, limit+1):
        if d in jobs:
            for j in jobs[d]:
                heappush(available_jobs, -j)

        if available_jobs:
            money += -heappop(available_jobs)

    print(money)


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappush, heappop
n,m = map(int,input().split())
jobs = [[] for i in range(100001)]
for i in range(n):
    a1,b1 = map(int,input().split())
    jobs[a1].append(b1)

reward = 0
heapsort = []
for daysLeft in range(1,m+1):
    if len(jobs[daysLeft]) != 0:
        for i in jobs[daysLeft]:
            heappush(heapsort,-i)
    if len(heapsort) > 0:
        reward += -heappop(heapsort)
print(reward)

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

N, M = list(map(int, input().split()))
jobs = defaultdict(list)
heap = []
ans = 0
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        jobs[a].append(b)

for m in range(1, M+1):
    for b in jobs[m]:
        heappush(heap, -b)
    if heap:
        maxb = -heappop(heap)
        ans += maxb
print(ans)

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop

_input = list(map(int, input().split()))
n = _input[0]
m = _input[1]
jobs = defaultdict(list)


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



reward = 0
heapsort = []
for daysLeft in range(1, m + 1):
    if daysLeft in jobs:
        for b in jobs[daysLeft]:
            heappush(heapsort, -b)
    if len(heapsort) > 0:
        reward += -heappop(heapsort)
print(reward)

Solution (Python):
import heapq

N, M = map(int, input().split())
dict = {}
keys = set([])
for i in range(N):
    a, b = map(int, input().split())
    if a in keys:
        dict[a].append(b)
    else:
        keys.add(a)
        dict[a] = [b]


cand = []
cand_num = 0
val = 0
for a in range(1, M + 1):
    
    if a in keys:
        for b in dict[a]:
            heapq.heappush(cand, (-b, a))
            cand_num += 1

    if cand_num != 0:
        (b_0, a_0)= heapq.heappop(cand)
        cand_num -= 1
        val += -b_0
    else:
        pass

print(val)

Solution (Python):
from sys import stdin
def main():
  from builtins import int, map, range
  readline = stdin.readline
  from heapq import heappush, heappop
  n, m = map(int, readline().split())
  jobs = {}
  for _ in range(n):
    a, b = map(int, readline().split())
    if a > m:
      continue
    if a in jobs:
      jobs[a].append(b)
    else:
      jobs[a] = [b]
  result = 0
  candidates = []
  for a in range(1, m + 1):
    if a in jobs:
      for b in jobs[a]:
        heappush(candidates, -b)
    else:
      if len(candidates) == 0:
        continue
    result += -heappop(candidates)
  print(result)
main()

Solution (Python):
import math

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

jobs = []
for i in range(n):
	day, reward = map(int, input().split())
	if m- day + 1 > 0:
		jobs.append((m - day + 1, reward))

jobs.sort(key = lambda x:(-x[1],-x[0]))

def find(i):
	global parent
	while i != parent[i]:
		parent[i] = parent[parent[i]]
		i = parent[i]
	return i 

def union(x, y):	
	global parent
	parent_x = find(x)
	parent_y = find(y)
	parent[parent_y] = parent_x

parent = [i for i in range(m+1)]

def job_sequencing(jobs):
	global parent 
	n = len(jobs)

	total_rewards = 0
	for job in jobs:
		available_slot = find(job[0])
		if available_slot > 0:
			union(available_slot-1, available_slot)
			total_rewards += job[1]
	return total_rewards
print(job_sequencing(jobs))

Solution (Python):
class Union:
  def __init__(self, num):
    self.T = [-1 for i in range(num)]

  def root(self, x):
    if self.T[x] == len(self.T):
      return len(self.T)
    if self.T[x] < 0: return x
    k = self.root(self.T[x])
    self.T[x] = k
    return k

  def merge(self, x, y):
    if y >= len(self.T):
      self.T[-1] = len(self.T)
      return True
    a = self.root(x)
    b = self.root(y)
    self.T[a] = b
    return True


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

AB.sort(key = lambda x:x[1], reverse = True)

ans = 0
T = Union(M)
for i in range(N):
  if AB[i][0] > M:
    continue
  t = AB[i][0] - 1
  k = T.root(t)
  if t == k:
    ans += AB[i][1]
    T.merge(t, t + 1)
  else:
    t = k
    if t < M:
      ans += AB[i][1]
      T.merge(t, t + 1)

print(ans)

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

sys.setrecursionlimit(10 ** 7)

n, m = input().split(' ')
N = int(n)
M = int(m)

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



index = 0
pool = []
rewords = 0
for i in range(M):
    day = i + 1

    while index < N and day >= int(sorted_ab[index][0]):
        heappush(pool, -sorted_ab[index][1])
        index += 1
    
    if pool:
        rewords += -heappop(pool)

print(rewords)

Solution (Python):
from heapq import heappop, heappush

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

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

hq = []
acc = 0
i = 0
j = 1
while j<M+1:
    while i<N and List[i][0]==j:
        heappush(hq, -List[i][1])
        i+=1
    if len(hq)>0:
        acc+=heappop(hq)*(-1)
    j+=1

print(acc)

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

import heapq

def main():  
    N,M=map(int, input().split())
    B=[[] for _ in range(M+1)]
    heapq.heapify(B)
    for i in range(N):
        a,b=map(int, input().split())
        if a > M: continue
        B[a].append(-b)
    max=[]
    heapq.heapify(max)
    ans=0
    for i in range(1, M+1):
        for j in B[i]:
            heapq.heappush(max, j)
        if len(max) != 0:
            ans+=heapq.heappop(max)
    print(-ans)


if __name__ == "__main__":
    main()

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

import heapq

def main():  
    N,M=map(int, input().split())
    B=[[] for _ in range(M+1)]
    heapq.heapify(B)
    for i in range(N):
        a,b=map(int, input().split())
        if a > M: continue
        B[a].append(-b)
    max=[]
    heapq.heapify(max)
    ans=0
    for i in range(1, M+1):
        for j in B[i]:
            heapq.heappush(max, j)
        if len(max) != 0:
            ans+=heapq.heappop(max)
    print(-ans)


if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappush, heappop

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


works = [[] for _ in range(10**6)]

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

hq = []
ans = 0

for i in range(1,M+1):
    
    for w in works[i]:
        
        heappush(hq, -w)
    
    if hq:
        ans += -heappop(hq)

print(ans)

Solution (Python):
from heapq import heappop, heappush
from bisect import bisect
from collections import deque
inf = 10**5


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

ans = 0
hq = []

for i in range(M-1,-1,-1):
    index = bisect(info, [M-i,inf])
    for _ in range(index):
        A, B = info.popleft()
        heappush(hq, -B)
    if hq:
        B = -heappop(hq)
        ans += B

print(ans)

Solution (Python):
import heapq
from bisect import bisect_left
n, m = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
l.sort()
b = 0
hq = []
ans = 0
for d in range(1, m + 1):
    idx = bisect_left(l, [d + 1, -1])
    for i in range(b, idx):
        heapq.heappush(hq, -l[i][1])
    if hq:
        ans += -heapq.heappop(hq)
    b = idx
print(ans)

Solution (Python):
import heapq

N,M=map(int,input().split())
d={}
for i in range(M):d[i]=[0]
for i in range(N):
    a,b=map(int,input().split())
    if 0<=M-a:
        d[M-a].append(b)

hq=[]
ans=0
for i in range(M)[::-1]:
    [heapq.heappush(hq,-j) for j in d[i]]
    ans-=heapq.heappop(hq)
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
job_list = [[] for _ in range(m)]
for _ in range(n):
    k, v = map(int, input().split())
    if k-1 < m:
        job_list[k-1].append(-v)

q = []
ans = 0
for i in range(m):
    for push_item in job_list[i]:
        heapq.heappush(q, push_item)
    if len(q) > 0:
        ans += -heapq.heappop(q)
print(ans)

Solution (Python):
import heapq


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



task_list = []

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

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

j = 0
hq = []
ans = 0

for i in range(1, m+1):
    
    while j < n and task_list[j][0] <= i:
        heapq.heappush(hq, -task_list[j][1])
        j += 1
    if len(hq):
        ans += heapq.heappop(hq)

print(-ans)

Solution (Python):
from heapq import heappop, heappush

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

task_list = []

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

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

hq = []
ans, j = 0, 0

for i in range(1, m+1):
  while j < n and task_list[j][0] <= i:
    heappush(hq, -task_list[j][1])
    j += 1
  if len(hq):
    ans += -heappop(hq)
  
print(ans)

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

Solution (Python):
import heapq
N, M = map(int, input().split())
lst = [[] for _ in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    if a-1 < M:
        lst[a-1].append(-b)
        
total = 0
heap = []

for i in range(M):
    for reward in lst[i]:
        heapq.heappush(heap, reward)
    if len(heap) > 0:
        reward = heapq.heappop(heap)
        total += -reward
print(total)

Solution (Python):
from heapq import heappush, heappop

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

jobs = {i:[] for i in range(1, M+1)}


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

ans = 0
heap = []
for day in range(1,M+1):
  lis = jobs[day]
  for pay in lis:
    heappush(heap, -pay)
  if len(heap) != 0:
    ans += -heappop(heap)

print(ans)

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

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

job.sort(reverse = True)

for x in job:
    s,t = x
    heapq.heappush(salary,t)
    if s - m > 0:
        heapq.heappop(salary)
    else:
        m -= 1

print(sum(salary))

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):
from heapq import heappop, heappush
import sys
input = sys.stdin.readline

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
from collections import defaultdict
 
dc = defaultdict(list)
n,m = map(int,input().split())
for i in range(n):
    a,b = map(int,input().split())
    dc[a].append(b)

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

print(ans)

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


def main(n, m, works):
    backward = sorted(works, key=itemgetter(0), reverse=True)
    q = []
    score = 0
    for i in range(1, m + 1):
        while backward and backward[-1][0] <= i:
            a, b = backward.pop()
            heappush(q, -b)
            
        if q:
            b = -heappop(q)
            score += b

    return score


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

print(main(n, m, works))

Solution (Python):
import heapq
N, M = map(int, input().split())
W = [tuple(map(int, input().split())) for i in range(N)]
W.sort()
h = []

j = 0
ans = 0
for i in range(M+1):
    while j < N and W[j][0] < i+1:
        heapq.heappush(h, (-W[j][1], W[j][1]))
        j += 1
    if h:
        p = heapq.heappop(h)
        ans += p[1]

print(ans)

Solution (Python):
from heapq import heappush, heappop
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.keys():
    jobs[a] += [-b]
  else:
    jobs[a] = [-b]
    
profit = 0
days = jobs.keys()
potential_jobs = []

for elappsed_day in range(1, m + 1):
  if elappsed_day in days:
    for job in jobs[elappsed_day]:
      heappush(potential_jobs, job)
  if len(potential_jobs) > 0:
    profit += heappop(potential_jobs)

print(-profit)

Solution (Python):
import heapq
N,M=map(int,input().split())
l=[[] for i in range(M+1)]

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

ans=0
que=[]
for i in range(1,M+1):
  for j in l[i]:
    heapq.heappush(que,-j)
  if len(que)>0:
    
    ans-=heapq.heappop(que)
    
    
print(ans)

Solution (Python):
from heapq import heappush, heappop

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

list_work = []
for n in range(N):
  a,b = map(int,input().split())
  list_work.append([a,b])  

list_work.sort(reverse=True)
q = []
ans = 0


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

print(ans)

Solution (Python):

n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
ab=sorted(ab, key=lambda x:x[0])
import heapq
i=0
heap=[]
res=[]
for x in range(1,m+1):
	while(i<n):
		if ab[i][0]==x:
			heapq.heappush(heap,-ab[i][1])
			i+=1
		else:
			break
	if len(heap)>0:
		res.append(heapq.heappop(heap))
print(-sum(res))

Solution (Python):
import heapq


def solve(m, work_map):
    days = work_map.keys()
    ans = 0
    priority_queue = []

    for day in range(1, m + 1):
        if day in days:
            for value in work_map[day]:
                heapq.heappush(priority_queue, -value)
        if len(priority_queue):
            value = -heapq.heappop(priority_queue)
            ans += value

    return ans


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

    work_map = {}
    
    for i in range(n):
        day, value = list(map(int, input().split()))
        if day > m:
            continue
        if day in work_map.keys():
            work_map[day].append(value)
        else:
            work_map[day] = [value]

    ans = solve(m, work_map)
    print(ans)


main()

Solution (Python):
from operator import itemgetter
import sys
input = sys.stdin.readline

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

List.sort(key=itemgetter(0))
List_S = [0]
 
result = 0
p = 0

for i in range(1, M+1):
    add = 0
    add_list = []
    while p < N and List[p][0] == i:
        if List_S[len(List_S)-1] < List[p][1]:            
            List_S.append(List[p][1])
        else:
            add_list.append(List[p][1])
            add += 1
        p += 1
            
    if len(List_S) != 1:
        result += List_S.pop()
        if add > 0:
            List_S.extend(add_list)
            List_S.sort()
 
print(result)

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

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

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, M = read_int()

job_dict = defaultdict(lambda: list())
for i in range(N):
    d, r = read_int()
    job_dict[d].append(-r)


hq = []
heapq.heapify(hq)

def solve():
    ans = 0
    for i in range(1, M + 1):
        for j in job_dict[i]:
            heapq.heappush(hq, j)
        if hq:
            ans += - heapq.heappop(hq)
    return ans

if __name__ == "__main__":
    print(solve())

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

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, M = read_int()

job_dict = defaultdict(lambda: list())
for i in range(N):
    d, r = read_int()
    job_dict[d].append(-r)

hq = []
heapq.heapify(hq)

def solve():
    ans = 0
    for i in range(1, M + 1):
        for j in job_dict[i]:
            heapq.heappush(hq, j)
        if hq:
            ans += - heapq.heappop(hq)
    return ans

if __name__ == "__main__":
    print(solve())

Solution (Python):

import sys, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, M = read_int()

jobs = [[] for _ in range(M + 1)]
for i in range(N):
    d, r = read_int()
    if d <= M:
        jobs[d].append(-r)


hq = []
heapq.heapify(hq)

def solve():
    ans = 0
    for i in range(1, M + 1):
        for j in jobs[i]:
            heapq.heappush(hq, j)
        if hq:
            ans += - heapq.heappop(hq)
    return ans

if __name__ == "__main__":
    print(solve())

Solution (Python):

import sys, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, M = read_int()

jobs = [[] for _ in range(M + 1)]
for i in range(N):
    d, r = read_int()
    if d <= M:
        jobs[d].append(-r)


hq = []
heapq.heapify(hq)

def solve():
    ans = 0
    for i in range(1, M + 1):
        for j in jobs[i]:
            heapq.heappush(hq, j)
        if hq:
            ans += - heapq.heappop(hq)
    return ans

if __name__ == "__main__":
    print(solve())

Solution (Python):

import heapq
MOD = 10 ** 9 + 7

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

AB.sort(key=lambda x: -x[0])

ans = 0
hq = []
for i in range(M + 1):
    while len(AB) != 0 and AB[-1][0] <= i:
        ab = AB.pop()
        ba = [-ab[1], ab[0]]
        heapq.heappush(hq, ba)

    if len(hq) != 0:
        ans += heapq.heappop(hq)[0]

print(-ans)

Solution (Python):
from heapq import heappush, heappop
import functools
def sorted_by(a, b):
    if a[0] > b[0]:
        return a[0] - b[0]
    if a[0] == b[0]:
        return b[1] - a[1]
    return -1

def solve(n, m, a, b):
    v = sorted(zip(a, b), key=functools.cmp_to_key(sorted_by))
    i = 0
    q = []
    ans = 0
    for t in range(1, m+1):
        while (i < n) and (v[i][0] <= t):
            heappush(q, -v[i][1])
            i += 1
        if len(q) > 0:
            ans -= heappop(q)
    return ans

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())
print(solve(n, m, a, b))

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*99*M
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 sys import stdin
import numpy as np
import heapq



def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


N, M = _li()

a_list = []
b_list = []
for _ in range(N):
    a, b = _li()
    a_list.append(a)
    b_list.append(b)

a_list = np.array(a_list)
b_list = np.array(b_list)
index = np.argsort(a_list)
a_list = a_list[index]
b_list = b_list[index]

ans = 0
cand = []
start = 0
for i in range(1, M+1):
    for j in range(start, N):
        if a_list[j] <= i:
            heapq.heappush(cand, -b_list[j])
            start = j+1
        else:
            break
    if len(cand) > 0:
        ans -= heapq.heappop(cand)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from heapq import *
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
job = defaultdict(list)
for _ in range(N):
	A, B = MAP()
	job[A].append(-B)

ans = 0
l = []

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

print(ans)

Solution (Python):
from heapq import *
n,m=map(int,input().split())
ss={}
it=[0]*n
for i in range(n):
    a,b=map(int,input().split())
    it[i]=b
    try:
        ss[m-a].append(i)
    except:
        ss[m-a]=[i]
heap=[]
tot=0
for i in range(m-1,-1,-1):
    try:
        x=ss[i]
        for i in x:
            heappush(heap,-it[i])
    except:
        pass
    if heap==[]:
        continue
    a=heappop(heap)
    tot-=a
print(tot)