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
from heapq import heappush, heappop

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
works = [[] for _ in range(M+1)]

for _ in range(N):
    a, b = lr()
    if a <= M:
        works[a].append(b)

cand = []
answer = 0
for i in range(1, M+1):
    for work in works[i]:
        heappush(cand, -work)
    if cand:
        answer -= heappop(cand)

print(answer)

Solution (Python):
import heapq
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x:x[0])
AB.append([10**5+1,0])
h=[]
ans=0
for i in range(1,M+1):
  while AB[0][0]<=i:
    heapq.heappush(h,-AB.pop(0)[1])
  if len(h)>0:
    ans+=-heapq.heappop(h)
print(ans)

Solution (Python):
import heapq

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

work = []
heapq.heapify(work)

idx = 0
res = 0
sz = 0
for i in range(m, -1, -1):
    dayleft = m - i
    while idx < n and ab[idx][0] <= dayleft:
        heapq.heappush(work, -ab[idx][1])
        sz += 1
        idx += 1
    if sz == 0:
        continue
    res -= heapq.heappop(work)
    sz -= 1
print(res)

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

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

Solution (Python):
import heapq

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

pay_list = []
heapq.heapify(pay_list)
ans = 0
AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)

Solution (Python):
import heapq

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

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

AB = sorted(AB, reverse=True)
for i in range(1, M+1):
    
    for j in range(len(AB)-1, -1, -1):
        a = AB[j][0]
        b = AB[j][1]
        if a <= i:
            
            heapq.heappush(pay_list, b*(-1))
            AB.pop(j)
        else:
            break
    
    if len(pay_list) > 0:
        ans += heapq.heappop(pay_list) * (-1)
print(ans)

Solution (Python):
import heapq as hq


def resolve():
    N,M = map(int,input().split())
    AB = [list(map(int,input().split())) for i in range(N)]
    AB.sort()
    q=[]
    sm =0
    counter =0
    for i in range(1,M+1):
        while counter <N and AB[counter][0]<=i:
            hq.heappush(q,-AB[counter][1])
            counter += 1

        if not q:
            continue
        sm -= hq.heappop(q)
    print(sm)


    

if __name__ == "__main__":
    resolve()

Solution (Python):

import heapq

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

l = []
heapq.heapify(l)

tmp = 0
ans = 0
for i in range(1, m+1):
    for j in range(tmp, n):
        a, b = ab[j]
        if i == a:
            heapq.heappush(l, -b)
        else:
            tmp = j
            break
    if len(l) > 0:
        ans += -1 * heapq.heappop(l)
print(ans)

Solution (Python):
from collections import deque
import heapq
import bisect

n,m = map(int,input().split())
L = []
for i in range(n):
    a,b = map(int,input().split())
    L.append([a, -b])
L.sort()
T = deque(L)
K = []
ans = 0
for i in range(m):
    while len(T) != 0:
        a = T.popleft()
        if a[0] == i+1:
            heapq.heappush(K, a[1])
        else:
            T.appendleft(a)
            break
    if len(K) != 0:
        t = heapq.heappop(K)
        ans += t
print(-ans)

Solution (Python):
import heapq

N,M = map(int,(input().split()))
A = [0]*N
B = [0]*N
mission = [[] for _ in range(M+1)]
work = 0

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

for day in range(1,M+1):
    for i in range(len(mission[day])):
        heapq.heappush(mission[0],mission[day][i])
    if mission[0]:
        work += heapq.heappop(mission[0])

print(-work)

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

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

if __name__ == '__main__':
    main()

Solution (Python):

"""
D - Summer Vacation
https://atcoder.jp/contests/abc137/tasks/abc137_d

"""
import sys


from heapq import heapify, heappop, heappush

def solve(N, M, jobs):
    heapify(jobs)
    availables = []
    ans = 0
    for day in range(1, M+1):
        while jobs and jobs[0][0] <= day:
            A, B = heappop(jobs)
            heappush(availables, (-B))
        if availables:
            ans -= heappop(availables)
    return ans


def main(args):
    N, M = map(int, input().split())
    jobs = []
    for _ in range(N):
        A, B = map(int, input().split())
        jobs.append((A, B))
    ans = solve(N, M, jobs)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

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

Solution (Python):
import sys
import numpy as np
import heapq


def input():
    return sys.stdin.readline()[:-1]


def solve():

    
    
    

    
    

    
    
    N, M = map(int, input().split())
    jobs = [[] for _ in range(M)]
    for _ in range(N):
        a, b = map(int, input().split())
        if a > M:
            continue
        jobs[M-a].append(b)
    que = []
    heapq.heapify(que)
    total = 0
    for i in reversed(range(M)):
        for b in jobs[i]:
            heapq.heappush(que, -b)
        if que:
            total += -heapq.heappop(que)
    print(total)


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import defaultdict
import heapq as pq
N,M = map(int,input().split())
dic = defaultdict(list)
for i in range(N):
    a,b = map(int,input().split())
    if a <= M:
        dic[a].append(b)
ls = []
ans = 0
for i in range(0,M+1):
    for j in dic[i]:
        pq.heappush(ls,-j)
    if ls:
        ans -= pq.heappop(ls)
print(ans)

Solution (Python):
import heapq

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

AB = sorted(AB, key=lambda x: x[0])
hq = []
ind = 0
ans = 0
for lim in range(1, M + 1):
    for i in range(ind, N):
        if AB[i][0] == lim:
            AB[i][0], AB[i][1] = -AB[i][1], AB[i][0]
            heapq.heappush(hq, AB[i])
            ind += 1
        else:
            break
    if hq:
        ans += -heapq.heappop(hq)[0]

print(ans)

Solution (Python):
import heapq
import sys

finput = sys.stdin.readline

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

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

for i in range(n):
    a,b = [int(j) for j in finput().split()]
    if a>m:
        continue
    q[a-1].append(-b)

hq = []
heapq.heapify(hq)
ans = 0
for i in range(m):
    if len(hq)==0 and len(q[i])==0:
        continue
    for j in range(len(q[i])):
        heapq.heappush(hq, q[i][j])
    ans += heapq.heappop(hq)

print(-ans)

Solution (Python):
import heapq
import numpy as np 

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

ab_list = []
for _ in range(N):
    a,b = map(int,input().split())
    ab_list.append([a,b])
ab_list = np.array(ab_list)
ab_list = ab_list[np.argsort(ab_list[:,0])]

ans = 0
h = []

flag = 0
for i in range(1,M+1):
    while flag<N:
        a = ab_list[flag][0]
        if a >i:
            break
        else: 
            b = ab_list[flag][1]
            heapq.heappush(h,-1 * b)
            flag+=1
    if len(h) > 0:
        ans += -1 * heapq.heappop(h)
print(ans)

Solution (Python):
import heapq

n,m=map(int,input().split())
X=[[] for _ in range(10**5+5)]
for i in range(n):
    a,b=map(int,input().split())
    X[a-1].append(-b)

ans=0
p=[0]*m
heapq.heapify(p)
for i in range(m):
    for j in range(len(X[i])):
        heapq.heappush(p,X[i][j])
    a=heapq.heappop(p)
    ans+=a*-1
print(ans)

Solution (Python):
import heapq

n,m=map(int,input().split())
X=[[] for _ in range(10**5+5)]
for i in range(n):
    a,b=map(int,input().split())
    X[a-1].append(-b)

ans=0
p=[]
heapq.heapify(p)
for i in range(m):
    if len(p)==0 and len(X[i])==0:
        continue
    for j in range(len(X[i])):
        heapq.heappush(p,X[i][j])
    a=heapq.heappop(p)
    ans+=a*-1
print(ans)

Solution (Python):
import heapq 
n,m = map(int, input().split())
a = [list(map(int,input().split())) for i in range(n)]
a.sort(reverse = True)
b = []
heapq.heapify(b)   
ans = 0

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

Solution (Python):
import heapq
n,m = map(int,input().split())
lst = [0 for _ in range(m)]
dic = {}
for i in range(n):
    a,b = map(int,input().split())
    if a in dic:
        dic[a].append(-b)
    else:
        dic[a] = [-b]
q = []
heapq.heapify(q)
ans = 0
for i in range(1,m+1):
    if i in dic:
        for j in range(len(dic[i])):
            heapq.heappush(q,dic[i][j])
    if q:
        ans -= heapq.heappop(q)
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort()

i = 0
works = []
reward = 0
for d in range(m):
  while i < n and jobs[i][0] <= d + 1:
    heapq.heappush(works, -jobs[i][1])
    i += 1
  if works:
    reward -= heapq.heappop(works)
print(reward)

Solution (Python):
N,M=map(int,input().split())
AB=[]
for _ in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
AB.sort(key=lambda x:x[0])
import heapq
h=[]
day=1
count=0
ans=0
while True:
    if day>M:
        break
    while count!=N:
        if AB[count][0]!=day:
            break
        heapq.heappush(h,-AB[count][1])
        count+=1
    if len(h)!=0:
        ans-=heapq.heappop(h)
    day+=1
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from heapq import *

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()

dic = defaultdict(list)
for i in range(N):
    a, b = MAP()
    dic[a].append((-1) * b)

ans = 0
incre = []


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

Solution (Python):
from heapq import heappop,heappush

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

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

heap = []
ans = 0

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

print(ans)

Solution (Python):


import heapq

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


event_list.sort()
jobs = []
ans = 0
for event in event_list:
  if event[1] < 10**5:
    heapq.heappush(jobs, -event[1])
  else:
    if jobs:
      ans -= heapq.heappop(jobs)

print(ans)

Solution (Python):
def main():
    from heapq import heapify,heappop,heappush

    n,m=map(int,input().split())
    W=[list(map(int,input().split())) for _ in range(n)]
    W.sort(key=lambda x:x[0])
    
    q=[]
    heapify(q)
    j,income=0,0
    for i in range(1,m+1):
        while j<n and W[j][0]<=i:
            heappush(q,-W[j][1])
            j+=1

        if q:
            t=heappop(q)
            income+=-t
    print(income) 

if __name__=='__main__':
    main()

Solution (Python):
import heapq
n,m=map(int,input().split())
A=[[0]*2for i in range(n)]
for i in range(n):
  A[i]=list(map(int,input().split()))
A=sorted(A)
ans=0
s=[]
j=0
for i in range(m+1):
  while j<n:
    if A[j][0]>i:
      break
    heapq.heappush(s,A[j][1]*(-1))
    j=j+1
  if len(s)==0:
    continue
  ans=ans-heapq.heappop(s)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
jobs = {}
for _ in range(n):
  a,b = map(int,input().split())
  if a in jobs:
    jobs[a].append(b)
  else:
    jobs[a] = [b]

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

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

Solution (Python):

from heapq import heapify, heappop, heappush
N, M = map(int, input().split())
AB = []
heapify(AB)
d = {}
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a > M:
        continue
    if a not in d.keys():
        d[a] = [-b]
    else:
        d[a].append(-b)
ans = 0
ables = []
now = 1
while now <= M:
    if now in d.keys():
        for v in d[now]:
            heappush(ables, v)
    if len(ables) > 0:
        v = heappop(ables)
        ans += -1 * v
    now += 1
print(ans)

Solution (Python):

from heapq import heapify, heappop, heappush
N, M = map(int, input().split())
AB = []
heapify(AB)
d = {}
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a > M:
        continue
    if a not in d.keys():
        d[a] = [-b]
    else:
        d[a].append(-b)
day = M - 1
ans = 0
ables = []
while day >= 0:
    now = M - day
    if now in d.keys():
        for v in d[now]:
            heappush(ables, v)
    if len(ables) > 0:
        v = heappop(ables)
        ans += -1 * v
    day -= 1
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def main():
    N, M = LI()
    jobsavailable = [[] for _ in range(10**5+1)]
    for _ in range(N):
        a, b = LI()
        if a > M:
            continue
        jobsavailable[M-a].append(b)
    cur_jobs = []
    cnt = 0
    for i in range(1, M+1):
        jobs = jobsavailable[M-i]
        for j in jobs:
            heapq.heappush(cur_jobs, -j)
        if cur_jobs != []:
            j = -heapq.heappop(cur_jobs)
            cnt += j
    print(cnt)

main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt

def main():
    N, M = LI()
    jobsavailable = [[] for _ in range(10**5+1)]
    for _ in range(N):
        a, b = LI()
        if a > M:
            continue
        jobsavailable[M-a].append(b)
    cur_jobs = []
    cnt = 0
    for i in range(1, M+1):
        
        jobs = jobsavailable[M-i]
        for j in jobs:
            _heappush_max(cur_jobs, j)
        if cur_jobs != []:
            j = _heappop_max(cur_jobs)
            cnt += j
    print(cnt)

main()

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

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

Solution (Python):
N, M = map(int, input().split())
works = dict()
for _ in range(N):
  a, b = map(int, input().split())
  if a in works.keys():
    works[a].append(-1 * b)
  else:
    works[a] = [-1 * b]


import heapq
cando = []
total = 0
day = M - 1
while day >= 0:
  lt = M - day

  if lt in works.keys():
    for salary in works[lt]:
      heapq.heappush(cando, salary)

  

  if len(cando) > 0:
    total += -1 * heapq.heappop(cando)

  

  day -= 1

print(total)

Solution (Python):
from sys import stdin
from heapq import heapify, heappop, heappush
from bisect import bisect_right

N, M = map(int, stdin.readline().split())
AB = [None]*N
for i in range(N):
    AB[i] = list(map(int, stdin.readline().split()))
AB.sort()

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m = inpl()
ab = [inpl() for i in range(n)]
hq = []
res = 0
ind = 0
ab.sort()
for i in range(1,m+1):
    while True:
        if ind >= n: break
        a,b = ab[ind]
        if a <= i:
            heappush(hq,[-b,a])
            ind += 1
        else: break
    if hq == []: continue
    tmp = heappop(hq)
    res -= tmp[0]
    
print(res)

Solution (Python):
from heapq import heappush, heappop
N, M = map(int, input().split())
ls = []
for i in range(N):
  a, b = map(int, input().split())
  ls += [(a,b)]
ls.sort(reverse=True)
hq = []
ans = 0
for i in range(1,M+1):
  while ls:
    if ls[-1][0]-i==0:
      heappush(hq,-ls[-1][1])
      ls.pop()
    else:
      break
  if hq==[]:
    continue
  ans -= heappop(hq)
print(ans)

Solution (Python):
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce
import statistics
import heapq

input = sys.stdin.readline
sys.setrecursionlimit(10**6)



def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList




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

data_list = [[0] for _ in range(m)]

for i in range(n):
    if data[i][0]-1 < m:
        data_list[data[i][0]-1].append(-data[i][1])

heap_list = []
heapq.heapify(heap_list)


ans = 0
for j in range(m):
    for i in range(len(data_list[j])):
        heapq.heappush(heap_list, data_list[j][i])
    a = heapq.heappop(heap_list)
    ans += a

print(-ans)

Solution (Python):
import heapq
 
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+1:
        ab[a-1].append(-b)
 
h=[]
ans=0
for i in range(m):
    for j in ab[i]:
        heapq.heappush(h,j)
    if len(h)==0:
        continue
    ans+=heapq.heappop(h)*(-1)
print(ans)

Solution (Python):
import heapq

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

dp = [0] * (M + max(a for a, b in AB))

AB.sort(key=lambda x:x[0])
q = []
heapq.heapify(q)
i = 0
ans = 0
for day in range(1, M+1):
  while i < len(AB) and AB[i][0] <= day:
    heapq.heappush(q, -1*AB[i][1])
    i += 1
  if q:
    ans += -1 * heapq.heappop(q)
    
print(ans)

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

n, m, *ab = map(int, sys.stdin.read().split())
ab = sorted(zip(*[iter(ab)] * 2))

def main():
    res = [0] * m
    hq = []
    i = 0
    for j in range(1, m+1):
        while i < n:
            a, b = ab[i]
            if a <= j:
                heappush(hq, -b)
                i += 1
            else:
                break
        if not hq:
            continue
        res[-j] = -heappop(hq)
    
    return sum(res)
        
if __name__ == '__main__':
    ans = main()
    print(ans)

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

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

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

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

print(ans)

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

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

print(ans)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def divisor(n):
        i = 1
        table = []
        while i * i <= n:
            if n % i == 0:
                table.append(i)
                table.append(n // i)
            i += 1
        table = list(set(table))
        return table


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)

    def list(self):
        return self.__q

    def length(self):
        return len(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


def main():
    
    N, M = Scanner.map_int()
    X = Scanner.int_list_list(N)
    X.sort()
    ans = 0
    tasks = PriorityQueue()
    i = 0
    for t in reversed(range(M)):
        while i < N and X[i][0] + t <= M:
            tasks.push(-X[i][1])
            i += 1
        if tasks.length() > 0:
            ans += -tasks.pop()
    print(ans)
    return


if __name__ == "__main__":
    main()

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

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

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

ind=0
ans=0
for i in range(1,m+1):
    while n>ind and ab_list[ind][0] <= i:
        heapq.heappush(l,-ab_list[ind][1])
        ind+=1

    if len(l) !=0:
        b = -heapq.heappop(l)
        ans+=b
print(ans)

Solution (Python):
n,m= map(int, input().split())
ab_list=[]
for _ in range(n):
    a, b = map(int, input().split())
    ab_list.append([a,b])
ab_list.sort()

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

ind=0
ans=0
for i in range(1,m+1):
    while n>ind and ab_list[ind][0] <= i:
        heapq.heappush(l,-ab_list[ind][1])
        ind+=1

    if len(l) !=0:
        b = -heapq.heappop(l)
        ans+=b
print(ans)

Solution (Python):
import heapq
N,M=map(int,input().split())
AB=[0]*N
for i in range(N):
    a,b=map(int,input().split())
    b*=-1
    AB[i]=[a,b]

AB.sort()
h=[]
heapq.heapify(h)
count=0
i=0
for day in range(AB[0][0],M+1):
    while i<N-1:
        if AB[i][0]==day:
            heapq.heappush(h,AB[i][1])
            i+=1
        elif AB[i][0]!=day:
            if len(h)!=0:
                count+=-heapq.heappop(h)
            break
    if i==N-1:
        if AB[i][0]==day:
            heapq.heappush(h,AB[i][1])
            count+=-heapq.heappop(h)
        elif AB[i][0]!=day:
            if len(h)!=0:
                count+=-heapq.heappop(h)
print(count)

Solution (Python):
import heapq as hq
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
a.sort()
for i in range(n):
    a[i][1]*=-1
b=[]
hq.heapify(b)
ans,i=0,0
for k in range(m-1,-1,-1):
    while i<n and a[i][0]+k<=m:
        hq.heappush(b,a[i][1])
        i+=1
    if b:
        ans-=hq.heappop(b)
print(ans)

Solution (Python):
import heapq as hq
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
a.sort()
for i in range(n):
    a[i][1]*=-1
b=[]
hq.heapify(b)
k,ans,i=1,0,0
while k<=m:
    while i<n and a[i][0]<=k:
        hq.heappush(b,a[i][1])
        i+=1
    if b:
        ans-=hq.heappop(b)
    k+=1
print(ans)

Solution (Python):
from heapq import heapify, heappush, heappop
N,M = map(int,input().split())
AB = [[] for i in range(M+1)]
for i in range(N):
    a,b = map(int,input().split())
    if a <= M:
        AB[a].append(-b)
AB2 = []
work = 0
heapify(AB2)
for i in range(1,M+1):
    for j in AB[i]:
        heappush(AB2,j)
    if len(AB2):
        b = heappop(AB2)
        work -= b
print(work)

Solution (Python):
import heapq
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
heapq.heapify(l)
a=[]
heapq.heapify(a)
fd=heapq.heappop(l)
heapq.heappush(a,-fd[1])
ans=0
for day in range(fd[0],m+1):
    while l:
        temp=heapq.heappop(l)
        if temp[0]>day:
            heapq.heappush(l,temp)
            break
        else:
            heapq.heappush(a,-temp[1])
    if a:
        ans+=heapq.heappop(a)
print(-ans)

Solution (Python):
N,M = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

import heapq
h = []
ans = 0
L.sort()
m = 1
i = 0
while i < N and L[i][0] <= M:
    if L[i][0] == m:
        heapq.heappush(h, -L[i][1])
        i += 1
    else:
        m += 1
        if h:
            ans -= heapq.heappop(h)
while m <= M and h:
    ans -= heapq.heappop(h)
    m += 1
print(ans)

Solution (Python):
import sys
import heapq
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
for _ in range(N):
  u, v = map(int, input().split())
  a.append((u, v))
a.sort(key = lambda x: -x[0])
h = []
res = 0

for i in range(M + 1):
  while len(a):
    if a[-1][0] > i: break
    heapq.heappush(h, -a.pop()[1])
  if len(h):
    res += heapq.heappop(h)
print(-res)