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



Sample input:
3 4
4 3
4 1
2 2


Sample output:
5

Submission status: Runtime Error

Solution code:
import heapq
n , m = map(int,input().split())
kouho = [[] for i in range(m)]
hosyu = [0 for i in range(m)]
for i in range(n):
    a , b = map(int,input().split())
    kouho[a-1].append(-b)
for i in range(m):
    now = i
    p = 0
    heapq.heapify(kouho[i])
    while kouho[i]:
        if now == m:
            break
        if p == 0:
            p = heapq.heappop(kouho[i])
        if hosyu[now] > p:
            hosyu[now] = p
            p = 0
        now += 1
print(abs(sum(hosyu)))

Submission status: Runtime Error

Solution code:
import heapq
n,m = map(int,input().split())
al = [[] for i in range(10)]
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)

Submission status: Runtime Error

Solution code:
import heapq

n,m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.append([100000,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):
    # if necessary day exceed the (now_day), extract the most valuable work
    if possible_days-2 >= len(max_values):
        break
    while possible_days <= ab[now][0]:
        if len(hq) < 1:
            pass
        else:
            max_values[possible_days-2] = -heapq.heappop(hq)
        possible_days += 1
    heapq.heappush(hq, -ab[now][1])
print(sum(max_values))

Submission status: Runtime Error

Solution code:
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):
    # if necessary day exceed the (now_day), extract the most valuable work
    while possible_days <= ab[now][0]:
        if len(hq) < 1:
            pass
        else:
            max_values[possible_days-2] = -heapq.heappop(hq)
        possible_days += 1
    heapq.heappush(hq, -ab[now][1])
print(sum(max_values))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split(' '))
AB = [[] for _ in range(M + 10)]
ans = 0
for _ in range(N):
  a, b = map(int, input().split())
  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)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split(' '))
AB = [[] for _ in range(N + 10)]
ans = 0
for _ in range(N):
  a, b = map(int, input().split())
  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)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
dp = [0]*(n+1)
L = []
for i in range(n):
    a,b = map(int,input().split())
    L.append([a,b])

L.sort(key = lambda x:x[1],reverse = True)
#print(L)
cnt =0
cost = 0
for i in range(n):
    if 0 <=L[i][0] <=m and dp[i] !=1:
        dp[m - L[i][0]] = 1
        cost +=L[i][1]
print(cost)

Submission status: Runtime Error

Solution code:
import heapq
N,M = map(int,input().split())
jobs = [ [] for _ in range(M+1) ]
for _ in range(N):
  a,b = map(int,input().split())
  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)

Submission status: Runtime Error

Solution code:

import collections
n,m = map(int,raw_input().split(' '))
h = [0] * (m+1)
for _ in range(n):
	a,b =  map(int, raw_input().split(' '))
		for d in range(m, -1, -1):
			if d - a >=0: h[d] = max(h[d], h[d-a] + b)

print h[m]

Submission status: Runtime Error

Solution code:
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 AB and AB[-2][0]<=latest:
    a,b=AB.pop(-2)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

Submission status: Runtime Error

Solution code:
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.append([0,0])
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-2][0]<=latest:
    a,b=AB.pop(-2)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

Submission status: Runtime Error

Solution code:
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.append(0)
for i in range(1,M+1):
  day=M-i
  latest=i
  while AB and AB[-2][0]<=latest:
    a,b=AB.pop(-2)
    heapq.heappush(works,-b)
  if not works:
    continue
  ans-=heapq.heappop(works)

print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_right as br
import heapq
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]

AB.sort()
A=[a for a,b in AB]
B=[b for a,b in AB]
works=[]
heapq.heapify(works)
ans=0
for i in range(1,M+1):
  day=M-i
  latest=i
  #worksにlatest日以内に報酬を貰える仕事を追加する
  if not A or A[0]>latest:
    pass
  else:
    for _ in range(br(A,latest)):
      A=A[1:]
      #index=br(works,B[0])
      #works=works[:index]+B[0]+works[index:]
      heapq.heappush(works,-B[0])
      B=B[1:]
  #if not works:
    #continue
  #ans+=works.pop(-1)
  ans-=heapq.heappop(works)

print(ans)

Submission status: Runtime Error

Solution code:
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]:z-=[heappush(q,-j)for j in i]or heappop(q)
print(z)

Submission status: Runtime Error

Solution code:
from heapq import*
(N,M),*t=[map(int,s.split())for s in open(0)]
z,*q=[0]*9*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)

Submission status: Runtime Error

Solution code:
import sys
import math
import bisect
stdin = sys.stdin

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

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

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

now=Bl[0]
ans=0
for i in range(1,M+2):
    if(now!=[]):
        max_val=max(now)
        ans+=max_val
        now.remove(max_val)
    now=now+Bl[i]

print(ans)

Submission status: Runtime Error

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

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

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

pys = deque([])
sum = 0
for i in range(m):
    tmp = lis[i]
    if tmp:
        for j in tmp:
            bisect.insort(pys,j)
    if pys:
        sum+=pys.pop()

print(sum)

Submission status: Runtime Error

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

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

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

pys = deque()
sum = 0
for i in range(m):
    tmp = lis[i]
    if tmp:
        for j in tmp:
            bisect.insort(pys,j)
    if pys:
        sum+=pys.pop()

print(sum)

Submission status: Runtime Error

Solution code:
import heapq

def main2():
    n,m=map(int,input().split())
    jobs_list=[list(map(int,input().split())) for _ in range(n)]
    #breakするタイミングを作るため、Mより大きい値の仕事を作成
    banpei_list=[[i,0] for i in range(1,m+2)]
    #リストの統合
    jobs_list.extend(banpei_list)
    jobs_list=sorted(jobs_list)
    print(jobs_list)
    ans=0
    index=0
    que=[]
    while 1:
        day,pay=jobs_list[index]
        print(jobs_list[index])
        #dayがMを超えていればbreak
        if day>m:
            break
        #同じdayのデータが次にもあれば-1倍してheapにpush
        if day==jobs_list[index+1][0]: 
            heapq.heappush(que,-pay)
        #仕事の報酬の最大値を答えに加える
        #前の日のデータからも報酬が良い仕事は選べるようにqueにデータは貯めておく
        else:
            heapq.heappush(que,-pay)
            print(que)
            ans-=heapq.heappop(que)
        index+=1
    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Runtime Error

Solution code:
def main2():
    n,m=map(int,input().split())
    jobs_list=[list(map(int,input().split())) for _ in range(n)]
    #breakするタイミングを作るため、Mより大きい値の仕事を作成
    banpei_list=[[i,0] for i in range(1,m+2)]
    #リストの統合
    jobs_list.extend(banpei_list)
    jobs_list=sorted(jobs_list)
    print(jobs_list)
    ans=0
    index=0
    que=[]
    while True:
        day,pay=jobs_list[index]
        print(jobs_list[index])
        #dayがMを超えていればbreak
        if day>m:
            break
        #同じdayのデータが次にもあれば-1倍してheapにpush
        if day==jobs_list[index+1][0]: 
            heapq.heappush(que,-pay)
        #仕事の報酬の最大値を答えに加える
        #前の日のデータからも報酬が良い仕事は選べるようにqueにデータは貯めておく
        else:
            heapq.heappush(que,-pay)
            print(que)
            ans-=heapq.heappop(que)
        index+=1
    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N, M = list(map(int, input().split()))
A = [0]*N
B = [0]*N
# 日数別にもらえる額を管理
# money = defaultdict(list)
keys = list(range(1, 10**5))
# print(keys)
values = [[] for i in range(1, 10**5)]
job = dict(zip(keys, values))
for i in range(N):
    a, b = map(int, input().split())
    job[a].append(b)
    # A[i], B[i] = a, b
# A日後にB円もらえる、M日後に報酬最大化
ans = 0
can_work = []
for delay in range(1, 1+M):
    can_work.extend(job[delay])
    # print('delay', delay)
    # print(job)
    if (len(can_work) != 0):
        max_money = max(can_work)
        ans += max_money
        can_work.remove(max_money)

    # for i in range(delay):
    #     print(delay-i)
    #     if len(jpb[delay-i]) != 0:
    #         print('found')
    #         max_money = max(jpb[delay-i])
    #         ans += max_money
    #         jpb[delay-i].remove(max_money)
    #         jpb[delay-i]
    #         break
    #     print('===========')
print(ans)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
s = [0 for i in range(n-1)]
for i in range(n-1):
    a,b = map(int,input().split())
    s[i] = [a,b]
s.sort()
ans = [-1 for i in range(n)]
for i in range(n-1):
    ans[s[i][1]-1] = s[i][0]-1
count = [0 for i in range(n)]
for i in range(q):
    p,x = map(int,input().split())
    count[p-1] += x
ans1 = [0 for i in range(n)]
ans1[0] = count[0]
for i in range(1,n):
    ans1[i] = ans1[ans[i]] + count[i]
for i in range(n):
    print(ans1[i])

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
chk = []
for i in range(n):
  a,b =  map(int,input().split())
  if a <= m:
    chk.append((-b,a))

from heapq import heapify, heappop, heappush
from operator import itemgetter
chk = sorted(chk,key=itemgetter(1))

s = []
heapify(s)
ans = 0
if chk == []:
  print(0)
  exit()
  
cnt = 1
for i in chk:
  a,b = i
  if b == cnt:
    heappush(s,i)
  if b > cnt:
    if s != []:
      r,t = heappop(s)
      ans -= r
      heappush(s,i)
    cnt += 1

for i in range(m-cnt+1):
  r,t = heappop(s)
  ans -= r
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
chk = []
for i in range(n):
  a,b =  map(int,input().split())
  if a <= m:
    chk.append((-b,a))

from heapq import heapify, heappop, heappush
from operator import itemgetter
chk = sorted(chk,key=itemgetter(1))

s = []
heapify(s)
ans = 0
if chk == []:
  print(0)
  exit()
  
cnt = chk[0][1]
for i in chk:
  a,b = i
  if b == cnt:
    heappush(s,i)
  if b > cnt:
    r,t = heappop(s)
    ans -= r
    heappush(s,i)
    cnt = b
  
for i in range(m-cnt+1):
  r,t = heappop(s)
  ans -= r    
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
chk = []
for i in range(n):
  a,b =  map(int,input().split())
  if a <= m:
    chk.append((-b,a))

from heapq import heapify, heappop, heappush
from operator import itemgetter
chk = sorted(chk,key=itemgetter(1))
print(chk)
s = []
heapify(s)
ans = 0
cnt = chk[0][1]
for i in chk:
  a,b = i
  if b == cnt:
    heappush(s,i)
  if b > cnt:
    r,t = heappop(s)
    ans -= r
    heappush(s,i)
    cnt = b
    
r,t = heappop(s)
ans -= r    
print(ans)

Submission status: Runtime Error

Solution code:
from operator import itemgetter

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

A_max = M
schedule = [0] * (M+1)
for A, B in pairs:
    for i in range(A, A_max+1):
        if schedule[i] == 0:
            schedule[i] = B
            break
    else:
        A_max = A
    


print(sum(schedule))

Submission status: Runtime Error

Solution code:
#137D
def main():
    import sys
    import heapq as h
    from collections import defaultdict
    N, M = map(int, sys.stdin.readline().split())
    L = tuple([tuple(map(int, sys.stdin.readline().split())) for _ in range(N)])
    #print(L)
    D = defaultdict(list)
    for i in range(N):
        D[L[i][0]-1].append(-L[i][1])
    
    #print(D)
    ans = 0
    tmp=[]
    h.heapify(tmp)
    for d in range(M):
        for b in D[d]:
            h.heappush(tmp, b)
        #print(tmp)
        ans += h.heappop(tmp)
        
    print(-ans)



if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def factorization(N):
    d = {}
    for n in range(2, N + 1):
        tmp = n
        for i in range(2, int(n ** 0.5) + 1):
            if tmp % i == 0:
                cnt = 0
                while tmp % i == 0:
                    cnt += 1
                    tmp //= i
                if i not in d:
                    d[i] = cnt
                else:
                    d[i] += cnt

        if tmp != 1:
            if tmp not in d:
                d[tmp] = 1
            else:
                d[tmp] += 1

    return d


def main():
    N = int(input())
    d = factorization(N)
    d = [v for k, v in d.items() if v > 1]
    cnt = {2: 0, 4: 0, 14: 0, 24: 0, 74: 0}
    cnt[2] = len(d)
    for i in range(len(d)):
        val = d[i]
        if val >= 74:
            cnt[74] += 1
        if val >= 24:
            cnt[24] += 1
        if val >= 14:
            cnt[14] += 1
        if val >= 4:
            cnt[4] += 1

    ans = cnt[74]
    ans += cnt[24] * (cnt[2] - 1)
    ans += cnt[14] * (cnt[4] - 1)
    ans += cnt[4] * (cnt[4] - 1) // 2 * (cnt[2] - 2)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import heapq
N, M = map(int,input().split())
L = [[] for _ in range(M)]
for _ in range(N):
    A, B = map(int,input().split())
    L[A-1].append(-B)
Q = []
ans = 0
for i in range(M):
    for j in L[i]:
        heapq.heappush(Q, j)
    try:
        ans -= heapq.heappop(Q)
    except:
        pass
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
N, M = map(int,input().split())
L = [[] for _ in range(M)]
for _ in range(N):
    A, B = map(int,input().split())
    L[A-1].append(-B)
Q = []
ans = 0
for i in range(M):
    for j in L[i]:
        heapq.heappush(Q, j)
    ans -= heapq.heappop(Q)
    
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from heapq import _heappop_max, _heapify_max
n,m=map(int,input().split())
task=[[0] for _ in range(m+1)]

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

task=task[::-1]

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

for i in range(m,-1,-1):
    cnt=0
    for j in task[i]:
        heapq.heappush(tmp,-j)
    cnt=heapq.heappop(tmp)
    ans-=cnt
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
N, M = map(int,input().split())
L = [[] for _ in range(M)]
for _ in range(N):
    A, B = map(int,input().split())
    L[A-1].append(B)
Q = []
ans = 0
for i in range(M):
    Q += L[i]
    try:
        ans += heapq.heappop(Q)
    except:
        pass
print(ans)

Submission status: Runtime Error

Solution code:
import heapq

n, m = map(int, input().split())
lis = []
for i in range(n + 1):
    lis.append([])

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

cnd = []
heapq.heapify(cnd)
res = 0

for j in range(1, m + 1):
    for e in lis[j]:
        heapq.heappush(cnd, e)
    if cnd:
        res += -heapq.heappop(cnd)
print(res)

Submission status: Runtime Error

Solution code:
import sys
import heapq
input = sys.stdin.readline

n,m=map(int,input().split())
p=tuple(tuple(map(int,input().split())) for _ in range(n))
p=sorted(p,key=lambda x:x[0])
q=[]
ans=0
count=0

for i in range(n-1):
  a,b=p[i]
  if a>m:
    break
  elif a==p[i+1][0]:
    heapq.heappush(q,-b)
  else:
    heapq.heappush(q,-b)
    ans-=heapq.heappop(q)
    count+=1
    
else:
  heapq.heappush(q,-p[n-1][1])
  for _ in range(m-count):
    ans-=heapq.heappop(q)

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):
        rem = []
        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_[1])
                rem.append(i)
            else:
                [a.pop(k) for k in rem]
                continue
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            # p = sorted(p, key = lambda x : x[1], reverse=True)
            p_max = max(p)
            bsum += p_max
            p.remove(p_max)

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):

        rem = []

        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_)
                rem.append(i)
            else:
                [a.pop(k) for k in rem] # 遅いかも
        
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            # p = sorted(p, key = lambda x : x[1], reverse=True)
            p_max = max(p, key=lambda x: x[1])
            bsum += p_max[1]
            p.remove(p_max)
        

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):

        rem = []

        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_)
                rem.append(i)
            else:
                [a.pop(k) for k in rem] # 遅いかも
        
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            p = sorted(p, key = lambda x : x[1], reverse=True)
            bsum += p[0][1]
            p.pop(0)
        

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import math

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

ABs = {}

for i in range(N):
    [day, reward] = [int(x) for x in sys.stdin.readline().split(" ")]
    if day in ABs:
        index = 0
        for r in ABs[day]:
            if r >= reward:
                break
            else:
                index += 1

        ABs[day].insert(index, reward)
    else:
        ABs[day] = [reward]

result = 0
for day in range(M, 0, -1):
    reward_and_day = (0, 0)
    for d in range(day, 0, -1):
        if d in ABs and ABs[d] and ABs[d][-1] > reward_and_day[0]:
            reward_and_day = (ABs[d][-1], d)
    (reward, day) = reward_and_day
    result += ABs[day].pop()

print(result)

Submission status: Runtime Error

Solution code:
import sys
import math
from typing import Dict, List

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

ABs: Dict[int, List[int]] = {}

for i in range(N):
    [day, reward] = [int(x) for x in sys.stdin.readline().split(" ")]
    if day in ABs:
        index = 0
        for r in ABs[day]:
            if r >= reward:
                break
            else:
                index += 1

        ABs[day].insert(index, reward)
    else:
        ABs[day] = [reward]

result = 0
for day in range(M, 0, -1):
    reward_and_day = (0, 0)
    for d in range(day, 0, -1):
        print(day, d, ABs)
        if d in ABs and ABs[d] and ABs[d][-1] > reward_and_day[0]:
            reward_and_day = (ABs[d][-1], d)
    (reward, day) = reward_and_day
    result += ABs[day].pop()

print(result)

Submission status: Runtime Error

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template

N,M = inputlist()
li = []
for i in range(N):
    A,B = inputlist()
    B *= -1
    li.append([A,B])
ans = 0
from heapq import *
li.sort()
k = []
heapify(k)
flag = 0
for i in range(1,M+1):
    if flag == N:
        for t in range(M+1-i):
            ans -= heappop(k)
		break
    while li[flag][0] <= i:
        heappush(k,li[flag][1])
        flag = flag + 1
        if flag == N:
            break
    if len(k)>=1:
        ans -= heappop(k)
print(ans)

Submission status: Runtime Error

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from heapq import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from bisect import *				# bisect_left(arr,x,start,end)  => start and end parameters are temporary
from sys import stdin				# bisect_left return leftmost position where x should be inserted to keep sorted
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''
3 4
4 3
4 1
2 2
'''

def solve(l,m):
	n = len(l)
	L = [[] for i in range(5)]
	for i in l:
		d,r = i
		L[d].append(r)
	ans = 0
	hp = []
	for i in range(1,m+1):
		for j in L[i]:
			heappush(hp,-j)
		if(hp):
			ans -= heappop(hp)
	return ans

N,M = list(map(int, stdin.readline().rstrip().split()))
l = []
for _ in range(N):
	a,b = list(map(int, stdin.readline().rstrip().split()))
	l.append([a,b])
ans = solve(l,M)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

dls = list(reversed(sorted(dls)))
memo = {}

def doit(index, time):
  if index >= n:
    return 0
  if (index, time) not in memo:
    a = doit(index + 1, time)
    w, r = dls[index]
    if time + w > m:
      b = 0
    else:
      b = r + doit(index + 1, time + 1)
    memo[(index, time)] = max(a, b)
  return memo[(index, time)]

print(doit(0, 0))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

dls = list(reversed(sorted(dls)))

def doit(index, time):
  if index >= n:
    return 0
  a = doit(index + 1, time)
  w, r = dls[index]
  if time + w > m:
    b = 0
  else:
    b = r + doit(index + 1, time + 1)
  return max(a, b)
  
print(doit(0, 0))

Submission status: Runtime Error

Solution code:
def main():
    N, M = map(int, input().split(' '))
    dict_works = dict()
    for n in range(1, N + 1):
        a, b = map(int, input().split(' '))
        if a > M:
            continue
        if a in dict_works:
            dict_works[a].append(b)
            dict_works[a].sort(reverse=True)
        else:
            dict_works[a] = [b]
    result = 0
    for x in range(1, M + 1):
        max_value = 0
        day = 0
        for n in range(x, 0, -1):
            if n in dict_works:
                if dict_works[n][0] > max_value:
                    day = n
                    max_value = dict_works[n][0]
        if day:
            result += dict_works[day][0]
            del dict_works[day][0]

    print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
A = []
for _ in range(N):
  Ai,Bi = map(int,input().split())
  A.append([Ai,Bi])
A.sort(key=lambda x: x[0],reverse=True)  
k = A[N-1][0]
A.sort(key=lambda x: x[1],reverse=True)

sum_pay = 0
checked = [1]*N
j=0
for i in range(M-k+1):
  while A[j][0] > M-i or checked[j] == 0:
    j += 1
    if j == N:
      break  
  sum_pay += A[j][1]
  checked[j] = 0
  j = 0
print(sum_pay)

Submission status: Runtime Error

Solution code:
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)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    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)

Submission status: Runtime Error

Solution code:
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)]
#Bが報酬
for _ in range(N):
    a, b = lr()
    works[a].append(b)

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

print(answer)

Submission status: Runtime Error

Solution code:
import heapq
n, m = map(int, input().split())
ab = [[] for _ in range(m+1)]
for _ in range(n):
    a, b = map(int, input().split())
    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)