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)

def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappush, heappop


def solve():
    n, m = MI()

    jobs = [[] for _ in range(m)]
    for i in range(n):
        a, b = MI()
        if m-a >= 0:
            jobs[m-a].append(b)

    
    hp = []
    heapify(hp)
    ans = 0
    for i in range(m-1, -1, -1):
        js = jobs[i]
        for j in js:
            heappush(hp, -j)

        if len(hp) > 0:
            ans += -heappop(hp)
    print(ans)





if __name__ == '__main__':
    solve()

Solution (Python):
def main():
  n,m=map(int,input().split())
  ab=[list(map(int,input().split())) for _ in range(n)]
  grp=[[]for _ in range(m+1)]
  for a,b in ab:
    if a>m:
      continue
    grp[a].append(b)
  for i,grpi in enumerate(grp):
    grpi.sort()
  import heapq
  maxs=[]
  heapq.heapify(maxs)
  ans=0
  for i in range(1,m+1):
    if len(grp[i])>0:
      heapq.heappush(maxs,[-grp[i].pop(),i])
    if len(maxs)>0:
      v,j=heapq.heappop(maxs)
      ans-=v
      if len(grp[j])>0:
        heapq.heappush(maxs,[-grp[j].pop(),j])

  print(ans)
if __name__=='__main__':
  main()

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)


from collections import Counter,deque,defaultdict




from heapq import heapify,heappush,heappop
n,m=n1()
AB=t3(n)

d=defaultdict(list)
for a,b in AB:
    d[a].append(b)

ans=0
h=[]
for i in range(1,m+1):
    while len(d[i])>0:
        heappush(h,-1*d[i].pop())
    if len(h)>0:
        ans+=-1*heappop(h)
print(ans)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)


from collections import Counter,deque,defaultdict




from heapq import heapify,heappush,heappop
n,m=n1()
AB=t3(n)

d=defaultdict(list)
for a,b in AB:
    d[a].append(b)

ans=0
h=[]
for i in range(1,m+1):
    while len(d[i])>0:
        heappush(h,-1*d[i].pop())
    if len(h)>0:
        ans+=-1*heappop(h)
print(ans)

Solution (Python):
import _heapq as heapq
n, m = [int(i) for i in input().split()]
ab = []
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    ab.append([a, -b])
ab.sort()
j = 0
ans=0
h = []
for i in range(1, m + 1):
    while j<n and ab[j][0] <= i:
        heapq.heappush(h, ab[j][1])
        j += 1
    if h:
        ans += heapq.heappop(h)
print(-ans)

Solution (Python):
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
N, M = map(int, input().split())
dates = [[] for _ in range(10**5+1)]
for i in range(N):
    x,y = map(int, input().split())
    dates[x].append(y)

h = []
heapify(h)
ans = 0
for i in range(1,M+1):
    for b in dates[i]:
        heappush(h,-b)
    if len(h):
        ans -= heappop(h)
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline









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


AB.sort(reverse=True)

import heapq
hq = []
ans = 0
for i in range(1,M+1):
    
    
    while AB and AB[-1][0] <= i:
        a,b = AB.pop()
        heapq.heappush(hq, -b)
    
    
    if hq:
        ans -= heapq.heappop(hq)

print(ans)

Solution (Python):
def main():
    from heapq import heappush as sushi
    from heapq import heappop as tempra
    n, m, *ab = map(int, open(0).read().split())
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i - m < 1:
            l[i].append(-j)

    w = []
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            sushi(h, j)
        if h:
            x = tempra(h)
            w.append(x)

    print(-sum(w))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from heapq import heappush as sushi
    from heapq import heappop as tempra
    n, m, *ab = map(int, open(0).read().split())
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            l[i].append(-j)

    w = []
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            sushi(h, j)
        if len(h) > 0:
            x = tempra(h)
            w.append(x)

    print(-sum(w))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq
    n, m, *ab = map(int, open(0).read().split())
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            heapq.heappush(l[i], -j)
    
    ans = 0
    h = []
    for i in range(1, m + 1):
        for j in l[i]:
            heapq.heappush(h, j)
        if len(h) > 0:
            x = heapq.heappop(h)
            ans += x

    print(-ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import heapq
    n, m, *ab = map(int, open(0).read().split())
    l = [[] for _ in range(m + 1)]
    for i, j in zip(*[iter(ab)] * 2):
        if i <= m:
            l[i].append(-j)

    ans = 0
    h = []
    for i in range(1, m + 1):
        g = l[i]
        for j in g:
            heapq.heappush(h, j)
        if len(h) > 0:
            x = heapq.heappop(h)
            ans += x

    print(-ans)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq

n,m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab.sort(key = lambda x : x[0])
q = []
heapq.heapify(q)
ind = 0
ans = 0
for i in range(1,m+1):
    ok = True
    while ok:
        if  ind <= n-1 and ab[ind][0] <= i:
            heapq.heappush(q, -1 * ab[ind][1])
            ind += 1
        else:
            ok = False
    if len(q) >= 1:
        ans -= heapq.heappop(q)
print(ans)

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

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

heap = []
salary = 0

for day in range(1, M + 1):
  for i in jobs[day]:
    heapq.heappush(heap, i)

  if len(heap) > 0:
    salary += abs(heapq.heappop(heap)) 

print(salary)

Solution (Python):
import heapq


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

Work.sort()
now = 1
Q = []
ans = 0
for day, reward in Work:
    if day > M:
        break
    if now > M:
        break
    if day == now:
        heapq.heappush(Q, -reward)
    else:
        while day - now > 1:
            if len(Q) > 0:
                a = heapq.heappop(Q)
                ans += a
            now += 1
        if len(Q) > 0:
            a = heapq.heappop(Q)
            ans += a
        heapq.heappush(Q, -reward)
        now = day
while M - now >= 0:
    if len(Q) > 0:
        a = heapq.heappop(Q)
        ans += a
    now += 1

print(-ans)

Solution (Python):

import sys
import heapq



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

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

    heap = []
    salary = 0

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

    print(salary)
    return

if __name__ == '__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import heapq
def main():
    n,m=map(int,input().split())
    AB=[tuple(map(int,input().split())) for i in range(n)]
    AB.sort(reverse=True)
    H=[]
    ans=0
    for i in range(1,m+1):
        while AB and AB[-1][0]<=i:
            a,b=AB.pop()
            heapq.heappush(H,(-b,a))
        if H:
            bb,aa=heapq.heappop(H)
            ans-=bb
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import sys
import math
import bisect
import heapq
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,M=nm()
Bl=[[] for i in range(10**5+5)]

for i in range(N):
    A,B=nm()
    Bl[A].append(-B)

now=[]
if(Bl[0]!=[]):
    for i in range(len(Bl[0])):
        heapq.heappush(now,Bl[0][i])

ans=0
for i in range(1,M+2):
    if(now!=[]):
        max_val=heapq.heappop(now)
        ans+=max_val
    if(Bl[i]!=[]):
        for j in range(len(Bl[i])):
            heapq.heappush(now,Bl[i][j])

print(-ans)

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

input = sys.stdin.buffer.readline
n, m = map(int, input().split())
AB = []
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda x: [-x[0], -x[1]])
ans = 0
kouho = []
for d in range(1, m + 1):
    while AB and AB[-1][0] <= d:
        a, b = AB.pop()
        heappush(kouho, (-b, a))
    if kouho:
        p, q = heappop(kouho)
        ans += -p
print(ans)

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

input = sys.stdin.buffer.readline
n, m = map(int, input().split())
AB = []
AB = [list(map(int, input().split())) for _ in range(n)]
AB.sort(key=lambda x: [-x[0], -x[1]])
ans = 0
kouho = []
for d in range(1, m + 1):
    while AB and AB[-1][0] <= d:
        a, b = AB.pop()
        heappush(kouho, (-b, a))
    if kouho:
        p, q = heappop(kouho)
        ans += -p
print(ans)

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

q = []
cnt = 0
for i in range(1, M+1):
    while len(AB) > 0 and AB[-1][0] <= i:
        tmp = AB.pop()
        heapq.heappush(q, -tmp[1])
    if len(q) > 0:
        max_val = -heapq.heappop(q)
        cnt += max_val

print(cnt)

Solution (Python):
from heapq import heappop, heappush

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

l.sort(reverse=True)

q=[]
ans=0

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

Solution (Python):
from heapq import heappop, heappush
n, m = map(int, input().split())
d = []
for _ in range(n):
    a, b = map(int, input().split())
    d.append((m - a, b))
d.sort()
ans = 0
q = []
for i in range(m - 1, -1, -1):
    while d and d[-1][0] == i:
        heappush(q, -d.pop()[1])
    if q:
        ans -= heappop(q)
print(ans)

Solution (Python):
N,M = map(int,input().split())
t = [[0] for _ in range(M+1)]
for _ in range(N):
    a,b = map(int,input().split())
    if a<=M:
        t[a].append(b)
import heapq
A=[-i for i in t[1]]
heapq.heapify(A)
ans = 0
for i in range(1,M+1):
    maxi = heapq.heappop(A)
    ans -= maxi
    if i+1<=M:
        for k in t[i+1]:
            heapq.heappush(A,-k)
print(ans)

Solution (Python):
from heapq import heappop,heappush
n,m=map(int,input().split())
L=[[] for _ in range(m)]
for _ in range(n):
  a,b=map(int,input().split())
  if a<=m:
    L[m-a].append(b)
s=0
h=[]
for i in range(m-1,-1,-1):
  for l in L[i][::-1]:
    heappush(h,-l)
  if len(h)>0:
    s-=heappop(h)
print(s)

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools
import heapq

readline = stdin.buffer.readline

INF = 10 ** 10


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




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

for _ in range(N):
    A, B = [int(x) for x in readline().split()]
    if A <= M:
        jobs[A].append(B)


pq = [] 

ans = 0


for i in range(1, M+1):
    for job in jobs[i]:
        heapq.heappush(pq, -job)
    
    if(pq):
        ans += -heapq.heappop(pq)

print(ans)

Solution (Python):
import heapq
from collections import defaultdict

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

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

Solution (Python):

import heapq
from collections import deque
n,m = map(int,input().split())
ab=[]
for i in range(n):
    ab.append(list(map(int,input().split())))

lis = [[]for i in range(m)]

for i in range(n):
    if ab[i][0]<=m:
        lis[ab[i][0]-1].append(ab[i][1])

pys = []
heapq.heapify(pys)
sum = 0
for i in range(m):
    tmp = lis[i]
    if tmp:
        for j in tmp:
            heapq.heappush(pys,-j)
    if pys:
        sum-=heapq.heappop(pys)

print(sum)

Solution (Python):
import heapq
n,m = map(int,input().split())
ma = {}
for _ in range(n):
    a,b = map(int,input().split())
    start = a
    if start<0:
        continue
    if start in ma:
        ma[start].append(-b)
    else:
        ma[start] = [-b]
li = []
heapq.heapify(li)
ans_li = []

for i in range(m):
    if i+1 in ma:
        for m in ma[i+1]:
            heapq.heappush(li, m)
    if len(li)>0:
        tmp = heapq.heappop(li)
        ans_li.append(-tmp)
        
print(sum(ans_li))

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

Solution (Python):
import heapq

n, m = map(int, input().split())
num = sorted([list(map(int, input().split())) for i in range(n)], key = lambda x: x[0])

s = []
j = 0
count = 0
heapq.heapify(s)

for i in range(1, m + 1):
  while (j < n and num[j][0] <= i):
    heapq.heappush(s, num[j][1] * (-1))
    j += 1
  if(len(s)):
    count += heapq.heappop(s) * (-1)

print(count)

Solution (Python):


import heapq

def main2():
    n,m=map(int,input().split())
    jobs_list=[list(map(int,input().split())) for _ in range(n)]
    
    banpei_list=[[i,0] for i in range(1,m+2)]
    
    jobs_list.extend(banpei_list)
    jobs_list=sorted(jobs_list)
    ans=0
    index=0
    que=[]
    while 1:
        day,pay=jobs_list[index]
        
        if day>m:
            break
        
        if day==jobs_list[index+1][0]: 
            heapq.heappush(que,-pay)
        
        
        else:
            heapq.heappush(que,-pay)
            ans-=heapq.heappop(que)
        index+=1
    print(ans)

if __name__ == "__main__":
    main2()

Solution (Python):

import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = [0]*N
B = [0]*N


keys = list(range(1, 1+10**5))

values = [[] for i in range(1, 1+10**5)]
job = dict(zip(keys, values))
for i in range(N):
    a, b = map(int, input().split())
    job[a].append(-b)
    

ans = 0
can_work = []
heapq.heapify(can_work)
for delay in range(1, 1+M):
    for item in job[delay]:
        heapq.heappush(can_work, item)
    
    
    if (len(can_work) != 0):
        ans -= heapq.heappop(can_work)

    
    
    
    
    
    
    
    
    
    
print(ans)

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

Solution (Python):
import heapq
n,m=map(int, input().split())
lst = []
for i in range(n):
  a,b=map(int, input().split())
  lst.append((-b,a))
lst = sorted(lst, key=lambda x:x[1])

now_idx = 0
queue = []
sum_ = 0
for i in range(1,m+1):
  for j in range(now_idx, n):
    if lst[j][1] <= i:
      heapq.heappush(queue, lst[j])
    else:
      now_idx = j
      break
  else:
    now_idx = n
  if len(queue)>0:
    sum_ -= heapq.heappop(queue)[0]
print(sum_)

Solution (Python):
from collections import defaultdict
import heapq
def solve():
    N,M = map(int,input().split())
    day_fee = defaultdict(list)
    for i in range(N):
        a, b = map(int,input().split())
        day_fee[a].append(b)

    fee_heap = []
    heapq.heapify(fee_heap)

    ans = 0
    for day in range(1,M+1):
        for fee in day_fee[day]:
            heapq.heappush(fee_heap, -fee)
        
        if len(fee_heap) != 0:
            ans += -heapq.heappop(fee_heap)
    
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
import heapq
from collections import defaultdict

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

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

Solution (Python):
import heapq 

inf = 10**15
mod = 10**9+7
n,m = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort(key = lambda x:x[0], reverse = True)
q = []
heapq.heapify(q)   
ans = 0
for i in range(1,m+1): 
    while ab and ab[-1][0] == i:
        _,val = ab.pop()
        heapq.heappush(q, (-1) * val)
    if q:
        ans += heapq.heappop(q) * (-1)

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
from collections import defaultdict
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")











N,M = map(int,input().split())
d = defaultdict(list)
for _ in range(N):
    a,b = map(int,input().split())
    d[a].append(b)
Q = [] 
heapify(Q)
s = 0
for i in range(1,M+1):
    li = d[i]
    for k in li:
        heappush(Q,-k)
    if len(Q) == 0:
        continue
    c = heappop(Q)
    s += -c
print(s)

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

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

ABs.sort()

PQ = []
iAB = 0
ans = 0
for rest in range(1, M+1):
    while iAB < N and ABs[iAB][0] <= rest:
        heappush(PQ, -ABs[iAB][1])
        iAB += 1
    if PQ:
        B = -heappop(PQ)
        ans += B

print(ans)

Solution (Python):
'''tanon  優先度付きキュー
t(=0 to M-1)日目に仕事を請けることを考えると、Ai>M-tなるiについては、
その仕事を請けるかどうかを考えなくて良い(∵この仕事を請けても、
Ai>M-t⇔Ai+t>Mより報酬はM日目より後にしかもらえません)。
したがって、tを大きい順に見ていくと、
M-1日目にはAi<=1なるiについて、報酬が最大となる仕事iを請けるべき、
M-2日目にはAi<=2なるiについて、まだ請けていない仕事の中で
報酬が最大となる仕事iを請けるべき、
…0日目にはAi<=Mなるiについて、まだ請けていない仕事の中で報酬が
最大となる仕事iを請けるべきであるといえます。
したがって、元の配列をAiの小さい順にソートしておき、
Ai=1の仕事について優先度付きキューに報酬Biを追加し、
優先度付きキューから取ってきた最大値を答えに加え、
次にAi=2の仕事について優先度付きキューに報酬Biを追加し、
優先度付きキューから取ってきた最大値を答えに加え、
…最後にAi=Mの仕事について優先度付きキューに報酬Biを追加し、
優先度付きキューから取ってきた最大値を答えに加えることで、
求める答えが得られます。
'''
import heapq
n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+2)] 


arr.extend(arr2)
arr=sorted(arr,key=lambda x:x[0]) 
q=[]
ans=0
pos=0
while 1:
 a,b=arr[pos]
 if a>m: 
   break
 if a==arr[pos+1][0]: 
  
   heapq.heappush(q,-b)
 else:
   heapq.heappush(q,-b)
   ans-=heapq.heappop(q)
  
 pos+=1
print(ans)

Solution (Python):
import heapq

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+2)] 
arr.extend(arr2)
arr=sorted(arr,key=lambda x:x[0]) 
q=[]
ans=0
pos=0
while 1:
 a,b=arr[pos]
 if a>m: 
   break
 if a==arr[pos+1][0]: 
   heapq.heappush(q,-b)
 else:
   heapq.heappush(q,-b)
   ans-=heapq.heappop(q) 
 pos+=1
print(ans)

Solution (Python):
from heapq import heapify, heappush, heappop
class HeapQue(object):
    def __init__(self, iterable, max_heap=False):
        self.sign = -1 if max_heap else 1
        sequence = [self.sign * number for number in iterable]
        heapify(sequence)
        self.heapque = sequence

    def push(self, item):
        heappush(self.heapque, item * self.sign)

    def pop(self):
        try:
            return heappop(self.heapque) * self.sign
        except:
            return 0

N, M = map(int, input().split())
jobs = sorted((tuple(map(int, input().split())) for _ in range(N)), key=lambda t: t[0], reverse=True)
q = HeapQue([], max_heap=True)
total = 0
for i in range(1, M + 1):
    while jobs and jobs[-1][0] == i:
        q.push(jobs.pop()[1])
    total += q.pop()
print(total)

Solution (Python):
import heapq
N, M = map(int, input().split())
value = [[] for _ in range(10 ** 5 + 1)]

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

koho = value[1]
heapq.heapify(koho)

ans = 0
for i in range(1, M + 1):
    if len(koho) > 0:
        ans += -1 * heapq.heappop(koho)
    if i < M:
        for v in value[i + 1]:
            heapq.heappush(koho, v)

print(ans)

Solution (Python):
import heapq
N, M = map(int, input().split())
value = [[] for _ in range(10 ** 5 + 1)]

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

koho = value[1]
heapq.heapify(koho)

ans = 0
for i in range(1, M + 1):
    if len(koho) > 0:
        ans += -1 * heapq.heappop(koho)
    if i < M:
        for v in value[i + 1]:
            heapq.heappush(koho, v)

print(ans)

Solution (Python):
def main():
    n,m = map(int,input().split())
    job =[[] for _ in range(m)]
    for i in range(n):
        a,b=map(int,input().split())
        if a-1 < m:
            job[a-1].append(-b) 
    
    res = 0
    q = []
    from heapq import heappop, heappush
    for i in range(m):
        for j in job[i]:
            heappush(q, j)
        if len(q) > 0:
            res += heappop(q) * -1 
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq

N, M = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
ABs.sort(reverse = True)

works = []
reward = 0
for i in range(1, M+1):
	while ABs and ABs[-1][0] <= i:
		heapq.heappush(works, -ABs.pop()[1])
	if works:
		reward -= heapq.heappop(works)
        
print(reward)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M, *m = map(int, read().split())
    tasks = [[] for _ in range(M)]
    for a, b in zip(m[::2], m[1::2]):
        if M-a>=0:
            tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s-=heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = map(int, readline().split())
    m = map(int, read().split())
    tasks = [[] for _ in range(M)]
    for a, b in zip(m, m):
        if M-a>=0:
            tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s-=heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = map(int, readline().split())
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, readline().split())
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s-=heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heappush 
def main():
    N, M = map(int, readline().split())
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, readline().split())
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = map(int, readline().split())
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, readline().split())
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in [i for i in range(M)][::-1]:
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = map(int, readline().split())
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, readline().split())
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in reversed(range(M)):
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from heapq import heappop, heapify, heappush 
def main():
    N, M = map(int, readline().split())
    tasks = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, readline().split())
        if M-a<0:
            continue
        tasks[M-a].append(b)
    s = 0
    h = []
    for i in reversed(range(M)):
        for b in tasks[i]:
            heappush(h, -b)
        if len(h)>0:
            s+=-heappop(h)
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
import heapq as hp

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

for i in range( N ):
  a, b = map( int, input().split() )
  if a - 1 < M:
    if L[ a - 1 ] == []:
      L[ a - 1 ] = [ b ]
    else:
      L[ a - 1 ].append( b )
      
ret = 0
C = []
hp.heapify( C )
for i in range( M ):
  for i in L[i]:
    hp.heappush( C, -i)
  if C != []:
    ret -= hp.heappop( C )

print( ret )

Solution (Python):

import heapq as hp

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

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

ans= 0
for i in range(m):
    for v in lists[i]:
        hp.heappush(h,-v)
    if len(h)>0:
        ans -= hp.heappop(h)
print(ans)

Solution (Python):
import heapq

dic = {}
n, m = map(int, input().split())
for _ in range(n):
  a, b = map(int, input().split())
  if dic.get(a) is not None:
    dic[a].append(-b)
  else:
    dic[a] = [-b,]

ans = 0
bite = []
for i in range(1, m+1):
  if dic.get(i) is not None:
    
    for val in dic[i]:
      heapq.heappush(bite, val)
  if len(bite) > 0:
    
    ans += heapq.heappop(bite)
print(ans*-1)

Solution (Python):
import heapq

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

works.sort()
day = 1
ans = 0
candidates = []
for work in works:
    if work[0] == day:
        heapq.heappush(candidates,-work[1])
    else:
        if M < work[0]:
            break
        while day < work[0]:
            if candidates:
                ans += -heapq.heappop(candidates)
            day += 1
        heapq.heappush(candidates,-work[1])

while day <= M:
    if candidates:
        ans += -heapq.heappop(candidates)
    day += 1

print(ans)

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

import heapq
N,M = (int(x) for x in input().split())
jobs = [[] for i in range(M)]
count = 0
h = []
for i in range(N):
    A,B = (int(x) for x in input().split())
    if A <= M:
        jobs[A-1].append(B*(-1))
heapq.heapify(h)
for i in range(M):
    if jobs[i]:
        for job in jobs[i]:
            heapq.heappush(h,job)
    if h:
        count -= heapq.heappop(h)
print(count)