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


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans -= heapq.heappop(hq) if len(hq) > 0 else 0
            count -= 1
            cap += 1
    ans += sum([-heapq.heappop(hq) for _ in range(count) if len(hq) > 0])
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
import heapq


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans += -heapq.heappop(hq) if len(hq) > 0 else 0
            count -= 1
            cap += 1
    ans += sum([-heapq.heappop(hq) for _ in range(count) if len(hq) > 0])
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
import heapq


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans -= heapq.heappop(hq) if len(hq) > 0 else 0
            count -= 1
            cap += 1
    ans -= sum([heapq.heappop(hq) if len(hq) > 0 else 0 for _ in range(count)])
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
import heapq


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans += -heapq.heappop(hq) if len(hq) > 0 else 0
            count -= 1
            cap += 1
    ans += sum([-heapq.heappop(hq) if len(hq) > 0 else 0 for _ in range(count)])
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
from heapq import *
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+1:
    l[a].append(b)

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

Solution (Python):
from heapq import heappush, heappop

n, m = map(int, input().split())
jobs = {}
for i in range(n):
    a, b = map(int, input().split())
    if a > m:
        continue
    k = m - a
    if k not in jobs:
        jobs[k] = []
    jobs[k].append(b)
h = []
ans = 0
for i in range(m - 1, -1, -1):
    if i in jobs:
        for b in jobs[i]:
            heappush(h, -b)
    if len(h) > 0:
        ans += -heappop(h)
print(ans)

Solution (Python):
import heapq

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

for i in range(N):
    A, B = map(int, input().split())
    l[i] = [A,-B]
l.sort()

tmp = []
ans = 0
j = 0

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

Solution (Python):
from heapq import heappop, heappush
N, M = map(int, input().split())
X = sorted([list(map(int, input().split())) for _ in range(N)], key = lambda x: x[0])

hq = []
ans, j = 0, 0

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

print(ans)

Solution (Python):
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


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq = []
    index, cap, count = 0, 1, m
    ans = 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            if len(hq) > 0:
                ans += -heapq.heappop(hq)
            count -= 1
            cap += 1
    for _ in range(count):
        if len(hq) > 0:
            ans += -heapq.heappop(hq)
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 




 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    heapify(ab)
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        heappush(ab, (a,b))
    
    ans = 0
    pq = []
    heapify(pq)
    for i in range(1,M+1):
        while True:
            if len(ab) == 0 or ab[0][0] != i:
                break
            a,b = heappop(ab)
            heappush(pq, -b)
            
        if len(pq) > 0:
            ans = ans + (heappop(pq) * (-1))
        
    
    print(ans)
    
if __name__ == "__main__":
	main()

Solution (Python):
from heapq import *
n,m = map(int,input().split())
t = [[] for i in range(10**5+1)]
for i in range(n):
    a,b = map(int,input().split())
    t[a].append(b)

ans = 0
h = []
for i in range(1,m+1):
    
    for j in t[i]:
        heappush(h, -j)
        
    if h:
        a = -heappop(h)
        
        ans += a
print(ans)

Solution (Python):


import heapq

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

l.sort()


L = [[] for _ in range(M+1)]
for i in range(N):
    if l[i][0] <= M:
        L[l[i][0]].append(l[i][1]) 




a = []
ans = 0
for i in range(M+1):
    for j in range(len(L[i])):
        heapq.heappush(a, -L[i][j])
    if len(a) != 0:
        ans += heapq.heappop(a)

print(-ans)

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

def sol():
    n,m=map(int,input().split())
    d=[]
    for i in range(n):
        a,b=map(int,input().split())
        
        if a<=m: heapq.heappush(d,(a,b))
    
    q=[]
    ans=0
    for i in range(1,m+1):
        
        while d and d[0][0]<=i:
            
            a,b=heapq.heappop(d)
            heapq.heappush(q,-b)
        if q:
            ans += -heapq.heappop(q)
    print(ans)

if __name__=="__main__":
    sol()

Solution (Python):
import heapq

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

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

ans = 0
pay = []

for day in range(1, m+1):
    while table and table[-1][0] <= day:
        tmp = table.pop()
        heapq.heappush(pay, -tmp[1])
   
    if pay:
        ans -= heapq.heappop(pay)

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:
        AB.append([a,b])
    
AB.sort(reverse=True)
List = []

ans = 0
for i in range(1,M+1):
    while AB and AB[-1][0] <= i:
        tmp = AB.pop()
        heapq.heappush(List,-tmp[1])

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

Solution (Python):
import heapq
NM = input()
N = int(NM.split(' ')[0])
M = int(NM.split(' ')[1])
each_days_list = [[] for _ in range(M)]
 
for i in range(N):
    ab = input()
    a = int(ab.split(' ')[0])
    b = int(ab.split(' ')[1])
    if a <= M:
        each_days_list[a-1].append(b)

max_list = []
salary_count = 0
for i in range(M):
    for j in range(len(each_days_list[i])): 
        heapq.heappush(max_list,-each_days_list[i][j])
    if len(max_list) > 0:
        salary_count += -heapq.heappop(max_list)
        
print(salary_count)

Solution (Python):

import heapq
import sys
input=sys.stdin.readline

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

if __name__=="__main__":
    sol()

Solution (Python):
import heapq
n, m = map(int, input().split())
ab_list = [list(map(int, input().split())) for i in range(n)]
ab_list.sort()
idx = 0
q = []
ans = 0
for j in range(1, m+1):
    while idx < n:
        a, b = ab_list[idx]
        if a <= j:
            heapq.heappush(q, -b)
            idx += 1
        else:
            break
    if len(q) > 0:
        b = heapq.heappop(q)
        ans -= b
print(ans)

Solution (Python):


import heapq

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

works = {}
for _ in range(N):
    a, b = map(int, input().split())
    works.setdefault(a, []).append(b)

ans = 0
heap = []

for i in range(M+1):
    if i in works:
        [heapq.heappush(heap, work) for work in map(lambda x: x * -1, works[i])]
    if heap:
        ans += heapq.heappop(heap) * -1

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 > m:
    continue
  jobs[m-a].append(b)

que = []
ans = 0
for i in range(m-1, -1, -1):
  for wage in jobs[i]:
    heapq.heappush(que, wage*-1)
  if len(que):
    ans += heapq.heappop(que)*-1
    
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 not a in ab:
        ab[a] = [-b]
    else:
        ab[a].append(-b)


q = []
ans = 0
for day in range(1, M+1):
    if day in ab:
        for i in ab[day]:
            heapq.heappush(q, i)

    if q != None and len(q) != 0:
        ans += -1 * heapq.heappop(q)

print(ans)

Solution (Python):
import heapq
n,m = map(int,input().split())
AB =[tuple(map(int,input().split())) for i in range(n)]
ans,memo=0,0
hq=[]
AB.sort(key=lambda tup:tup[0])
for day in range(m):
	for i in range(memo,n):
		if AB[i][0]<=(day+1):
			heapq.heappush(hq,-AB[i][1])
			memo+=1
		else:
			break
	if len(hq):	
		ans += -heapq.heappop(hq)
print(ans)

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: jobs[a-1].append(b)
h=[]
rsl=0
for i in range(M):
    for j in jobs[i]:
        heapq.heappush(h, -j)
    if len(h) > 0:
        rsl += -heapq.heappop(h)
print(rsl)

Solution (Python):
import heapq

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

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

Q=[]
heapq.heapify(Q)
l=0
ans=0
for i in range(1,M+1):
    for j in range(l,N):
        if L[j][0]==i:
            heapq.heappush(Q,[L[j][1],L[j][0]])
            
            l+=1
        else:
            break
    
    if len(Q)==0:
        pass
    else:
        ans-=Q[0][0]
        
        heapq.heappop(Q)
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 sys
import heapq
input = sys.stdin.readline
hq = []
heapq.heapify(hq)

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

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

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

import heapq

n, m = map(int, input().split())
ab = []

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

l = []

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

print(ans)

Solution (Python):
import heapq
n,m = map(int,input().split())
j = []
for i in range(n):
    j1 = list(map(int,input().split()))
    j.append(j1)
j.sort(key=lambda x:x[1],reverse=True)
j.sort(key=lambda x:x[0])
day = 1
i = 0
q = []
ans = 0
count = 0
while day <= m and i < n:
    if day == j[i][0]:
        heapq.heappush(q,-(j[i][1]))
        ans = ans + heapq.heappop(q)*(-1)
        day = day + 1
        i = i + 1
    elif day > j[i][0]:
        heapq.heappush(q,-(j[i][1]))
        i = i + 1
        count = count + 1
    else:
        day = day + 1
        if count > 0:
            ans = ans + heapq.heappop(q)*(-1)
            count = count - 1
while day <= m and count > 0:
        ans = ans + heapq.heappop(q)*(-1)
        day = day + 1
        count = count - 1
print(ans)

Solution (Python):
from heapq import heappop, heappush

n,m=map(int,input().split())
a=[]
for i in range(n):
    heappush(a,list(map(int,input().split())))
heap=[]
s=0
for i in range(m):
    while 1:
        if len(a)==0:break
        else:
            if a[0][0]==i+1:heappush(heap,-heappop(a)[1])
            else:break
    if len(heap)>0:s+=-heappop(heap)
print(s)

Solution (Python):

import heapq

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

for i in range(N):
    AB[i] = [int(x) for x in input().split()]








candidates = []

nextpush = 0




AB = sorted(AB)


answer = 0

for day in range(M)[::-1]:
    while nextpush < N:
        a, b = AB[nextpush]
        if day + a == M:
            
            heapq.heappush(candidates, -b)
            nextpush += 1
        else:
            
            
            break
    if candidates: 
        b = heapq.heappop(candidates)
        
        answer -= b

print(answer)

Solution (Python):
import math, heapq
from operator import itemgetter as ig
from collections import defaultdict as dd

INF = float("inf")
MOD = int(1e9 + 7)

class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)

def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2

def bsr(a, v, lo=0, hi=None):
    if hi == None:
        hi = len(a) - 1
    if hi < lo:
        return lo
    mi = (lo + hi) // 2
    if v < a[mi]:
        return bsr(a, v, lo, mi - 1)
    else:
        return bsr(a, v, mi + 1, hi)

class uft:
    def __init__(self, n):
        self.height = [1] * n
        self.group = [-1] * n
    def root(self, v):
        if self.group[v] < 0:
            return v
        self.group[v] = self.root(self.group[v])
        return self.group[v]
    def size(self, v):
        return - self.group[self.root(v)]
    def merge(self, v1, v2):
        v1, v2 = self.root(v1), self.root(v2)
        if v1 == v2:
            return
        if self.height[v1] < self.height[v2]:
            self.group[v2] += self.group[v1]
            self.group[v1] = v2
            self.height[v2] = max(self.height[v1] + 1, self.height[v2])
        else:
            self.group[v1] += self.group[v2]
            self.group[v2] = v1
            self.height[v1] = max(self.height[v1] , self.height[v2] + 1)

class graph:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
    def append(self, v1, v2, cost=1):
        self.graph[v1].append((v2, cost))
    
    def dks(self, v):
        costs = [INF] * self.n
        costs[v] = 0
        done = [False] * self.n
        heap = heapque((0, v))
        while heap.que:
            c_cost, c_index = heap.pop()
            if done[c_index]:
                continue
            done[c_index] = True
            for n_index, n_cost in self.graph[c_index]:
                if c_cost + n_cost < costs[n_index]:
                    costs[n_index] = c_cost + n_cost
                    heap.push((costs[n_index], n_index))
        return costs

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

    heap = heapque()
    v = 0
    for m in range(1, M + 1):
        if m in ab:
            for b in ab[m]:
                heap.push(-b)
        if heap.que:
            v += -heap.pop()
    print(v)

main()

Solution (Python):
import heapq

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

for i in range(N):
    AB[i] = [int(x) for x in input().split()]

AB = sorted(AB)

jobs = set()
answer = 0
nextpush = 0
candidates = []

for day in range(M)[::-1]:
    while nextpush < N:
        a, b = AB[nextpush]
        if day + a <= M:
            heapq.heappush(candidates, (-b, a))
            nextpush+=1
        else:
            break
    if candidates:
        b, a = heapq.heappop(candidates)
        answer -= b
print(answer)

Solution (Python):
import math, heapq
from operator import itemgetter as ig
from collections import defaultdict as dd

INF = float("inf")
MOD = int(1e9 + 7)

class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)

def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2

def bsr(a, v, lo=0, hi=None):
    if hi == None:
        hi = len(a) - 1
    if hi < lo:
        return lo
    mi = (lo + hi) // 2
    if v < a[mi]:
        return bsr(a, v, lo, mi - 1)
    else:
        return bsr(a, v, mi + 1, hi)

class uft:
    def __init__(self, n):
        self.height = [1] * n
        self.group = [-1] * n
    def root(self, v):
        if self.group[v] < 0:
            return v
        self.group[v] = self.root(self.group[v])
        return self.group[v]
    def size(self, v):
        return - self.group[self.root(v)]
    def merge(self, v1, v2):
        v1, v2 = self.root(v1), self.root(v2)
        if v1 == v2:
            return
        if self.height[v1] < self.height[v2]:
            self.group[v2] += self.group[v1]
            self.group[v1] = v2
            self.height[v2] = max(self.height[v1] + 1, self.height[v2])
        else:
            self.group[v1] += self.group[v2]
            self.group[v2] = v1
            self.height[v1] = max(self.height[v1] , self.height[v2] + 1)

class graph:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
    def append(self, v1, v2, cost=1):
        self.graph[v1].append((v2, cost))
    
    def dks(self, v):
        costs = [INF] * self.n
        costs[v] = 0
        done = [False] * self.n
        heap = heapque((0, v))
        while heap.que:
            c_cost, c_index = heap.pop()
            if done[c_index]:
                continue
            done[c_index] = True
            for n_index, n_cost in self.graph[c_index]:
                if c_cost + n_cost < costs[n_index]:
                    costs[n_index] = c_cost + n_cost
                    heap.push((costs[n_index], n_index))
        return costs

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

    ab = sorted(AB)
    heap = heapque()
    v, i = 0, 0
    for m in range(1, M + 1):
        while i < N:
            if ab[i][0] <= m:
                heap.push(-ab[i][1])
                i += 1
            else:
                break
        if heap.que:
            v += -heap.pop()
    print(v)

main()

Solution (Python):
import math, heapq
from operator import itemgetter as ig
from collections import defaultdict as dd

INF = float("inf")
MOD = int(1e9 + 7)

class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)

def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2

def bsr(a, v, lo=0, hi=None):
    if hi == None:
        hi = len(a) - 1
    if hi < lo:
        return lo
    mi = (lo + hi) // 2
    if v < a[mi]:
        return bsr(a, v, lo, mi - 1)
    else:
        return bsr(a, v, mi + 1, hi)

class uft:
    def __init__(self, n):
        self.height = [1] * n
        self.group = [-1] * n
    def root(self, v):
        if self.group[v] < 0:
            return v
        self.group[v] = self.root(self.group[v])
        return self.group[v]
    def size(self, v):
        return - self.group[self.root(v)]
    def merge(self, v1, v2):
        v1, v2 = self.root(v1), self.root(v2)
        if v1 == v2:
            return
        if self.height[v1] < self.height[v2]:
            self.group[v2] += self.group[v1]
            self.group[v1] = v2
            self.height[v2] = max(self.height[v1] + 1, self.height[v2])
        else:
            self.group[v1] += self.group[v2]
            self.group[v2] = v1
            self.height[v1] = max(self.height[v1] , self.height[v2] + 1)

class graph:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
    def append(self, v1, v2, cost=1):
        self.graph[v1].append((v2, cost))
    
    def dks(self, v):
        costs = [INF] * self.n
        costs[v] = 0
        done = [False] * self.n
        heap = heapque((0, v))
        while heap.que:
            c_cost, c_index = heap.pop()
            if done[c_index]:
                continue
            done[c_index] = True
            for n_index, n_cost in self.graph[c_index]:
                if c_cost + n_cost < costs[n_index]:
                    costs[n_index] = c_cost + n_cost
                    heap.push((costs[n_index], n_index))
        return costs

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

    ab = dd(list)
    for a, b in AB:
        ab[a] += [b]
    heap = heapque()
    v = 0
    for m in range(1, M + 1):
        if m in ab:
            for b in ab[m]:
                heap.push(-b)
        if heap.que:
            v += -heap.pop()
    print(v)

main()

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

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

for _ in range(N):
  a,b = map(int,input().split())
  d[a].append(b)
  
h = []
res = 0

for a in range(1,M + 1):
  for b in d[a]:
    heappush(h,-b)
  if len(h) > 0:
    c = heappop(h)
    res -=c
    
print(res)

Solution (Python):
import heapq as hp

n,m = map(int,input().split())
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
j = 0
list = sorted(list)

for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      hp.heappush(temp,-list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans -= hp.heappop(temp)
print(ans)

Solution (Python):
import heapq as hp
import sys
def input():
    return sys.stdin.readline()

n,m = map(int,input().split())
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
j = 0
list = sorted(list)

for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      hp.heappush(temp,-list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans -= hp.heappop(temp)
print(ans)

Solution (Python):
import numpy as np
import heapq

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


AB = sorted(AB, reverse = True)



H = []
sum_c = 0
for m in range(1, M + 1):
    while AB and AB[-1][0] <= m:
        c = AB.pop()[1]
        heapq.heappush(H, -c)

    if H:
        sum_c -= heapq.heappop(H)

print (sum_c)

Solution (Python):
import heapq
class Heapq :
    def __init__(self, arr, desc=False) :
        if desc :
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)
    def pop(self) :
        return heapq.heappop(self.hq) * self.sign
    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)
    def top(self):
        return self.hq[0] * self.sign
    def size(self):
        return len(self.hq)

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

q = Heapq([], True)
ans = 0

for a in range(1,M+1) :
    for b in D[a] :
        q.push(b)
    if q.size() > 0 :
        ans += q.pop()
print(ans)

Solution (Python):
from heapq import heappush, heappop

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

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


queue = []
ans = 0
for i in range(M) :
    for j in range(len(AB[i])) :
        heappush(queue, -AB[i][j])

    if len(queue) != 0 :
        ans += -heappop(queue)

print(ans)


'''
ans = 0

for i in range(1, M + 1) :
    maxIncome = 0
    anker = -1
    for k in ABkeys :
        
        if k <= i :
            if heappop(AB[k]) > maxIncome :
                maxIncome = AB[j][0]
                anker = k
                
                continue
        else :
            break
    ans += maxIncome
    if len(AB) == 0 :
        break
    if anker != -1:
        AB[anker].pop(0)
        if len(AB[anker]) == 0 :
            AB.remove


print(ans)
'''
'''
N, M = map(int, input().split())
AB = []
for i in range(N) :
    AB.append(list(map(int, input().split())))

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

ans = 0

for i in range(1, M + 1) :
    maxIncome = 0
    anker = -1
    for j in range(len(AB)) :
        
        if AB[j][0] <= i :
            if AB[j][1] > maxIncome :
                maxIncome = AB[j][1]
                anker = j
                
                continue
        else :
            break
    ans += maxIncome
    if len(AB) == 0 :
        break
    if anker != -1:
        AB.pop(anker)

print(ans)
'''

Solution (Python):
import heapq
N,M = [ int(x) for x in input().split()]

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

i = 0
memo = []

for x in range(M-1, -1, -1):
    allaw = M - x
    while i < N:
        ab = AB[i]
        limit, reward = ab
        if limit > allaw:
            
            break
        else:
            
            heapq.heappush(memo, -reward)
            i += 1
    if memo:
        
        result += -heapq.heappop(memo)
print(result)

Solution (Python):
from collections import defaultdict
import heapq

dct=defaultdict(list)

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

hq=[]
val=0

for i in range(1,m+1):
  if dct[i]:
    for item in dct[i]:
      heapq.heappush(hq,item)
  if hq:
    val+=heapq.heappop(hq)

print(-val)

Solution (Python):
import heapq

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

ans = 0
A = []
for i in range(1, M + 1):
    while AB:
        if AB[-1][0] <= i:
            cand = - AB.pop(-1)[1]
            heapq.heappush(A, cand)
        else:
            break
    
    if A:
        ans += - heapq.heappop(A)
    
print(ans)

Solution (Python):
import math, heapq
from operator import itemgetter
from collections import defaultdict
INF = float("inf")
MOD = int(1e9 + 7)

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

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

    v, que = 0, []
    for m in range(1, M + 1):
        if m in AB:
            for b in AB[m]:
                heapq.heappush(que, -b)
        if que:
            v += -heapq.heappop(que)
    print(v)

main()

Solution (Python):
import heapq
import collections

n, m = map(int, input().split())
work = collections.defaultdict(list)
hp = [[] for _ in range(m+1)]
ignore = []
ans = 0
for _ in range(n):
    x, y = map(int, input().split())
    work[x].append(-y)
candidate = []
for x in range(1, m+1):
    if work[x]:
        for e in work[x]:
            heapq.heappush(candidate, e)
    if candidate:
        ans += heapq.heappop(candidate)
print(abs(ans))