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 bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap














        













@mt
def slv(N, M, AB):
    ans = 0
    cand = []
    AB.sort(key=lambda x: x[0], reverse=True)
    for i in range(1, M+1):
        while AB and AB[-1][0] <= i:
            a, b = AB.pop()
            heapq.heappush(cand, -b)
        if cand:
            ans -= heapq.heappop(cand)
    return ans 

def main():
    N, M = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(N, M, AB))

    
    
    
    


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
import math
import sys
import heapq
sys.setrecursionlimit(10000000)

def main():
    N, M = map(int, input().split())
    AB = []
    for i in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    
    AB = sorted(AB)
    ans = 0
    idx = 0
    priority_queue = []
    for i in range(1, M + 1):
        
        while True:
            if idx < N and AB[idx][0] <= i:
                heapq.heappush(priority_queue, - AB[idx][1])
                idx += 1
            else:
                break
        if len(priority_queue) == 0:
            continue
        else:
            max_element = - heapq.heappop(priority_queue)
            ans += max_element
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import heapq
N, M = map(int, input().split())
Work = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x: x[0])

ans = 0
can_work = []
i = 0
for m in range(M + 1):
    while (i < N) and (Work[i][0]) <= m:
        heapq.heappush(can_work, -Work[i][1])
        i += 1
    if can_work:
        ans += -heapq.heappop(can_work)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
import heapq
A=[]
for i in range(n):
  a,b=map(int,input().split())
  A.append([a,-b])
A.sort(key=lambda x:(x[0],x[1]))
B=[]
heapq.heapify(B)
ans=0
c=0
for i in reversed(range(m)):
  while c<n:
    a,b=A[c]
    if i+a<=m:
      heapq.heappush(B,[b,a])
      c+=1
    else:
      break
  if len(B)==0:
    continue
  else:
    bb,aa=heapq.heappop(B)
    ans+=bb
print(-ans)

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



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

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 bisect import bisect_left

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

for a in range(M_max):
    B[a] = sorted(B[a])

queue = []
ans = []
for m in range(1,M+1):
    if B[m]:
        b = B[m].pop(-1)
        idx = bisect_left(queue, (b, m))
        queue.insert(idx, (b, m))

    if queue:
        b, a = queue.pop(-1)
        ans.append(b)
        if B[a]:
            b_ = B[a].pop(-1)
            idx = bisect_left(queue, (b_, a))
            queue.insert(idx, (b_, a))

    
    
    

print(sum(ans))

Solution (Python):




 
from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul,itemgetter
from bisect import* 
from heapq import* 
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)

 
def main():
    a,b=map(int,input().split())
    d=defaultdict(list)
    for i in range(a):
        c,e=map(int,input().split())
        d[c].append(-e)
    for k,v in d.items():
        v=sorted(v)
    ans=[]
    anss=0
    for i in range(1,b+1):
        if i in d.keys():
            for j in d[i]:
                heappush(ans,j)
        if ans:
            anss+=heappop(ans)
    print(-anss)
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys, math
from collections import deque

readline = sys.stdin.readline

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

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

skip_pointer = [i for i in range(M)]
total = 0

def update_skip(s):
    if s < 0:
        return -1
    else:
        if s == skip_pointer[s]:
            skip_pointer[s] -= 1
            return s
        else:
            t = update_skip(skip_pointer[s])
            skip_pointer[s] = t - 1
            return t

for a, b in AB:
    s = M - a
    t = update_skip(s)

    if t >= 0:
        total += b

print(total)

Solution (Python):
import heapq
import sys
n, m = [int(i) for i in input().split()]
que = list()
dic = dict()
for i in range(n):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    if dic.get(a) is None:
        dic[a] = []
    dic[a].append(b)

cnt = 0
for i in range(1, m+1):
    if dic.get(i) is not None:
        for j in dic.get(i):
            heapq.heappush(que, -j)
    if len(que) > 0:
        _max = heapq.heappop(que)
        cnt += -_max
print(cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
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 II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N, M = LI()
    import collections
    ab = collections.defaultdict(list)
    for i in range(N):
        a, b = LI()
        ab[a].append(-1*b)

    dprint(ab)
    import heapq
    sumb = 0
    tgt = []
    heapq.heapify(tgt)

    for i in range(1, M+1):
        b_list = ab[i]
        for b in b_list:
            heapq.heappush(tgt, b)

        dprint(tgt)
        if len(tgt) != 0:
            mn = heapq.heappop(tgt)
            sumb += mn
            dprint(mn, sumb)
        dprint(tgt)

    print(sumb*-1)

solve()

Solution (Python):
import heapq
n, m = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = [int(item) for item in input().split()]
    ab.append([-a, b])
ab.sort(reverse = True)

st = []
newday = -1 
ans = 0
itr = 0
for day in range(1, m+1):
    if itr < n:
        while day == -ab[itr][0]:
            heapq.heappush(st, -ab[itr][1])
            itr += 1
            if itr >= n:
                break
    if not st:
        continue
    ans += heapq.heappop(st)
print(-ans)

Solution (Python):
from heapq import heappush, heappop

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


ans = 0
idx = 0
cand = []
for day in reversed(range(m+1)):
    while work[idx][0] <= m - day and idx < n:
        a, b = work[idx]
        heappush(cand, -b)
        idx += 1

    if len(cand) > 0:
        v = heappop(cand)
        ans += -v
        

print(ans)

Solution (Python):
import heapq

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

A = {}

for i in range(N):
  a, b = map(int, input().strip().split())
  if a not in A: A[a] = []
  A[a].append(b)

total = 0
days = []
for i in range(1, M + 1):
  if i in A:
    for a in A[i]:
      heapq.heappush(days, -a)
  if len(days) > 0:
    total += - heapq.heappop(days)

print(total)

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

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

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

from heapq import heappush,heappop


q = []

prev = X[0][0]
ans = 0
for x in X:
    if x[0]>M:
        break
    if x[0]!=prev:
        for i in range(x[0]-prev):
            if len(q)==0:
                break
            a=-heappop(q)
            ans+=a
        prev=x[0]
    heappush(q,-x[1])

for i in range(M-prev+1):
    if len(q)==0:
        break
    a=-heappop(q)
    ans+=a
    
print(ans)

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

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

q = []
ret = 0
for i in range(1, M+1):
    for j in AB[i]:
        heappush(q, j)
    if q:
        ret += heappop(q)

print(-ret)

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

def main():
    N,M = map(int,input().split())
    jobs = [[] for _ in range(M)]
    
    for _ in range(N):
        A,B = map(int,input().split())
        if A<=M:
            jobs[M-A].append(B)
            
    job_candidate = []
    reward = 0
    
    for i in range(M-1,-1,-1):
        for x in jobs[i]:
            insort(job_candidate,x)
        if job_candidate:
            reward += job_candidate.pop(-1)
            
    print(reward)
            
    
    
if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappop, heappush, heappushpop, heapreplace


class HeapQueue:
    def __init__(self, reverse=False):
        self.reverse = reverse
        self.heap = []

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

    def append(self, p, v):
        heappush(self.heap, (-p if self.reverse else p, v))

    def appendpop(self, p, v):
        return heappushpop(self.heap, (-p if self.reverse else p, v))

    def popappend(self, p, v):
        return heapreplace(self.heap, (-p if self.reverse else p, v))

    def pop(self):
        return heappop(self.heap)[1]

    def front(self):
        return self.heap[0]


N, M = map(int, input().split())
AB = [[] for _ in range(100010)]
for _ in range(N):
    a, b = map(int, input().split())
    AB[a].append(b)
ans = 0
q = HeapQueue(reverse=True)
for i in range(M):
    for b in AB[i + 1]:
        q.append(b, b)
    if len(q):
        ans += q.pop()
print(ans)

Solution (Python):
import heapq

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

alist=[]

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

alist=sorted(alist)


eternal=1
now=0
hoshu=0

koholist=[]
for i in range(m):
    j=i+1
    switch=1
    while switch==1 and eternal==1:
        if alist[now][0]<=j:
            heapq.heappush(koholist, -alist[now][1])
            if now>=n-1:
                switch=0
                eternal=0
            else:
                now+=1
        else:
            switch=0
    if len(koholist)>0:
        saidai=heapq.heappop(koholist)
        
        hoshu+=saidai

print(-hoshu)

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

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
import heapq

n, m = getList()

works = [[] for i in range(m+2)]

for i in range(n):
    a, b = getList()
    try:
        works[a].append(-b)
    except:
        pass

ans = 0
q = []

for i, day in enumerate(works[1:]):
    if i >= m:
        
        break
    for work in day:
        heapq.heappush(q, work)
    if q:
        reward = heapq.heappop(q)
        ans += reward
    

print(-ans)

Solution (Python):
import sys
import heapq


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    tasks = list(map(tuple, zip(A, B)))
    tasks.sort()
    tasks.reverse()

    earnables = []

    ans = 0
    for i in range(M + 1):
        while len(tasks) > 0 and tasks[-1][0] <= i:
            _, pay = tasks.pop()
            heapq.heappush(earnables, -pay)
        if len(earnables) > 0:
            ans += -heapq.heappop(earnables)

    return ans



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()

    N = int(next(tokens))  
    M = int(next(tokens))  
    A = [int()] * (N)  
    B = [int()] * (N)  
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))

    ans = solve(N, M, A, B)
    if ans is not None:
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import heapq
N, M = map(int, input().split())
H = [[] for _ in range(M)]
for _ in range(N):
  A, B = map(int, input().split())
  if A <= M:
    H[A - 1].append(-B)
Q = []
cnt = 0
for d in range(M):
  for b in H[d]:
    heapq.heappush(Q, b)
  if Q:
    cnt -= heapq.heappop(Q)
print(cnt)

Solution (Python):
N, M = map(int, input().split())
A, B = (
    zip(*(map(int, input().split()) for _ in range(N))) if N else
    ((), ())
)


class SegmentTree:
    def __init__(self, n, f, e):
        self.n = 2**((n - 1).bit_length())
        self.f = f
        self.e = e
        self.d = [e for _ in range(2 * self.n)]

    def __setitem__(self, key, value):
        key += self.n - 1
        self.d[key] = value
        while key > 1:
            key //= 2
            self.d[key] = self.f(self.d[2 * key], self.d[2 * key + 1])

    def __getitem__(self, key):
        return self.q(key, 1, 1, self.n + 1)

    def q(self, key, i, left, right):
        return (
            self.e if left >= key.stop or right <= key.start else
            self.d[i] if key.start <= left <= right <= key.stop else
            self.f(
                self.q(key, 2 * i, left, (left + right) // 2),
                self.q(key, 2 * i + 1, (left + right) // 2, right)
            )
        )


T = SegmentTree(M, max, -1)
for i in range(M + 1):
    T[i] = i

ans = 0
for a, b in sorted(zip(A, B), key=lambda t: (-t[1], t[0])):
    x = T[1:(M-a+2)]
    if x != -1:
        ans += b
        T[x] = -1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import heapq
N, M = map(int, input().split())
day = [[] for _ in range(100001)]
for i in range(N):
    a, b = map(int, input().split())
    day[a].append(b)


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

print(ans)

Solution (Python):



import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    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])
    
    ans = 0
    j = 0
    h = []
    for i in range(1,m+1):
        while j < len(ab) and ab[j][0] <= i:
            heappush(h, (-ab[j][1]))
            j += 1

        if len(h) > 0:
            val = heappop(h)
            
            ans -= val

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


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():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return


def D():
    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:x[0])
    ans = 0
    f = [1]*n
    j = 0
    q = []
    for i in range(m)[::-1]:
        while j < n and m-g[j][0] >= i:
            a,b = g[j]
            heappush(q,(-b,a))
            j += 1
        if q:
            x,a = heappop(q)
            if i+a <= m:
                ans -= x
    print(ans)
    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    D()

Solution (Python):
from collections import defaultdict
import heapq

n, m = map(int, input().split())
dct = defaultdict(list)

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

h = []
res = 0

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

print(-res)

Solution (Python):
import heapq
N, M = map(int, input().split())
B = [[] for _ in range(M+1)]
q = []
ans = 0
for i in range(N):
    a, b = map(int, input().split())
    if a>M:
        continue
    B[M-a].append(b)
for i in range(M+1)[::-1]:
    for b in B[i]:
        heapq.heappush(q, -b)
    if q:
        m = -heapq.heappop(q)
        ans+=m
print(ans)

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

_d = {i:[] for i in range(M+1)}
for a,b in AB:
  _d.setdefault(a, []).append(b)
  
import heapq

ans = 0
hp = []

for i in range(1, M+1):
  for b in _d[i]:
    heapq.heappush(hp, -b)
  if hp:
    ans += -heapq.heappop(hp)

print(ans)

Solution (Python):
import heapq
N,M = [int(i) for i in input().split()]
AB = [[int(i) for i in input().split()] for j in range(N)]
AB.sort(key=lambda x:x[0])
curpos = 0
Works = []
d = 1
ans = 0
curpos = 0

for d in range(1,M+1):
    while 1:
        if curpos>=N:break
        if AB[curpos][0]<=d:
            heapq.heappush(Works,-AB[curpos][1])
            curpos += 1
        else:
            break
    if Works:
        ans -= heapq.heappop(Works)
print(ans)

Solution (Python):
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in [0]*n]
ab.sort()


import heapq
h=[]
ans=0
j=0
for i in range(1,m+1):
    while j<n:
        if ab[j][0]>i:
            break
        else:
            heapq.heappush(h,-ab[j][1])
            j+=1
    
    if h!=[]:
        k=-heapq.heappop(h)
        ans+=k
print(ans)

Solution (Python):
N, M = map(int, input().split())
lst = []
for _ in range(N):
    A, B = map(int, input().split())
    lst.append([A, -B])
lst.sort()

ans = 0
index = 0
import heapq
hq = []
for i in range(M):
    while index < N and lst[index][0] <= i + 1:
        heapq.heappush(hq, lst[index][1])
        index += 1
    if len(hq) > 0:
        ans -= heapq.heappop(hq)
    
print (ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m = na()
c = []

for i in range(n):
    a,b = na()
    c.append([a,b])

import heapq

c = sorted(c)
k = 0
d = 1
h = []
ans = 0
while d < m+1:
    while k < len(c) and c[k][0] <= d:
        heapq.heappush(h, -c[k][1])
        k+=1
    if len(h) > 0:
        j = heapq.heappop(h)
        ans -= j
    d += 1

print(ans)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

from heapq import heappush, heappop

import __pypy__


def main():
    n, m = map(int, input().split())
    a, b = [0] * n, [0] * n
    for i in range(n):
        a[i], b[i] = map(int, input().split())
    order = sorted(range(n), key=lambda x: a[x])

    heap = []

    cnt = 0
    i, j = 0, 1
    while i < n and j <= m:
        while i < n and a[order[i]] <= j:
            heappush(heap, -b[order[i]])
            i += 1
        if heap:
            cnt -= heappop(heap)
        j += 1

    while j <= m:
        if heap:
            cnt -= heappop(heap)
        j += 1

    print(cnt)




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

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

B.sort()
j=0

import heapq
H=[]
ANS=0


for day in range(1,M+1):
    while j<N and B[j][0]<=day:
        heapq.heappush(H,-B[j][1])
        j+=1

    if H:
        ANS+=-heapq.heappop(H)


print(ANS)

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

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

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

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

print(res)

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

input = sys.stdin.readline

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

vmax = pow(10, 5)

jobs = [[] for _ in range(vmax + 1)]
for _ in range(n):
    a, b = map(int, input().split())
    jobs[a].append(-b)

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

Solution (Python):
import heapq

n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]
info = sorted(info)
q = []
i = 0
j = 0
day = 1
ans = 0
for i in range(m):
    while True:
        if j >= n:
            break
        if info[j][0] == i+1:
            heapq.heappush(q, -info[j][1])
            j += 1
        else:
            break
    if q:
        ans += -heapq.heappop(q)
  
print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappop, heappush

n, m = li()
works = [[] for _ in range(m+1)]
for _ in range(n):
    ai, bi = li()
    if m-ai >= 0:
        works[m-ai].append(bi)

pque = []
ans = 0
for mi in range(m, -1, -1):
    if works[mi]:
        for wi in works[mi]:
            heappush(pque, -wi)

    if pque:
        ans += -heappop(pque)

print(ans)

Solution (Python):


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

from heapq import *

n,m = [int(i) for i in readline().split()]
ab = [[int(i) for i in readline().split()] for _ in range(n)]

ab.sort(reverse = True)



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




print(ans)

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YNeos='YNeos'
mo=10**9+7
imp='IMPOSSIBLE'

n,m=MI()
w=[[] for _ in range(10**5+3)]
for i in range(n):
    a,b=MI()
    w[a].append(-b)

q=[]
ans=0
for i in range(1,m+1):
    for j in w[i]:
        heappush(q,j)
    if q:
        x=heappop(q)
        ans+=-x
        

print(ans)

Solution (Python):
from operator import itemgetter
import heapq
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=itemgetter(0))
q = []
idx = 0
ans = 0
for i in range(1, M+1):
    while idx < N:
        a, b = AB[idx]
        if a <= i:
            heapq.heappush(q, -b)
            idx += 1
        else:
            break
    if len(q)>0:
        b = heapq.heappop(q)
        ans -= b
print(ans)

Solution (Python):
import sys
from operator import itemgetter

stdin = sys.stdin

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

n, m = na()
ab = []
for i in range(n):
    ab.append(na())

ab.sort(key=itemgetter(0))

pq = []
import  heapq

ans = 0
p = 0
for i in range(m+1):
    while p < n and ab[p][0] <= i:
        heapq.heappush(pq, -ab[p][1])
        p += 1

    if len(pq) == 0:
        continue

    ans -= heapq.heappop(pq)

print(ans)

Solution (Python):
from itertools import accumulate
from bisect import bisect, bisect_left
from collections import Counter, deque
from functools import reduce
from itertools import accumulate
from operator import itemgetter, xor
from heapq import heappop, heappush

from sys import setrecursionlimit
setrecursionlimit(10**9)
 
import sys
input = sys.stdin.readline
from collections import Counter
def inpl(): return list(map(int, input().split()))

N, M = inpl()
T = sorted([inpl() for _ in range(N)], key=itemgetter(0), reverse=True)
Q = []
ans = 0

for i in range(1, M+1):
    while len(T) and (T[-1][0] == i):
        heappush(Q, -T.pop()[1])
    if len(Q):
        ans += -heappop(Q)

print(ans)

Solution (Python):
import sys
from heapq import heappush as hp, heappop as hpp
N, M = map(int, input().split())
W = [[] for _ in range(M+1)]
for _ in range(N):
    a, b = map(int, sys.stdin.readline().split())
    if a > M:
        continue
    W[a].append(b)

Q = []
ans = 0
for j in range(M+1):
    for b in W[j]:
       hp(Q, -b)
    if Q:
        ans -= hpp(Q)

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
import heapq
n,m=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(n)]
AB.sort()
hq=[]
tmp=0
ans=0
for i in range(1,m+1):
    while tmp<n and AB[tmp][0]<=i:
        heapq.heappush(hq,-AB[tmp][1])
        tmp+=1
    if hq:
        ans-=heapq.heappop(hq)
print(ans)

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

n, m = list(map(int, input().split()))
works = []
for line in sys.stdin:
    a, b = map(int, line.split())
    if a > m:
        continue
    works.append((a, b))

works.sort(reverse=True)

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