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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, *AB = map(int, read().split())

    J = [[] for _ in range(M)]
    for a, b in zip(*[iter(AB)] * 2):
        if a > M:
            continue
        J[a - 1].append(b)

    hq = []
    ans = 0
    for i in range(M):
        for b in J[i]:
            heappush(hq, -b)
        if hq:
            ans += -heappop(hq)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from heapq import heappush, heappop

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

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

    ans = 0
    heap = []
    for a in range(M):
        for b in AB[a]:
            heappush(heap, b)
        if heap:
            ans -= heappop(heap)
    
    print(ans)

main()

Solution (Python):
import heapq
n,m = map(int,input().split())
al = [[] for i in range(10**5)]
ans = 0
h = []
for i in range(n):
    a,b = map(int,input().split())
    al[a-1] += [-b]
for i in range(m):
    for j in al[i]:
        heapq.heappush(h,j)
    if h:
        ans -= heapq.heappop(h)
print(ans)

Solution (Python):
import heapq
n,m = map(int,input().split())
al = [[] for i in range(10**5)]
ans = 0
h = []
for i in range(n):
    a,b = map(int,input().split())
    al[a-1] += [-b]
for i in range(m):
    for j in al[i]:
        heapq.heappush(h,j)
    if h:
        ans -= heapq.heappop(h)
print(ans)

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

N, M = list(map(int, input().split()))
jobs = defaultdict(list)
heap = []
ans = 0
for _ in range(N):
    a, b = list(map(int, input().split()))
    if a <= M:
        jobs[a].append(b)

for m in range(1, M+1):
    for b in jobs[m]:
        heappush(heap, -b)
    if heap:
        maxb = -heappop(heap)
        ans += maxb
print(ans)

Solution (Python):
n, m = map(int, input().split())
ab = [[] for _ in range(m+1)]
for i in range(n):
  t = [int(inp) for inp in input().split()]
  if t[0] <= m:
    ab[t[0]].append(-t[1])
ans = 0
import heapq
h = ab[1]
heapq.heapify(h)
for i in range(1, m+1):
  if len(h) > 0:
    ans += heapq.heappop(h)
  if i != m:
    for t in ab[i+1]:
      heapq.heappush(h, t)
print(-ans)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import heapq

def main():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n)]
    a_t = list(zip(*ab))
    AB = [[] for _ in range(m+1)]
    for a, b in zip(*a_t):
        if a <= m:
            AB[a].append(b*-1)
    
    cnt = 0
    h = [] 
    heapq.heapify(h)
    for i in AB:
        for j in i:
            heapq.heappush(h, j)
            
        if h:
            cnt += heapq.heappop(h)
    print(-1*cnt)   

if __name__ == '__main__':
    main()

Solution (Python):

import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


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


def read_float():
    return float(buff_readline())


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


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


def read_str_n():
    return readline().strip().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


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo


@mt
def slv(N, M, AB):
    AB.sort(key=lambda x: x[0], reverse=True)
    from heapq import heappop, heappush
    q = []
    ans = 0
    for i in range(1, M+1):
        while AB and AB[-1][0] <= i:
            _, b = AB.pop()
            heappush(q, -b)
        if q:
            ans += -heappop(q)
    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):
from heapq import heappush,heappop
n,m = map(int,input().split())
x = []
xa = x.append
for _ in range(n):
  ai,bi = map(int,input().split())
  xa((ai,bi))
x.sort(key=lambda a:a[0])
y = []
count = 0
j = 0
for i in range(1,m+1):
  while x[j][0] == i:
    heappush(y,-x[j][1])
    if j+1 < n:
      j += 1
    else:
      break
  if len(y) != 0:
    count -= heappop(y)
print(count)

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

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

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

AB.sort()
q = []
heapify(q)
cersol = 0
ans = 0
for i in range(0,M+1,1):
    while 1:
        if cersol < N and AB[cersol][0] <= i:
            heappush(q,-AB[cersol][1])
            cersol+=1
        else:
            break
    if len(q)>0:
        ans += -heappop(q)
print(ans)

Solution (Python):
from heapq import heappush as hpush
from heapq import heappop as hpop

class Descending_HeapQueue:
    def __init__(self, hq=[]):
        self.hq = []
        for i in hq:
            self.push(i)
    def push(self, n):
        hpush(self.hq, -n)
    def pop(self):
        return -hpop(self.hq)
    def __len__(self):
        return len(self.hq)

def main():
    N, M = map(int, input().split())
    AB = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[0])
    ans = 0
    dhq = Descending_HeapQueue()
    pointer = 0
    for m in range(1, M+1):
        while (pointer < N) and (AB[pointer][0] <= m):
            dhq.push(AB[pointer][1])
            pointer += 1
        if len(dhq) > 0:
            ans += dhq.pop()
    print(ans)
main()

Solution (Python):
import heapq

n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.append([m+1,0])
ab = sorted(ab, key=lambda x:x[0])



max_values = [0 for _ in range(m)]
hq = []

possible_days = 0

for now in range(n+1):
    
    while possible_days <= ab[now][0]:
        if len(hq) < 1:
            pass
        else:
            max_values[possible_days-2] = -heapq.heappop(hq)
        possible_days += 1
    if possible_days-2 > m-1:
        break
    heapq.heappush(hq, -ab[now][1])
print(sum(max_values))

Solution (Python):
from heapq import heapify, heappop, heappush
from collections import deque
N,M=map(int,input().split())
A=[0]*N 
B=[0]*N 
C=[0]*N
for i in range(N):
    A[i],B[i]= map(int,input().split())
    C[i]=[A[i],B[i]]
C.sort()
D=deque(C)
reward=0


h=[]
heapify(h)

for i in range(M-1,-1,-1):   
    while D:
        e = D.popleft()
        if e[0]==M-i:
            heappush(h,-1*e[1])
        else:
            D.appendleft(e)
            break
    
    if h:
        reward += (-1)*heappop(h)
        
print(reward)

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

N, M = map(int, input().split())
job = [list(map(int, input().split())) for _ in range(N)]
ans = 0
job.sort()
job.append([np.inf, np.inf])
jobhq = []
index = 0
for day in range(1, M + 1):
    while day >= job[index][0]:
        hq.heappush(jobhq, - job[index][1])
        index += 1
    if jobhq:
        ans += - hq.heappop(jobhq)
print(ans)

Solution (Python):
def insertHeap(array, val):
    array.append(val)
    n = len(array)-1
    while n != 0:
        parent = int((n - 1) / 2)
        if array[n] > array[parent]:
            tmp = array[n]
            array[n] = array[parent]
            array[parent] = tmp
            n = parent
        else:
            break
    return array

def desertHeap(array):
    if array == []:
      return None
    array[0], array[-1] = array[-1], array[0]
    res = array.pop(-1)
    n = len(array)
    if n == 0 or n == 1: 
      return res
    parent = 0
    while True:
        child = 2 * parent + 1
        if child >= n: break
        if (child + 1 < n) and array[child] < array[child + 1]:
            child += 1
        if array[parent] < array[child]:
            array[child], array[parent] = array[parent], array[child]
            parent = child;
        else:
            break
    return res

n, m = map(int, input().split())
 
li = [[] for j in range(10**5+1)]
 
for _ in range(n):
  a, b = map(int, input().split())
  li[a].append(b)
 
ans = 0
jobsHeap = []
for i in range(1, m+1):
  for val in li[i]:
    jobsHeap = insertHeap(jobsHeap, val)
  
  if jobsHeap != []:
    res = desertHeap(jobsHeap)
    ans += res
print(ans)

Solution (Python):





from collections import defaultdict
from heapq import heapify, heappush, heappop

n, m = map(int, input().split())
d = defaultdict(list)
queue = []

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

ans = 0
for i in range(1, m + 1):
    for j in d[i]:
        heappush(queue, -j) 
    if len(queue) > 0: 
        ans += heappop(queue) * -1

print(ans)

Solution (Python):
import heapq

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

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

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

print(ans)

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

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

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

ans = 0
for i in range(1, M+1):
    for j in d[i]:
        heappush(q, -j)
    if q:
        ans += heappop(q)*-1

print(ans)

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










import sys
sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():
    N, M = map(int, input().split())
    jobs=[[] for _ in range(M)]
    for i in range(N):
        a, b = map(int, input().split())
        if a > M:
            continue
        jobs[M-a].append(b)
    
    ans=0
    q=[]
    
    for i in reversed(range(M)):
        for b in jobs[i]:
            heappush(q,-b)
        if not q: continue
        ans+= -heappop(q)
    print(ans)
    
    
    

resolve()

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write

import heapq as hq
pq = []
hq.heapify(pq)
n, m = map(int, input().split())
arr = []
for _ in range(n):
    a, b = map(int, input().split())
    arr.append((a, b))
arr = sorted(arr, key=lambda x: x[0])
i = 0
ans = 0
for d in range(1, m + 1):
    while i < n and arr[i][0] <= d:
        hq.heappush(pq, -arr[i][1])
        i += 1
    if pq:
        ans -= hq.heappop(pq)
print(str(ans))

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

AB = []
for _ in range(N):
    A, B = map(int, input().split())
    AB.append((A, B))
AB = sorted(AB, key=lambda x: x[0])

hq = []
heapq.heapify(hq)  

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

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

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

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10000000)

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

jobs = [[] for _ in range(m) ]

for i in range(n):
    a,b= map(int,input().split())
    if a > m:continue
    jobs[m-a].append(b)
q=[]
heapq.heapify(q)
ans=0

for i in range(m-1,-1,-1):
    for b in jobs[i]:
        heapq.heappush(q,-b)

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

print(ans)

Solution (Python):
from heapq import heappush, heappop

n, m = map(int, input().split())
jobs = [[] for _ in range(10 ** 5 + 100)]

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

hq = []
res, j = 0, 0
for i in range(1, m + 1):
    for v in jobs[i]:
        heappush(hq, -v)
    if len(hq) > 0:
        res += -heappop(hq)
print(res)

Solution (Python):
from heapq import heappush, heappop

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

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

hq = []
res, j = 0, 0
for i in range(1, m + 1):
    while j < n and jobs[j][0] <= i:
        heappush(hq, -jobs[j][1])
        j += 1
    if len(hq) > 0:
        res += -heappop(hq)
print(res)

Solution (Python):
import heapq
n,m = map(int,input().split())
dp = [0]*(n+1)
L = []
days = []
cost = []
for i in range(n):
    a,b = map(int,input().split())
    if a > m:
        continue
    L.append([a,b])

L.sort(reverse = True)

ans = 0
li = []
for i in range(1,m+1):
    while L and L[-1][0] <= i:
        (a,b) = L.pop()
        heapq.heappush(li,b*(-1))

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

print(ans)

Solution (Python):
import heapq

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

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

ans = 0
job_rew = []
heapq.heapify(job_rew)
for i in range(1, M+1):
    if i in day_rew:
        for r in day_rew[i]:
            heapq.heappush(job_rew, r*(-1))
    if job_rew:
        this_day_rew = heapq.heappop(job_rew)*(-1)
        ans += this_day_rew

print(ans)

Solution (Python):









from heapq import heapify, heappop, heappush, heappushpop

def main():
    N, M = map(int, input().split())
    S = {}
    for i in range(N):
        a,b = map(int,(input().split()))
        if a in S.keys():
            S[a].append((-b,a))
        else:
            S[a]=[(-b,a)]
    hp = []
    heapify(hp)
    ret = 0
    for i in range(1,M+1):
        if i in S:
            for jb in S[i]:
                heappush(hp, jb)
        if len(hp)==0:
            continue
        x = heappop(hp)
        ret += -x[0]
    print(ret)

if __name__ == '__main__':
    main()

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

pq = []
d = 1
res = 0
for _ in range(M-1, -1, -1):
  for b in jobs[d]:
    heapq.heappush(pq, -b)
  if pq:
    res += -heapq.heappop(pq)
  d += 1

print(res)

Solution (Python):
import heapq

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

ans = 0
canditates = []
for a in range(1, M+1):
    if a in jobs.keys():
        for b in jobs[a]:
            heapq.heappush(canditates, -b)
    else:
        if not canditates:
            continue
    ans += heapq.heappop(canditates) * (-1)

print(ans)

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


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

    for i in range(M):
        AB[i] = sorted(AB[i])

    

    reward = []
    heapq.heapify(reward)
    ans = 0
    for i in range(M):
        if AB[i]:
            r = (-1) * AB[i].pop(-1)
            heapq.heappush(reward, (r, i))
        if reward:
            r, ind = heapq.heappop(reward)
            ans += (-1) * r
            if AB[ind]:
                r = (-1) * AB[ind].pop(-1)
                heapq.heappush(reward, (r, ind))

    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import heapq
N, M = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
day = {i+1:[] for i in range(M)}
for a, b in ab:
  if a <= M:
    day[a] += [-b]

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

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

N, M = map(int, input().split())
AB = []
for _ in range(N):
    ab = list(map(int, input().split()))
    a,b = ab[0], ab[1]
    AB.append((a,b))
AB.sort()

q = queue.PriorityQueue()
idx = 0
ans = 0
for i in range(1, M+1):
    while True:
        if idx >= N or AB[idx][0] > i:
            break
        b = AB[idx][1]
        q.put((-b, b))
        idx += 1
    if not q.empty():
        ans += q.get()[1]
print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import defaultdict
from heapq import heapify, heappush, heappushpop


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

    dic = defaultdict(list)
    for a, b in ab:
        dic[a].append(b)

    q = []
    heapify(q)

    for i in sorted(dic.keys(), reverse=True):
        for j in sorted(dic[i], reverse=True):
            if len(q) <= m - i:
                heappush(q, j)
            elif q and q[0] < j:
                heappushpop(q, j)

    print(sum(q))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
import collections
import heapq
n,m = map(int,raw_input().split(' '))

elems = [map(int, raw_input().split(' ')) for _ in range(n)]
elems.sort(key = lambda x:x[0])
heap = []

j = 0
cumul = 0
for i in range(1,m+1):

	while(j < len(elems) and elems[j][0] <= i): 
		heapq.heappush(heap,-elems[j][1])
		j+=1
	if heap:
		cumul -= heapq.heappop(heap)

print cumul

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*10**5
v=[[]for _ in q]
z=0
for a,b in t:v[a-1]+=b,
for i in v[:M]:
 for j in i:heappush(q,-j)
 z-=heappop(q)
print(z)

Solution (Python):
from heapq import*
(N, M), *t = [map(int, s.split()) for s in open(0)]
q = [0] * 10**5
v = [[] for _ in q]
for a, b in t:
    v[a - 1] += b,
z = 0
for i in v[:M]:
    for j in i:
        heappush(q, -j)
    z += -heappop(q)
print(z)

Solution (Python):
import heapq as hp
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)

ans=0
l=[]
hp.heapify(l)
for ab in AB:
  for t in ab:
    hp.heappush(l,t)
  if len(l)>0:
    ans-=hp.heappop(l)
print(ans)

Solution (Python):
import heapq
from collections import defaultdict

n, m = map(int, input().split())
ans = 0
jobs = defaultdict(list)
to_do = []
heapq.heapify(to_do)
for i in range(n):
    array = list(map(int, input().split()))
    array[1] *= -1
    jobs[array[0]].append(array[1])

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

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 M >= A:
        AB[A].append(B)

hp = []
ans = 0

for B in AB:
    for b in B:
        heapq.heappush(hp, (-1)*b)
    if hp:
        ans += heapq.heappop(hp)*(-1)
print(ans)

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




N,M = map(int,readline().split())
from collections import defaultdict
tasks = defaultdict(list)
for i in range(N):
  a,b = map(int,readline().split())
  tasks[a].append(b)
  
import heapq
q = []
heapq.heapify(q)
ans = 0
for i in range(1,M + 1):
  for task in tasks[i]:
    heapq.heappush(q,-task)
  if q:
    t = heapq.heappop(q)
    ans += (-t)

print(ans)

Solution (Python):

import sys
from collections import deque
import heapq

def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=False)
    a = deque(a)
    
    b_cands = []
    heapq.heapify(b_cands)

    ans = 0
    for M in range(M+1):
        while (len(a)!=0) and (a[0][0]==M):
            heapq.heappush(b_cands, -a.popleft()[1])
            
        if len(b_cands)>0:
            ans -= heapq.heappop(b_cands)

    print(ans)
    return



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 = []

    for i in range(N):
        A.append((int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq
N,M = map(int,input().split())
AB_s = [list(map(int, input().split())) for _ in range(N)]
MAPperA = {i: [] for i in range(1,M+1)}

for i, AB in enumerate(AB_s):
    if AB[0] in MAPperA:
        MAPperA[AB[0]].append(AB[1])
excutableJobList = []
heapq.heapify(excutableJobList)
answer = 0

for i in range(1,M+1):
    for j in MAPperA[i]:
        heapq.heappush(excutableJobList, j*-1)
    if excutableJobList:
        answer += heapq.heappop(excutableJobList) * -1
print(answer)

Solution (Python):
import heapq  
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
lsta=[[] for i in range(10**5+1)]
lst=[]
ans=0
for i in range(N):
 A,B=map(int,input().split())
 lsta[A].append(-B)
heapq.heapify(lst)  
for i in range(1,M+1):
 for j in lsta[i]:
  heapq.heappush(lst, j)
 if lst!=[]:
  ans+=heapq.heappop(lst)
print(-ans)

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

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

import heapq
from collections import deque

AB = deque(AB)

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

for i in range(M):
    while len(AB) > 0 and AB[0][0] <= i+1:
        t = AB.popleft()
        heapq.heappush(task,-t[1])
    if len(task) > 0:
        ans += heapq.heappop(task)
print(-ans)

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

AB.sort(reverse=True)
works=[]
heapq.heapify(works)
ans=0
AB=AB+[[0,0]]
for i in range(1,M+1):
  day=M-i
  latest=i
  while len(AB)>=2 and AB[-2][0]<=latest:
    a,b=AB.pop(-2)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

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

AB.sort(reverse=True)
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-1][0]<=latest:
    a,b=AB.pop(-1)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

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

AB.sort(reverse=True)
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-1][0]<=latest:
    a,b=AB.pop()
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

Solution (Python):
import sys
from collections import Counter
from collections import deque
import heapq
import math
import fractions
import bisect
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,m=mp()
l=[lmp() for i in range(n)]
l.append([10**6,10**6])
val=[]
heapq.heapify(val)
ans=0
c=0
l=sorted(l,key=lambda x: x[0],reverse=True)
for i in range(1,m+1):
    while l[-1][0]==i:
        q=l.pop()
        heapq.heappush(val,(-1)*q[1])
        c+=1
    if c>0:
        ans-=heapq.heappop(val)
        c-=1
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
pay = sorted([[-int(i) for i in input().split()] for _ in range(n)])

res = 0
pq = []
for i in range(1, m+1):
    while pay and -pay[-1][0] <= i:
        a, b = pay.pop()
        heapq.heappush(pq, b)
    if pq:
        res -= heapq.heappop(pq)

print(res)

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
print(sum(([heappush(q,-j)for j in i],-heappop(q))[1]for i in v[:M]))

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
z,*q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
for i in v[:M]:[heappush(q,-j)for j in i];z-=heappop(q)
print(z)

Solution (Python):
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
z,*q=[0]*50*M
v=[[]for _ in q]
for a,b in t:v[a-1]+=b,
for i in v[:M]:
 for j in i:heappush(q,-j)
 z-=heappop(q)
print(z)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
AB = [LIST() for _ in range(N)]
AB.sort(key=lambda x:-x[0])

ans = 0
h = []

for day in range(1, M+1):
	while AB and AB[-1][0] == day:
		heappush(h, -AB.pop()[1])

	if h:
		ans -= heappop(h)

print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
dic = {}
for i in range(n):
    ai, bi = map(int, input().split())
    if ai in dic:
        dic[ai].append(bi)
    else:
        dic[ai] = [bi]

for key in dic.keys():
    dic[key] = sorted(dic[key])

ans = 0
hp = []
heapq.heapify(hp)
for i in range(1, m+1):
    if i in dic:
        heapq.heappush(hp, (-dic[i].pop(), i))
    if hp != []:
        maxi = heapq.heappop(hp)
        ans += -maxi[0]
        if dic[maxi[1]] != []:
            heapq.heappush(hp, (-dic[maxi[1]].pop(), maxi[1]))
print(ans)

Solution (Python):
import heapq

n, m = map(int, input().split())
dic = {}
for i in range(n):
    ai, bi = map(int, input().split())
    if ai in dic:
        dic[ai].append(bi)
    else:
        dic[ai] = [bi]

for key in dic.keys():
    dic[key] = sorted(dic[key])

ans = 0
hp = []
heapq.heapify(hp)
for i in range(1, m+1):
    if i in dic:
        heapq.heappush(hp, (-dic[i].pop(), i))
    if hp != []:
        maxi = heapq.heappop(hp)
        ans += -maxi[0]
        if dic[maxi[1]] != []:
            heapq.heappush(hp, (-dic[maxi[1]].pop(), maxi[1]))
print(ans)

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

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

que = []
ans = 0
for d in range(1, M + 1):
    while AB and AB[-1][0] <= d:
        heappush(que, -AB.pop()[1])
    if que:
        ans += heappop(que)
print(-ans)

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

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)


h = []
import heapq
heapq.heapify(h)
ans = 0
for i in reversed(range(m)):
    while AB:
        a, b = AB.pop()
        if i+a > m:
            AB.append((a, b))
            break
        else:
            heapq.heappush(h, (-b, a))
    
    if h:
        b, a = heapq.heappop(h)
        b = b*(-1)
        ans += b
print(ans)

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

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

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

pq = []
ans = 0

for i in range(M, -1, -1):
    for v in d[M-i]:
        heappush(pq, -v)
    
    if len(pq)>0:
        ans += -heappop(pq)

print(ans)