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

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import *
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b = LI()
    print(max(a + b, a - b, a * b))
    return


def B():
    k, x = LI()
    ans = []
    for i in range(x - k + 1, x + k):
        ans.append(i)
    print(*ans)
    return


def C():
    n = II()
    d = defaultdict(int)
    for _ in range(n):
        s = S()
        s.sort()
        d[tuple(s)] += 1
    ans = 0
    for i in d.values():
        ans += i * (i - 1) // 2
    print(ans)
    return





def D():
    n, m = LI()
    q = []
    d = defaultdict(list)
    ans = 0
    for _ in range(n):
        a, b = LI()
        d[a].append(b)
    for i in range(m):
        for di in d[i+1]:
            heappush(q, -di)
        if q:
            ans -= heappop(q)
    print(ans)
    return


def E():
    return


def F():
    return


if __name__ == '__main__':
    D()

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

N, M = map(int, input().split())
W = [list(map(int, input().split())) for i in range(N)]
W.sort()
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)

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

N, M = map(int, input().split())
W = []
for i in range(N):
  a = list(map(int, input().split()))
  heappush(W, a)
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)

Solution (Python):
from heapq import heappop, heappush

N, M = map(int, input().split())
W = []
for i in range(N):
  a = list(map(int, input().split()))
  heappush(W, a)
  
ans = 0
C = []

for i in range(1,M+1):
  j = -i
  while W != [] and W[0][0] <= i:
    a,b = heappop(W)
    heappush(C,-b)
  if C != []:
    ans += -heappop(C)
print(ans)

Solution (Python):
import heapq

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

Days = [[] for i in range(m)]
for i in range(n):
    if Jobs[i][0] <= m:
        Days[Jobs[i][0]-1] += [[-Jobs[i][1],Jobs[i][0]]]

Heap = []
heapq.heapify(Heap)
ans = 0
for i in range(m):
    for job in Days[i]:
        heapq.heappush(Heap,job)
    if Heap:
        ans -= heapq.heappop(Heap)[0]
        
print(ans)

Solution (Python):
import numpy
import heapq
import math

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

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

print(rew)

Solution (Python):
from heapq import heappush, heappop
from collections import deque
n,m=map(int,input().split())
data=[[] for _ in range(m+1)]
for _ in range(n):
    a,b=map(int,input().split())
    if a<=m:
        data[a].append([-b,a])
for i in range(1,m+1):
    data[i].sort()
    data[i]=deque(data[i])
h=[]
s=0
for i in range(1,m+1):
    if data[i]:
        heappush(h,data[i].popleft())
    if h:
        d=heappop(h)
        if data[d[1]]:
            heappush(h,data[d[1]].popleft())
        s+=-d[0]
print(s)

Solution (Python):

import sys
import heapq
from collections import defaultdict

"""解説PDF
(1) 0日目から考えると、最初に選べるアルバイトが多すぎて、どれを適切に選んでいけばよいかわからない

(2) M日後から考えると、M日後からi日前には、A_i<=iとなるアルバイトしか選べないので、これで考えると貪欲的に解ける

"""
def solve():
    N, M = list(map(int, sys.stdin.readline().split()))
    jobs = defaultdict(lambda: [])

    
    for i in range(N):
        a, b = list(map(int, sys.stdin.readline().split()))
        jobs[a].append(b)

    ans = 0
    heap = []  
    for i in range(1, M+1):
        
        for b in jobs[i]:
            
            heapq.heappush(heap, -b)  

        if len(heap) != 0:
            
            ans += -heapq.heappop(heap)

    print(ans)


if __name__ == "__main__":
    solve()

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

jobs = []
available = []
for _ in range(N):
    temp = tuple(map(int, input().split()))
    jobs.append(temp)

heapq.heapify(jobs)
ans = 0
for i in range(1, M+1):
    while(len(jobs)):
        a, b = heapq.heappop(jobs)
        if a == i:
            heapq.heappush(available, -b)
        else:
            heapq.heappush(jobs, (a, b))
            break
    if len(available):
        val = heapq.heappop(available)
        ans += -val

print(ans)

Solution (Python):
import heapq

n, m = list(map(int, input().split()))
tasks = []
for i in range(n):
    ai, bi = list(map(int, input().split()))
    tasks.append((-bi, i, ai))
tasks.sort(key=lambda x: x[2])    
prtasks = []
money = 0
j = 0
for i in range(1,m+1):
    for k in range(j,len(tasks)):
        if tasks[k][2] <= i:
            heapq.heappush(prtasks, tasks[k])
            j += 1
        else:
            break
    if not prtasks:
        continue
    work = heapq.heappop(prtasks)
    money += work[0]

print(-money)

Solution (Python):
import heapq


def main():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    ab = sorted(ab)
    
    
    index = 0
    ans = []
    money = 0
    for day in range(1, m+1):
        
        while index < n and ab[index][0] <= day:
            heapq.heappush(ans, -ab[index][1])
            index += 1
            
        if ans:
            money -= heapq.heappop(ans)

    print(money)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq

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

baitos = sorted(baitos)



money = 0
avail = []

index = 0
for day in range(m):
    while index < n and baitos[index][0] <= day + 1:
        heapq.heappush(avail, -baitos[index][1])
        index += 1
        
    
    
    
    
    
    
    
    
    
    
    if avail:

        money += -heapq.heappop(avail)
        
    
    

print(money)

Solution (Python):
import heapq

n, m = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
ls.sort(key=lambda x: x[0])

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

Solution (Python):
import heapq

n, m = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
ls.sort(key=lambda x: x[0])

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

Solution (Python):
from heapq import heappop, heappush
n, m = map(int, input().split())
l = [[] for i in range(n)]
for i in range(n):
    a,b = map(int, input().split())
    c = min(m-a, n-1)
    if c < 0:
        continue
    heappush(l[c], -b)
heap = []
ans = 0
for i in range(n)[::-1]:
    for b in l[i]:
        heappush(heap, b)
    if heap:
        b = heappop(heap)
        ans += -b
print(ans)

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

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=1)

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

print(ans)

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

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=1)

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

print(ans)

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

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append((a,b))
ab.sort(reverse=1)

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

print(ans)

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

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append((a,b))
ab.sort()

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

print(ans)

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

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()

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

print(ans)

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

n,m = map(int,input().split())
ab = []
for line in sys.stdin:
    a,b = map(int,line.split())
    if a <= m:
        ab.append([a,b])
ab.sort()

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

print(ans)

Solution (Python):
import heapq
N, M = map(int,input().split())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
for i in range(N):
    A[i], B[i] = map(int,input().split())
ptj = {}
for i in range(N):
    if A[i] in ptj:
        ptj[A[i]].append(-B[i])
    else:
        ptj[A[i]] = [-B[i]]
count = 0
s = []
for i in range(1,M+1):
    if i in ptj:
        for j in ptj[i]:
            heapq.heappush(s,j)
    if s != []:
        count -= heapq.heappop(s)
print(count)

Solution (Python):
import heapq

def main():
    n, m, *ab = map(int, open(0).read().split())

    ab_dict = {}
    for a, b in zip(*[iter(ab)] * 2):
        if a > m:
            continue
        if a in ab_dict:
            ab_dict[a].append(-b)
        else:
            ab_dict[a] = [-b]

    cand = []
    ans = 0
    for day in range(1, m+1):
        if day in ab_dict:
            for cand_b in ab_dict[day]:
                heapq.heappush(cand, cand_b)
        if cand:
            ans += -heapq.heappop(cand)

    print(ans)
    return()

if __name__ == '__main__':
    main()

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
import logging
import heapq
logging.basicConfig(level=logging.DEBUG)

input = sys.stdin.readline

setrecursionlimit(1000000)

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

    jobs=defaultdict(list)
    for _ in range(N):
        a,b=[int(x) for x in input().split()]
        jobs[M-a].append(b)
    
    

    hq=[]
    heapq.heapify(hq)
    ans=0
    for day in reversed(range(M)):
        for job in jobs[day]:
            heapq.heappush(hq, -job)

        if hq:
            ans+=-heapq.heappop(hq)

    print(ans)


main()

Solution (Python):
import heapq

N,M=[int(s) for s in input().split()]
baito = []
for i in range(N):
  a,b=[int(s) for s in input().split()]
  heapq.heappush(baito, (-a,-b))
job=[]

while baito:
  x, y = heapq.heappop(baito)
  r = M-len(job)
  if -x > r:
    heapq.heappush(job, -y)
    heapq.heappop(job)       
  else:
    heapq.heappush(job, -y)
print(sum(job))

Solution (Python):



import heapq



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


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

job_pool = []
heapq.heapify(job_pool)
income = 0
for available_jobs in money_schedule:
    for salary in available_jobs:
        heapq.heappush(job_pool, -salary)

    if 0 < len(job_pool):
        income -= heapq.heappop(job_pool)

print(income)

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

Solution (Python):


import heapq


class MaxHeap:
    def __init__(self, li):
        self.heap = [-e for e in li]
        heapq.heapify(self.heap)

    def push(self, val):
        heapq.heappush(self.heap, -val)

    def pop(self):
        return -heapq.heappop(self.heap)

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


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

ab.sort()

h = MaxHeap([])

i = 0
ans = 0
for day in range(1, m + 1):
    while i < n and ab[i][0] <= day:
        h.push(ab[i][1])
        i += 1
    if len(h) == 0:
        continue
    ans += h.pop()

print(ans)

Solution (Python):
from collections import defaultdict
import heapq

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

dic=defaultdict(list)
for a,b in ab:
    dic[a]+=[-b]

li=[]
ans=0
for i in range(1,m+1):
    if dic[i]:
        for j in dic[i]:
            heapq.heappush(li,j)
    if li:
        ans+=heapq.heappop(li)
print(-ans)

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

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

s = []
while lis:
  x, y = heapq.heappop(lis)
  r = m-len(s)
  if -x > r:
    heapq.heappush(s, -y)
    heapq.heappop(s)
  else:
    heapq.heappush(s, -y)
print(sum(s))

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

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

for i in range(1, m+1):
    for x in day[i]:
        heapq.heappush(q, x)
    if q != []:
        ans += -heapq.heappop(q)

print(ans)

Solution (Python):
import heapq
import bisect

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

ans=0
ab.sort()
_index=[i[0] for i in ab]
p=[]
heapq.heapify(p)

for i in range(1,M+1):
	for g in ab[bisect.bisect_left(_index, i) : bisect.bisect_right(_index, i)]:
		heapq.heappush(p, -g[1])
	if p!=[]:
		ans+=heapq.heappop(p)*(-1)

print(ans)

Solution (Python):
import heapq

N,M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
d_AB = {a:[] for a,b in AB}
for a,b in AB:
    d_AB[a].append(-b)
H = []
money = 0
for i in range(1,M+1):
    if i in d_AB.keys():
        for ab in d_AB[i]:
            heapq.heappush(H,ab)
        money += heapq.heappop(H)
    else:
        money += heapq.heappushpop(H,0)
print(-money)

Solution (Python):
import heapq

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


d_AB = {a:[] for a,b in AB}
for a,b in AB:
    d_AB[a].append(-b)

H = []
heapq.heapify(H)
money = 0
for i in range(1,M+1):
    if i in d_AB.keys():
        for ab in d_AB[i]:
            heapq.heappush(H,ab)
        
        money += heapq.heappop(H)
        
        
    else:
        money += heapq.heappushpop(H,0)

print(-money)

Solution (Python):
from bisect import bisect_left as bisect

def main():
    n, m, *ab = map(int, open(0).read().split())

    rate = []
    for a, b in zip(*[iter(ab)] * 2):
        if a > m:
            continue
        rate.append([a, b])

    rate = sorted(rate, key=lambda x: -x[1])

    day = list(range(1, m + 1))
    day_len = m
    ans = 0
    for term, money in rate:
        work_day = bisect(day, term)
        if work_day == day_len:
            continue
        else:
            del day[work_day]
            day_len -= 1
            ans += money

    print(ans)
    return()

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import heapq
import sys

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

print(sum(ans))

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)

from collections import defaultdict
d = defaultdict(list)

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

q = Heapq([], True)
 
ans = 0
for i in range(1,m+1):
    for b in d[i]:
        q.push(b)
    if q.size():
        ans += q.pop()
print(ans)

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


def main():
    N, M = map(int, input().split())
    Q = []
    for _ in range(N):
        a, b = map(int, input().split())
        heapq.heappush(Q, [a, b])
    
    ans = 0
    P = []
    for i in range(1, M+1):
        if len(Q)!=0:
            a, b = heapq.heappop(Q)
            while a==i:
                heapq.heappush(P, [b*(-1), a])
                if len(Q)==0:
                    break
                a, b = heapq.heappop(Q)
            heapq.heappush(Q, [a, b])
        if len(P)!=0:
            b, a = heapq.heappop(P)
            ans += b*(-1)
    
    print(ans)


if __name__ == '__main__':
    main()

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


def main():
    N, M = map(int, input().split())
    Q = []
    for _ in range(N):
        a, b = map(int, input().split())
        heapq.heappush(Q, [a, b])
    
    ans = 0
    P = []
    for i in range(1, M+1):
        if len(Q)!=0:
            a, b = heapq.heappop(Q)
            while a==i:
                heapq.heappush(P, [b*(-1), a])
                if len(Q)==0:
                    break
                a, b = heapq.heappop(Q)
            heapq.heappush(Q, [a, b])
        if len(P)!=0:
            b, a = heapq.heappop(P)
            ans += b*(-1)
    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import heapq


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


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

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

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

        if not hq:
            continue

        ans += -heapq.heappop(hq)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq
from collections import defaultdict

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


work = defaultdict(list)

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




current_work = []
ans = 0
for i in range(M):
    if i in work.keys():
        for j in work[i]:
            
            heapq.heappush(current_work, j)

    if current_work:
        
        ans += -1 * heapq.heappop(current_work)

print(ans)

Solution (Python):
import heapq
from collections import defaultdict

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


work = defaultdict(list)

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




current_work = []
ans = 0
for i in range(M):
    if i in work.keys():
        for j in work[i]:
            heapq.heappush(current_work, j)

    if current_work:
        
        ans += -1 * heapq.heappop(current_work)

print(ans)

Solution (Python):
from collections import defaultdict
import heapq

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

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

ans = 0
now_cand = []
for i in range(1, M+1):
    for job in daily_jobs[str(i)]:
        heapq.heappush(now_cand, job)
    if now_cand:
        ans += heapq.heappop(now_cand)

print(-ans)

Solution (Python):
from collections import defaultdict
import heapq

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

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

ans = 0
now_cand = []
for i in range(1, M+1):
    for job in daily_jobs[str(i)]:
        heapq.heappush(now_cand, job)
    if now_cand:
        ans += heapq.heappop(now_cand)

print(-ans)

Solution (Python):
import heapq
N, M = map(int, input().split())
ABs = []
for i in range(N):
  A, B = map(int, input().split())
  ABs.append((A, B))
ABs.sort()
r = 0
q = []
i = 0
for m in range(1, M+1):
  while i < N:
    A, B = ABs[i]
    if A <= m:
      heapq.heappush(q, -B)
      i += 1
    else:
      break
  if q:
    B = -heapq.heappop(q)
    r += B
print(r)

Solution (Python):
import heapq

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

heap_lst = []
heapq.heapify(heap_lst)
j = 0
res = 0

sorted_lst = sorted(lst, key = lambda x: x[0])
for i in range(1, m+1):
    while j < n and sorted_lst[j][0] <= i:
        a, b = sorted_lst[j]
        heapq.heappush(heap_lst, (-b, a))
        j += 1
    if len(heap_lst) == 0:
        continue
    else:
        minus_b, a = heapq.heappop(heap_lst)
        res += -minus_b
print(res)

Solution (Python):
import heapq


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


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


rewards = 0
heap = []


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

print(rewards)

Solution (Python):
import heapq

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

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

rewards = 0
heap = []


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

print(rewards)

Solution (Python):
from collections import deque
import heapq
n, m = map(int, input().split())
jobs = [None]*n
q = []
heapq.heapify(q)
day = 0
ans = 0

for i in range(n):
    jobs[i] = tuple(map(int, input().split()))
jobs.sort(key = lambda x:x[1], reverse=True)
jobs.sort(key = lambda x:x[0])
jobs = deque(jobs)

for i in range(m):
    while True:
        if  not len(jobs) == 0:
            j = jobs.popleft()
            if not j[0] == i+1:
                jobs.appendleft(j)
                break
            else:
                heapq.heappush(q, -j[1])
        else:
            break
    if q:
        ans -= heapq.heappop(q)
print(ans)

Solution (Python):
from collections import defaultdict
import heapq
n,m=map(int,input().split())
dic=defaultdict(list)
for i in range(n):
    a,b=map(int,input().split())
    dic[a]+=[-b]
    
li=[]
ans=0

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

Solution (Python):
from collections import defaultdict
import heapq
n,m=map(int,input().split())
dic=defaultdict(list)
for i in range(n):
    a,b=map(int,input().split())
    dic[a].append(-b)
    
li=[]
ans=0

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

Solution (Python):

from heapq import heappush, heappushpop


def main():
    N, M, *A = map(int, open(0).read().split())
    
    T = [[] for _ in range(M + 1)]
    for a, b in zip(*[iter(A)] * 2):
        if a <= M:
            T[M - a] += [b]
    q = []  
    for i in range(M + 1):
        for v in T[i]:
            
            if len(q) <= i:
                heappush(q, v)
            else:
                heappushpop(q, v)
    print(sum(q))


if __name__ == "__main__":
    main()

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


if __name__ == '__main__':
    slove()

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

     ans=0
     N,M=map(int,input().split())
     Ar=[]
     for _ in range(M):
           Ar.append([])
     for _ in range(N):
          A,B=map(int,input().split())
          if A <= M:
              Ar[A-1].append(-B)
     An=[]
     for i in range(M):
          for j in Ar[i]:
              heapq.heappush(An,j)
          if len(An)>0:
              ans+=-heapq.heappop(An)
     print(ans)

if __name__ =='__main__':
    main()

Solution (Python):
import heapq
a,b = map(int,input().split())
L = []
for _ in range(a):
    L.append( tuple(map(int,input().split())) )
L = sorted(L)
h = []
c = 0
ans = 0
for i in range(1,b+1):
    while True:
        if c >= len(L): break
        if L[c][0] <= i:
            heapq.heappush(h,-L[c][1])
            c += 1
        else:
            break
    try :
        ans += -heapq.heappop(h)
    except:
        pass
print(ans)

Solution (Python):
import heapq

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



tasks=[[] for _ in range(10**5+1)]
for i in range(N):
  tasks[A[i]-1].append(B[i])


pq=[]
ans=0
for i in range(M):
  for b in tasks[i]:
    heapq.heappush(pq,-b)
  if len(pq)!=0:
    temp=heapq.heappop(pq)
    ans-=temp
print(ans)

Solution (Python):
from heapq import heappush, heappop

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


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

Solution (Python):
from heapq import heappush, heappop


def main():
    n, m = map(int, input().split())
    jobs = [list(map(int, input().split())) for _ in range(n)]

    jobs.sort(key=lambda x: x[0], reverse=True)

    hq = []
    ans = 0

    for i in range(1, m+1):
        for j in range(len(jobs)-1, -1, -1):
            if jobs[j][0] != i:
                break
            heappush(hq, jobs[j][1]*-1)
            jobs.pop()
        if len(hq) > 0:
            ans += heappop(hq)*-1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq

def main():
    [N, M] = [int(number) for number in input().split()]
    part_times = []
    for i in range(N):
        job= [int(number) for number in input().split()]
        part_times.append(job)
    part_times = [part_time for part_time in sorted(part_times, key = lambda x: x[0])] 
    money = 0
    hp = []
    for day in range(M):
        while part_times != [] and part_times[0][0] <= day + 1:
            part_time = part_times[0]
            heapq.heappush(hp, - part_time[1])
            part_times.pop(0)
        if hp != []:
            money -= heapq.heappop(hp)

    print(money)

if __name__ == '__main__':
    main()

Solution (Python):
"""
N, M = map(int, input().split())
AB = []
for i in range(N):
    a, b =  (int(x) for x in input().split())
    AB.append([a, b])
AB.sort(key=lambda x: x[1])

dic = {}
for i in range(M):
"""

import heapq

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


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

ans = 0
q = [] 


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

print(ans)