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: Wrong Answer

Solution code:
from collections import defaultdict as dd
from collections import deque

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


for k,v in dic.items():
    dic[k]=deque(sorted(dic[k],reverse=True))

#print(dic)

ans=0
max2nd=[]
for i in reversed(range(m)):
    if len(dic[m-i])==0:
        if len(max2nd)==0: continue;
        max2nd=deque(sorted(max2nd, reverse=True))
        ans+=max2nd.popleft()

    else:
        ans+=dic[m-i].popleft()
        if len(dic[m-i])>0:
            max2nd.append(dic[m-i][0])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from collections import deque

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

for k, v in dic.items():
  dic[k]=sorted(v, reverse=True)
  dic[k]=deque(dic[k])

ans=0
flg=[False]*(m+1)
for i in range(m+1):
  if i in dic.keys():
    flg[m-i]=True
    ans+=dic[i][0]
    dic[i].popleft()

    
for i in range(m):
  if not flg[i]:
    tv=tk=0
    for k, v in dic.items():
      if len(v)==0: continue;
      if tv<v[0] and i+k<=m:
        tv=v[0]
        tk=k
    ans+=tv
    if tk: dic[tk].popleft();
    
    
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

def main():
    n, m = map(int, input().split())
    data = {}
    priorityQ = []
    for _ in range(n):
        a, b = map(int, input().split())
        if m + 1 - a >= 0:
            if m+1-a in data:
                data[m+1-a].append(b)
            else:
                data[m+1-a] = [b]
    print(data)
    ans = 0
    for i in range(m, 0, -1):
        if i in data.keys():
            for k in data[i]:
                heapq.heappush(priorityQ, -k)
        if priorityQ:
            ans -= heapq.heappop(priorityQ)
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq
class SegmentTree: #1-indexed
    def __init__(self,list,f=lambda x,y:x+y,inf=0):
        self.height=(len(list)-1).bit_length()+1 #木の高さ
        self.id=inf #単位元
        self.tree=[self.id]*(2**self.height) #木を単位元で初期化
        self.f=f
        for i in range(len(list)):
            self.tree[2**(self.height-1)+i]=list[i]
        for i in range(2**(self.height-1)-1,0,-1):
            self.tree[i]=self.f(self.tree[2*i],self.tree[2*i+1])
        
    def update(self,i): #i番目の要素をxに変更
        i+=2**(self.height-1)
        self.tree[i]+=1
        while i>1:
            i//=2
            self.tree[i]=self.f(self.tree[2*i],self.tree[2*i+1])
    
    def query(self,l,r):
        l+=2**(self.height-1)
        r+=2**(self.height-1)
        lf,rf=self.id,self.id
        while l<r:
            if l&1:
                lf=self.f(lf,self.tree[l])
                l+=1
            if r&1:
                r-=1
                rf=self.f(self.tree[r],rf)
            l//=2
            r//=2
        return self.f(lf,rf)

n,m=map(int,input().split())
t=[[0] for _ in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    if a>m:
        continue
    t[a-1].append(b)
S=SegmentTree([0]*m)
h=[]
for i in range(m-1,-1,-1):
    t[i].sort(reverse=True)
    for j in range(min(len(t[i]),m-i-S.query(i,m))):
        h.append(-t[i][j])
heapq.heapify(h)
ans=0
for i in range(m):
    ans+=-heapq.heappop(h)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,m=map(int,input().split())
t=[[0] for _ in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    if a>m:
        continue
    t[a-1].append(b)
h=[]
for i in range(m):
    t[i].sort(reverse=True)
    for j in range(min(len(t[i]),m-i)):
            h.append(-t[i][j])
heapq.heapify(h)
ans=0
for i in range(m):
    ans+=-heapq.heappop(h)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,m=map(int,input().split())
t=[[0] for _ in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    if a>m:
        continue
    t[a-1].append(b)
h=[]
for i in range(m):
    t[i].sort(reverse=True)
    for j in range(min(len(t[i]),m-i)):
            h.append(-t[i][j])
print(h)
heapq.heapify(h)
ans=0
for i in range(m):
    ans+=-heapq.heappop(h)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
ans=0
t=[[0] for _ in range(m+1)]
for a,b in l:
    if a>m:
        continue
    t[a].append(b)
h=[]
for i in range(1,m+1):
    t[i].sort(reverse=True)
    for j in range(min(len(t[i]),m-i+1)):
            h.append(-t[i][j])
heapq.heapify(h)
for i in range(m):
    ans+=-heapq.heappop(h)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

input = sys.stdin.readline
N, M = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)], key=lambda x: x[1])[::-1]
plan = []
ans = 0
for a, b in AB:
    i = bisect.bisect_left(plan, a)
    if a+len(plan)-i<=M and len(plan)<M:
        plan.insert(i, a)
        ans += b
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def solve(N,M,XB,reverse):
    X = []
    for b,A in XB.items():
        A.sort(reverse=reverse)
        X.append((b,A))

    X.sort(reverse=True)


    alpha = 0
    ans = 0
    count = 0
    for x in X:
        b,A = x
        for i in range(len(A)):
            a = A[i]
            a += alpha # a: 振り込み日

            if a > M: continue
            ans += b
            count += 1
            alpha += 1
            # if count >= M:
            #     break
        
        # if count >= M:
        #     break

    return ans

def main():
    N,M = mi()
    XB = collections.defaultdict(lambda: [])
    for i in range(N):
        a,b = mi()
        # X.append((a,b))
        XB[b].append(a)
    print(max(solve(N,M,XB,True),solve(N,M,XB,False)))
    


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M = mi()

    # X = []
    XB = collections.defaultdict(lambda: [])
    for i in range(N):
        a,b = mi()
        # X.append((a,b))
        XB[b].append(a)

    X = []
    for b,A in XB.items():
        A.sort(reverse=True)
        X.append((b,A))

    X.sort(reverse=True)


    alpha = 0
    ans = 0
    count = 0
    for x in X:
        b,A = x
        for i in range(len(A)):
            a = A[i]
            a += alpha # a: 振り込み日

            if a > M: continue
            ans += b
            count += 1
            alpha += 1
            # if count >= M:
            #     break
        
        # if count >= M:
        #     break

    print(ans)
    
    
    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M = mi()

    # X = []
    XB = collections.defaultdict(lambda: [])
    for i in range(N):
        a,b = mi()
        # X.append((a,b))
        XB[b].append(a)

    X = []
    for b,A in XB.items():
        A.sort(reverse=True)
        X.append((b,A))

    X.sort(reverse=True)


    alpha = 0
    ans = 0
    count = 0
    for x in X:
        b,A = x
        for i in range(len(A)):
            a = A[i]
            a += alpha # a: 振り込み日

            if a > M: continue
            ans += b
            count += 1
            alpha += 1
            if count >= M:
                break
        
        if count >= M:
            break

    print(ans)
    
    
    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
d=[]
ab=sorted(ab,key=lambda x: x[0])
ans=0
j=0
for i in range(m):
    for k in range(j,n):
        if ab[k][0]>i+1:
            j=k
            break
        heapq.heappush(d,(-1)*ab[k][1])
    else:
      j=n-1
    if not d:
        continue
    res=heapq.heappop(d)*(-1)
    ans+=res
    
print(ans)

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
d=[]
ab=sorted(ab,key=lambda x: x[0])
ans=0
j=0
for i in range(m):
    for k in range(j,n):
        if ab[k][0]>i+1:
            j=k
            break
        heapq.heappush(d,(-1)*ab[k][1])
    if not d:
        continue
    res=heapq.heappop(d)*(-1)
    ans+=res
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from operator import itemgetter

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

q = deque(jobs)
res = 0
for i in range(M):
    while q:
        job = q.popleft()
        if M - job[0] >= i:
            res += job[1]
            break

print(res)

Submission status: Wrong Answer

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

A = []

for i in range (0, N):
	C, D = map(int, input().split())
	A.append([C, D])

A = sorted(A)
Days = []
Number = []

for i in range (0, N):
	Days.append(A[i][0])
	Number.append(A[i][1])

Queue = []

import bisect as bi
for i in range (1, M+1):
	left = bi.bisect_left(Days, i)
	right = bi.bisect_right(Days, i)
	if right-left > 0:
		V = Number[left:right]
		for j in range (0, min(len(V), M-i+1)):
			Queue.append(V[len(V)-1-j])

Queue = sorted(Queue, reverse = True)

print(sum(Queue[0:min(len(Queue), M)]))

Submission status: Wrong Answer

Solution code:
import sys

icase=0
if icase==0:
    n,m=map(int,input().split())
#    ab=[[0]*2 for i in range(n)]
    ab=[]
    for i in range(n):
        ai,bi=map(int,input().split())
        if m-ai>=0:
            ab.append([m-ai,-bi])
#        ab[i][0]=m-ai
#        ab[i][1]=-bi
elif icase==1:
    n,m=3,4
    ab=[[2, -2], [0, -1], [0, -3]]    
elif icase==2:
    n,m=5,3
    ab=[[1, -3], [1, -1], [2, -4], [2, -3], [2, -2]]
elif icase==3:
    n,m=1,1
    ab=[]

from heapq import heappop,heappush
#heappush(H,l) 
#print(heappop(H)) #2

#ab.sort(reverse=True)
if len(ab)==0:
    print(0)
    sys.exit()
ab.sort()

n=len(ab)
a0=ab[0][0]
h=[]
asum=0
idx=0
for i in range(m):
    while ab[idx][0]<m-i:
        heappush(h,ab[idx][1])
#        print("i:",i,"idx:",idx,h)
        idx+=1
        if idx>=n:
            asum+=heappop(h)
#            print("1:",idx,asum,h)
            break
    if idx>=n:
        asum+=heappop(h)
#        print("2:",idx,asum,h)
        break

if idx>=n:
    asum+=heappop(h)
#    print("3:",idx,asum,h)

print(-asum)

Submission status: Wrong Answer

Solution code:
def main():
    n, m = map(int, input().split())
    work = [[] for _ in range(10 ** 4 + 1)]
    for _ in range(n):
        a, b = map(int, input().split())
        work[b].append(a)
    for i in range(10 ** 4 + 1):
        work[i].sort(reverse=True)
    cnt = 0
    ans = 0
    for i in range(10 ** 4, -1, -1):
        for w in work[i]:
            if cnt + w <= m:
                ans += i
                cnt += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, M = read_ints()
    AB = []
    for _ in range(N):
        AB.append(read_ints())
    AB.sort(reverse=True)
    rewards = []
    total = 0
    for i in range(M):
        while AB and AB[-1][0] <= i+1:
            a, b = AB.pop()
            heapq.heappush(rewards, -b)
        if rewards:
            total += -heapq.heappop(rewards)
    return total


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter


def main():
    # input = sys.stdin.buffer.readline
    n, m = map(int, input().split())
    jobs = [tuple(map(int, input().split())) for _ in range(n)]
    jobs.sort(reverse=True, key=itemgetter(0))
    jobs.sort(reverse=True, key=itemgetter(1))
    ans = 0
    index = 0
    # 働くのは0からm-1日後
    for i in range(m):
        while i + jobs[index][0] > m:
            index += 1
            if index == n:
                break
        else:
            ans += jobs[index][1]
            index += 1
            if index == n:
                break
            continue
        break
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#請け負った仕事の振り込みまでの合計日数が、期日を超えず、
#振り込みまでに日数＋受けた仕事数が期日を超えない

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

max_SumA = (M*(M+1))//2
AB.sort(key= lambda x:(-x[1],-x[0]))
memo = 0#請け負うと決めた仕事の数
total_A = 0
ans = 0
for a,b in AB:
    if  a+memo<=M:#請け負う
        memo +=1
        ans += b
        total_A += a
print(ans)

Submission status: Wrong Answer

Solution code:
#請け負った仕事の振り込みまでの合計日数が、期日を超えず、
#振り込みまでに日数＋受けた仕事数が期日を超えない

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

max_SumA = (M*(M+1))//2
AB.sort(key= lambda x:(-x[1],-x[0]))
memo = 0#請け負うと決めた仕事の数
total_A = 0
ans = 0
for a,b in AB:
    if total_A+a <= max_SumA and a+memo<=M:#請け負う
        memo +=1
        ans += b
        total_A += a
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
ab=[]
ans=[]
for i in range(n):
    a,b=map(int,input().split())
    a=m-a
    ab.append((a,b))
ab.sort()
ab.sort(key=lambda x:x[1],reverse=True)
for i in range(n):
    a,b=ab[i][0],ab[i][1]
    if a>=len(ans):
        ans.append(b)
print(sum(ans))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()

N, M = inl()


def solve():
    jobs = []
    for i in range(N):
        a, b = inl()
        jobs.append((b, a))
    jobs.sort(reverse=True)

    days = M
    ans = 0
    for b, a in jobs:
        if days <= 0:
            break
        if a <= days:
            ans += b
            days -= 1

    return ans


print(solve())

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
S = list()
A = [list() for _ in range(M+1)]
for _ in range(N):
    a, b = map(int, input().split())
    if M < a:
        continue
    heapq.heappush(A[M-a], -b)

ans = 0
for i in range(1, M+1):
    for num in A[i]:
        heapq.heappush(S, num)
    if S == []:
        continue
    ans -= heapq.heappop(S)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
S = list()
A = [list() for _ in range(M)]
for _ in range(N):
    a, b = map(int, input().split())
    if M < a:
        continue
    heapq.heappush(A[M-a-1], -b)

ans = 0
for i in range(M):
    for num in A[i]:
        heapq.heappush(S, num)
    if S == []:
        continue
    ans -= heapq.heappop(S)
print(ans)

Submission status: Wrong Answer

Solution code:
import re
import sys
from collections import *
from functools import lru_cache
from heapq import *
from itertools import *
from statistics import *
from string import (
    ascii_letters as letters,
    ascii_lowercase as lcletters,
    ascii_uppercase as ucletters,
    digits
)

hexdigits='01234567890ABCDEFabcdef'
inf=float('inf')

sys.setrecursionlimit(2**31-1)

rfs=lambda:rl().split()
ri=lambda:int(rl())
ris=lambda:list(map(int,rfs()))
rls=lambda:list(sys.stdin)
rl=lambda:next(sys.stdin)
rrs=lambda:list(map(str.strip,rls()))
rs=lambda:rl().strip()
rt=lambda:sys.stdin.read()
rg=lambda:list(map(list,rrs()))
rig=lambda:[[*map(int,x.split())] for x in rls()]

def n4(i,j):
    return [
                (i-1,j),
        (i,j-1),        (i,j+1),
                (i+1,j),
    ]

def n8(i,j):
    return [
        (i-1,j-1),(i-1,j),(i-1,j+1),
        (  i,j-1),        (  i,j+1),
        (i+1,j-1),(i+1,j),(i+1,j+1),
    ]

inb=lambda i,j,m,n:0<=i<m and 0<=j<n

###########################################

N,M=ris()
A=rig()
A.sort(key=lambda x:(-x[1],-x[0]))
ans=i=0
for j in range(M,-1,-1):
    while i<N and A[i][0]-j>0:
        i+=1
    if i==N:
        break
    ans+=A[i][1]
    i+=1

print(ans)

Submission status: Wrong Answer

Solution code:
import re
import sys
from collections import *
from functools import lru_cache
from heapq import *
from itertools import *
from statistics import *
from string import (
    ascii_letters as letters,
    ascii_lowercase as lcletters,
    ascii_uppercase as ucletters,
    digits
)

hexdigits='01234567890ABCDEFabcdef'
inf=float('inf')

sys.setrecursionlimit(2**31-1)

rfs=lambda:rl().split()
ri=lambda:int(rl())
ris=lambda:list(map(int,rfs()))
rls=lambda:list(sys.stdin)
rl=lambda:next(sys.stdin)
rrs=lambda:list(map(str.strip,rls()))
rs=lambda:rl().strip()
rt=lambda:sys.stdin.read()
rg=lambda:list(map(list,rrs()))
rig=lambda:[[*map(int,x.split())] for x in rls()]

def n4(i,j):
    return [
                (i-1,j),
        (i,j-1),        (i,j+1),
                (i+1,j),
    ]

def n8(i,j):
    return [
        (i-1,j-1),(i-1,j),(i-1,j+1),
        (  i,j-1),        (  i,j+1),
        (i+1,j-1),(i+1,j),(i+1,j+1),
    ]

inb=lambda i,j,m,n:0<=i<m and 0<=j<n

###########################################

N,M=ris()
A=rig()
A.sort(key=lambda x:(-x[1],-x[0]))
ans=i=0
for j in range(M,0,-1):
    while i<N and A[i][0]-j>0:
        i+=1
    if i==N:
        break
    ans+=A[i][1]
    i+=1

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n, m = map(int, input().split())
d = []
heapq.heapify(d)
for _ in range(n):
    a, b = map(int, input().split())
    heapq.heappush(d, (-b, -a))

cnt = m
ans = 0
while True:
    hp =  heapq.heappop(d)
    if hp[1]*(-1) <= cnt:
        ans += (hp[0]*(-1))
        cnt -= 1
    
    if cnt == 0 or (not d):
        break
      
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
d = []
for _ in range(n):
    a, b = map(int, input().split())
    d.append((b, a))
d.sort(reverse=True)

cnt = m
ans = 0
i = 0
while i < min(n, m):
    if d[i][1] <= cnt:
        ans += d[i][0]
        i += 1
        cnt -= 1
    else:
        i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect, copy, heapq, math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

ab.sort(lambda x: (-x[1],-x[0]))

# print(ab)
cnt=m
ans=0
for i in range(n):
    if cnt>=ab[i][0]:
        ans+=ab[i][1]
        cnt-=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from heapq import heappush, heappop, merge

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

day = defaultdict(list)
for a, b in ab:
    heappush(day[a], -b)

work, ans = [], 0
for i in range(1, m + 1):
    work = list(merge(work, sorted(day[i])))
    if len(work) > 0:
        ans -= heappop(work)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input()
ans,d=0,{}
for ln in sys.stdin:
    s=''.join(sorted(ln))
    ans+=d.setdefault(s,0)
    d[s]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import sys
N, M = map(int, input().split())
B = [[] for _ in range(M + 1)]
for _ in range(N):
    a, b = map(int, sys.stdin.readline().split())
    if a <= M:
        heapq.heappush(B[a], -b)

ans = 0
q = []
for i in range(1, M + 1):
    q = list(heapq.merge(q, B[i]))
    if q:
        ans += -(heapq.heappop(q))
print(ans)

Submission status: Wrong Answer

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


class MaxHeapObj(object):
  def __init__(self, val):
    self.val = val

  def __lt__(self, other):
    return self.val > other.val

  def __eq__(self, other):
    return self.val == other.val

  def __str__(self):
    return str(self.val)


class MinHeap(object):
  def __init__(self, L=None):
    if L is None:
      self.h = []
    else:
      heapq.heapify(L)
      self.h = L

  def heappush(self, x):
    heapq.heappush(self.h, x)

  def heappop(self):
    return heapq.heappop(self.h)

  def __getitem__(self, i):
    return self.h[i]

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


class MaxHeap(MinHeap):
  def __init__(self, L=None):
    super(MaxHeap, self).__init__(list(map(lambda x: MaxHeapObj(x), L)))

  def heappush(self, x):
    heapq.heappush(self.h, MaxHeapObj(x))

  def heappop(self):
    return heapq.heappop(self.h).val

  def __getitem__(self, i):
    return self.h[i].val


def main():
  N, M = list(map(int, input().split()))
  L = []
  for i in range(N):
    a, b = map(int, input().split())
    L.append((b, a))

  PQ = MaxHeap(L)
  max_ = 0

  for m in range(M):
    res = M - m

    while len(PQ):
      now = PQ.heappop()
      if now[-1] <= res:
        # print(now)
        max_ += (now[0])
        break

  print(max_)


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

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
hq = []
for _ in range(n):
    a,b = map(int,input().split())
    hq.append((-b,a))
hq.sort(key = lambda x:(x[0],-x[1]))
q = deque(hq)
day = 0
cnt = 0
while day <=m and q:
    b,a = q.popleft()
    b = -b
    if a+day<=m:
        cnt += b
        day += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
d=[[] for _ in range(n)]
for i in range(n):
  a,b=map(int,input().split())
  d[i]=(b,a)
d=sorted(d)[::-1]
ans=0
for i,j in d:
  if m<j:continue
  ans+=i
  m-=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

n,m=map(int, input().split())
ab = [deque() for i in range(m+1)]

for i in range(n):
    a,b=map(int, input().split())
    if(not a>m):
        ab[a-1].append(-1*b)
res = 0
cnt = 0

for i in range(m):
    if(len(ab[i])>0):
        tmp = min(ab[i])*-1
        ab[i].pop()
        res+=tmp
    ab[i+1]+=(ab[i])
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

n,m=map(int, input().split())
d = deque()
dd = defaultdict(int)
ab = [deque() for i in range(10**5)]

for i in range(n):
    a,b=map(int, input().split())
    if(not a>m):
        ab[b-1].append(-1*a)
res = 0
cnt = 0
popcnt = 0
for i in range(10**5 - 1,-1,-1):
    lenab = len(ab[i])
    while(lenab>0):
        tmp = ab[i].pop()*-1
        lenab-=1
        if(m-cnt < tmp):
            continue
        else:
            cnt+=1
            res += (i+1)
    if(cnt==m):
        break
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

n,m=map(int, input().split())
d = deque()
ab = [deque() for i in range(10**5)]

for i in range(n):
    a,b=map(int, input().split())
    if(not a>m):
        ab[b-1].append(a)
res = 0
cnt = 0
popcnt = 0
for i in range(10**5 - 1,-1,-1):
    lenab = len(ab[i])
    while(lenab>0):
        tmp = ab[i].pop()
        lenab-=1
        if(m-cnt < tmp):
            continue
        else:
            cnt+=1
            res += (i+1)

print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict

n,m=map(int, input().split())
d = deque()
ab = [deque() for i in range(10**5)]

for i in range(n):
    a,b=map(int, input().split())
    if(not a>m):
        ab[b-1].append(a)
res = 0
cnt = 0
popcnt = 0
for i in range(10**5 - 1,-1,-1):
    lenab = len(ab[i])
    while(lenab>0):
        tmp = ab[i].pop()
        lenab-=1
        if(m-cnt < tmp):
            continue
        else:
            cnt+=1
            res += (i+1)
    if(cnt==m):
        break
print(res)

Submission status: Wrong Answer

Solution code:
N, M = list(map(int,input().split()))
A = []
for i in range(N):
    in1, in2 = list(map(int,input().split()))
    A.append([in1, in2])
A = sorted(A, key = lambda x: x[1])[::-1]
ans = 0
day = M
for i in range(N):
    if A[i][0] <= day:
        ans += A[i][1]
        day -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N,M=map(int,input().split())
work = []
for i in range(N):
  a,b=map(int,input().split())
  work.append((a,b))
# 2重にソート
work = sorted(work, key=lambda x:x[0],reverse=True)
work = sorted(work, key=lambda x:x[1])
print(work)
occupied=[0 for i in range(M)]
SUM=0
count = 0
for i in range(N):
  a,b = work.pop()
  if M-a>=0 and occupied[M-a]==0:
    occupied[M-a]=1
    SUM+=b
    count+=1
  elif M-a<0:continue
  else:
    idx = bisect_left(occupied,1,0,M-a)
    if idx>0:
      occupied[idx-1]=1
      SUM+=b
      count+=1
  if count==M:break
    
print(SUM)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N,M=map(int,input().split())
work = []
for i in range(N):
  a,b=map(int,input().split())
  work.append((a,b))
# 2重にソート
work = sorted(work, key=lambda x:x[0])
work = sorted(work, key=lambda x:x[1])
occupied=[0 for i in range(M)]
SUM=0
count = 0
for i in range(N):
  a,b = work.pop()
  if M-a>=0 and occupied[M-a]==0:
    occupied[M-a]=1
    SUM+=b
    count+=1
  elif M-a<0:continue
  else:
    idx = bisect_left(occupied,1,0,M-a)
    if idx>0:
      occupied[idx-1]=1
      SUM+=b
      count+=1
  if count==M:break
    
print(SUM)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

work = sorted(work, key=lambda x:x[1])
occupied=[0 for i in range(M)]
SUM=0
count = 0
for i in range(N):
  a,b = work.pop()
  if M-a>=0 and occupied[M-a]==0:
    occupied[M-a]=1
    SUM+=b
    count+=1
  elif M-a<0:continue
  else:
    idx = bisect_left(occupied,1,0,M-a)
    if idx>0:
      occupied[idx-1]=1
      SUM+=b
      count+=1
  if count==M:break
    
print(SUM)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
lmoney=[]
for _ in range(n):
    a,b=map(int,input().split())
    if a<=m:
        lmoney.append([b,a])
lmoney=sorted(lmoney,reverse=True)
q=deque()
for i in lmoney:
    q.append(i)
day=0
money=0

while day<=m and q:
    job=q.popleft()
    if job[1]<=m-day:
        money+=job[0]
        day+=1
        
print(money)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
l=[]
for _ in range(n):
    a,b=map(int,input().split())
    l.append([b,a])
l=sorted(l,reverse=True)
q=deque()
for i in l:
    q.append(i)
day=0
money=0
while day<=m:
    ll=deque()
    job=q.popleft()
    if job[1]+day<=m:
        money+=job[0]
    else:
        ll.appendleft(job)
    for j in ll:
        q.appendleft(j)
    day+=1
print(money)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 13, 2020 23:22:33 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
import bisect
from sys import stdin
input = stdin.readline


class KeyWrapper:
    def __init__(self, iterable, key):
        self.it = iterable
        self.key = key

    def __getitem__(self, i):
        return self.key(self.it[i])

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


def main():
  N, M = list(map(int, input().split()))
  L = []
  for i in range(N):
    a, b = map(int, input().split())
    L.append((a, b))

  L = sorted(L, key=lambda x: (x[0]+x[1], x[0]))

  max_ = 0

  for m in range(M):
    res = M - m
    # idx = bisect.bisect_right(KeyWrapper(L, key=lambda x: x[0]), res) - 1

    # while idx in set_:
    #   idx -= 1
    # if idx < 0:
    #   break
    while len(L) and L[-1][0] > res:
      L.pop()

    if not len(L): break

    max_ += L[-1][1]
    L.pop()

  print(max_)


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

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
ab = sorted(ab, key=lambda x:(x[1], x[0]), reverse=True)

ans = 0
now = 0
i = 0
while now < m and i < n:
    if now + ab[i][0] <= m:
        ans += ab[i][1]
        now += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def S(): return sys.stdin.readline().rstrip()

N,M = map(int,S().split())
AB = [tuple(LI()) for i in range(N)]

from operator import itemgetter

AB = sorted(AB,key = itemgetter(1,0),reverse=True)

ANS = []

# 価値が大きく、報酬までの時間が長いものから貪欲に

r = M
for i in range(N):
    a,b = AB[i]
    if a <= r:
        ANS.append(b)
        r -= 1

print(sum(ANS))