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: Time Limit Exceeded

Solution code:
import heapq

N, M = [int(_) for _ in input().split(' ')]
ABs = {}
r = 0

for i in range(N):
    A, B = [int(x) for x in input().split()]

    if A in ABs:
        heapq.heappush(ABs[A], -B)
    else:
        ABs[A] = [-B]
        heapq.heapify(ABs[A])

for m in range(M, 0, -1):
    B_max = -1
    A_max = -1

    for a in range(m, 0, -1):
        if not (a in ABs) or len(ABs[a]) == 0:
            continue

        b = - ABs[a][0]
        if B_max < b:
            A_max = a
            B_max = b

    if A_max != -1:
        r -= heapq.heappop(ABs[A_max])

print(r)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

jobs = {}

for i in range(N):
  d, r = map(int, input().split())
  
  if d not in jobs:
    jobs[d] = []  
  heapq.heappush(jobs[d], -r)
  
sum_rew = 0


for i in range(1, M + 1):
  max_reward = 0
  rew_index = 0
  job_gen = {j: jobs[j] for j in jobs if j <= i}
  
  if not job_gen:
    continue
  
  rew_index = max(list(job_gen.items()), key=lambda x: -x[1][0])[0]
  max_reward = -heapq.heappop(jobs[rew_index])
  
  if not jobs[rew_index]:
    del jobs[rew_index]
    
  sum_rew += max_reward

print(sum_rew)

Submission status: Time Limit Exceeded

Solution code:

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

AB_list = [list(map(int,input().split())) for i in range(N)]
check = [0]*N
B = 0
flag = 0
payment = 0
for i in range(1, M+1):
    for j in range(0, N):
        if (AB_list[j][0] <= i) and (check[j] == 0) and (AB_list[j][1] > B):
            B = AB_list[j][1]
            c = j
            flag = 1
        
    payment = payment + B
    if flag == 1:
        check[c] = 1
    B = 0

print(payment)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
 
a_list = []
b_list = []
for i in range(n):
    a,b = map(int, input().split())
    a_list.append(a)
    b_list.append(b)
 
ans = 0
for i in range(m):
    i = i+1
    if min(a_list) <= i:
        # ind = a_list.index(max(b_list[list(map(lambda x : x < i+1,b_list))]))
        max_num = max( map(lambda y: y[0] * (y[1] <=i) ,zip(b_list,a_list)))
        # ind = a_list.index(max(b_list * list(map(lambda x : x <= i,a_list))))
        ind = b_list.index(max_num)
        ans = ans + b_list[ind]
        a_list[ind] = m + 1
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq 
input = sys.stdin.readline
n,m = map(int,input().split())
alwork = [() for i in range(10**5+1)]
w = []
hpop = heapq.heappop
hpush = heapq.heappush
ans = 0

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

for i in range(1,m+1):
  for j in alwork[i]:
    hpush(w,j)
  if w:
    ans += hpop(w)
print(-ans)

Submission status: Time Limit Exceeded

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

# from operator import neg
from bisect import bisect_right
from itertools import accumulate


def solve(N, M, A, B):
    ab = dict()
    for a, b in zip(A, B):
        if a in ab:
            ab[a].append(b)
        else:
            ab[a] = [b]

    keys = []
    for a in ab:
        ab[a] = sorted(ab[a], reverse=True)
        keys.append(a)
    keys = sorted(keys)

    mxs = list(accumulate([ab[key][0] for key in keys], max))

    total = 0
    for i in range(1, M+1):
        if i < keys[0]:
            continue
        # print('i=', i)
        mx = 0
        pos = -1
        for j in range(bisect_right(keys, i)):
            # print('  j=', j)
            key = keys[j]
            if ab[key][0] > mx:
                mx = ab[key][0]
                pos = j
            if mxs[j] == mx:
                break
        if pos >= 0:
            total += mx
            key = keys[pos]
            ab[key] = ab[key][1:]
            if len(ab[key]) == 0:
                keys = keys[0:pos] + keys[pos+1:]
                if len(keys) == 0:
                    break
            # print(' ab=', ab)
            # print(' keys=', keys)

    return total


if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [0] * N
    B = [0] * N
    for idx in range(N):
        A[idx], B[idx] = map(int, input().split())

    ans = solve(N, M, A, B)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq 
input = sys.stdin.readline
n,m = map(int,input().split())
alwork = {}
w = []
hpop = heapq.heappop
hpush = heapq.heappush
ans = 0

for i in range(n):
  a,b = map(int,input().split())
  if a not in alwork:
    alwork[a] = (-b,)
  else:
    alwork[a] += (-b,)

for i in range(1,m+1):
  if i in alwork:
    for j in alwork[i]:
      hpush(w,j)
  if w:
    ans += hpop(w)
print(-ans)

Submission status: Time Limit Exceeded

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

# from operator import neg
from bisect import bisect_right


def solve(N, M, A, B):
    ab = dict()
    for a, b in zip(A, B):
        if a in ab:
            ab[a].append(b)
        else:
            ab[a] = [b]

    keys = []
    for a in ab:
        ab[a] = sorted(ab[a], reverse=True)
        keys.append(a)
    keys = sorted(keys)

    # print('ab=', ab)
    # print('keys=', keys)

    total = 0
    for i in range(1, M+1):
        if i < keys[0]:
            continue
        # print('i=', i)
        mx = 0
        pos = -1
        for j in range(bisect_right(keys, i)):
            # print('  j=', j)
            key = keys[j]
            if ab[key][0] > mx:
                mx = ab[key][0]
                pos = j
        if pos >= 0:
            total += mx
            key = keys[pos]
            ab[key] = ab[key][1:]
            if len(ab[key]) == 0:
                keys = keys[0:pos] + keys[pos+1:]
                if len(keys) == 0:
                    break
            # print(' ab=', ab)
            # print(' keys=', keys)

    return total


if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [0] * N
    B = [0] * N
    for idx in range(N):
        A[idx], B[idx] = map(int, input().split())

    ans = solve(N, M, A, B)

    print(ans)

Submission status: Time Limit Exceeded

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

from operator import neg
from bisect import bisect_left


MIN = -10**5-1


def solve(N, M, A, B):
    ab = list(sorted(zip(A, map(neg, B))))
    len_ab = len(ab)
    mx = 0
    for i in range(len_ab):
        if mx > ab[i][1]:
            mx = ab[i][1]
        ab[i] = tuple(list(ab[i]) + [mx])

    total = 0
    for i in range(1, M+1):
        right = bisect_left(ab, (i, MIN))
        if right >= len(ab):
            right -= 1
        if ab[right][0] > i:
            right -= 1

        pos = -1
        bmax = -1
        for j in range(right, -1, -1):
            if -ab[j][1] > bmax:
                bmax = -ab[j][1]
                pos = j
            if ab[j][1] == ab[j][2]:
                break
        if pos >= 0:
            ab = ab[0:pos] + ab[pos+1:]
            total += bmax
    return total


if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [0] * N
    B = [0] * N
    for idx in range(N):
        A[idx], B[idx] = map(int, input().split())

    ans = solve(N, M, A, B)

    print(ans)

Submission status: Time Limit Exceeded

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

from operator import neg
from bisect import bisect_left


MIN = -10**5-1


def solve(N, M, A, B):
    ab = list(sorted(zip(A, map(neg, B))))
    total = 0
    for i in range(1, M+1):
        right = bisect_left(ab, (i, MIN))
        if right >= len(ab):
            right -= 1
        if ab[right][0] > i:
            right -= 1

        pos = -1
        bmax = -1
        for j in range(right, -1, -1):
            if -ab[j][1] > bmax:
                bmax = -ab[j][1]
                pos = j
        if pos >= 0:
            ab = ab[0:pos] + ab[pos+1:]
            total += bmax
    return total


if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [0] * N
    B = [0] * N
    for idx in range(N):
        A[idx], B[idx] = map(int, input().split())

    ans = solve(N, M, A, B)

    print(ans)

Submission status: Time Limit Exceeded

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

t=0
for i in range(m):
   s += l[i]
   if len(s)>0:
      s.sort(reverse=True)
      t+=s.pop(0)

print(t)

Submission status: Time Limit Exceeded

Solution code:
#coding utf-8
import heapq
N,M=map(int,input().split())
AB= [[int(i) for i in input().split()] for i in range(N)]
work=[]
heapq.heapify(work)
ans=0
for i in range(1,M+1):
  for j in range(N):
    if AB[j][0]==i:
      heapq.heappush(work,-AB[j][1])
  if work:
    tmp=heapq.heappop(work)
    ans-=tmp
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000)
N,M = map(int,input().split())
AB = []
for n in range(N):
    a,b = map(int,input().split())
    AB.append([a,b])
avalist=[i for i in range(N)]
AB.sort(key=lambda k:k[0])
def maxReward(avalist,date):
    #print(avalist)
    mreward = 0
    if avalist == None:
        return 0
    else:
        count=0
        for i,ava in enumerate(avalist):
            
            if date+AB[ava][0] <= M:
                count=1
                
                #print(f'{ava} in {avalist}')
                newavalist= avalist[:i]+avalist[i+1:]
                reward = AB[ava][1] + maxReward(newavalist, date+1)
                if mreward < reward:
                    mreward = reward
            else:#sortされているので、これ以降はすべてelseになる。よってbreakしてよい
                break
              #  print(f'reject {ava}')
        if count==0:#有効なバイトがない
            return 0
        else:
            return mreward
print(maxReward(avalist, 0))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000)

N,M = map(int,input().split())
A = []
B = []
for n in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
avalist=[i for i in range(N)]
def maxReward(avalist,date):
    #print(avalist)
    mreward = 0
    if avalist == None:
        return 0
    else:
        count=0
        for i,ava in enumerate(avalist):
            
            if date+A[ava] <= M:
                count=1
                
                #print(f'{ava} in {avalist}')
                newavalist= avalist[:i]+avalist[i+1:]
                reward = B[ava] + maxReward(newavalist, date+1)
                if mreward < reward:
                    mreward = reward
           # else:
              #  print(f'reject {ava}')
        if count==0:#有効なバイトがない
            return 0
        else:
            return mreward
print(maxReward(avalist, 0))

Submission status: Time Limit Exceeded

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


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

    ans = 0
    for i in range(1, M+1):
        ab = list(filter(lambda x: x[1] <= i, W))
        if len(ab) == 0:
            continue
        heapq.heapify(ab)
        ans -= ab[0][0]
        W.remove(ab[0])

    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
i = list(map(int, input().split()))
S = []
T = []
ans = 0
for a in range(i[0]):
    j = list(map(int, input().split()))
    S.append(j)
S.sort()
for a in range(i[1]):
    can = []
    for b in range(len(S)):
        if S[b][0] < a + 2:
            can.append(S[b])
        else:
            break
    can1 = sorted(can, key=lambda x: x[1])
    if len(can1) != 0:
        T.append(can1[len(can1) - 1][1])
        S.remove(can1[len(can1) - 1])
for a in range(len(T)):
    ans += T[a]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq
n,m = map(int,input().split())
l = [0]*n
for i in range(n):
  l[i] = list(map(int,input().split()))
l.sort()

L = [[] for i in range(m+1)]
for i in range(n):
  if l[i][0] <= m:
    L[l[i][0]].append(l[i][1])

a = []
ans = 0
for i in range(m):
  key = i+1
  for j in L[key]:
    a.append(-j)
  if a != []:
    heapq.heapify(a)
    ans += heapq.heappop(a)
print(-ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        ab.append((a,b))
        
    ab = sorted(ab, key=lambda x:(x[0],-x[1]))
    pq = []
    heapify(pq)
    
    ans = 0
    for i in range(1,M+1):
        ind = -1
        for j,val in enumerate(ab):
            a,b = val
            ind = -1
            if a > i:
                break
            if i == a:
                heappush(pq, -b)
                ind = j
                
        if len(pq) > 0:
            tmp = heappop(pq) * (-1)
            ans += tmp
            
        if ind != -1:
            ab = ab[ind+1:]
    
    print(ans)
    
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        ab.append((a,b))
        
    ab = sorted(ab, key=lambda x:(x[0],-x[1]))
    pq = []
    heapify(pq)
    
    ans = 0
    for i in range(1,M+1):
        for j,val in enumerate(ab):
            a,b = val
            if a > i:
                break
            if i == a:
                heappush(pq, -b)
                
        if len(pq) > 0:
            tmp = heappop(pq) * (-1)
            ans += tmp
    
    print(ans)
    
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        ab.append((a,b))
        
    ab = sorted(ab, key=lambda x:(x[0],-x[1]))
    
    ans = 0
    for i in range(1,M+1):
        tmp = 0
        ind = -1
        memo = -1
        for j,val in enumerate(ab):
            a,b = val
            if a > i:
                break
            if memo == a:
                continue
            else:
                memo = a
            if tmp < b:
                tmp = b
                ind = j
        if ind != -1:
            ans += tmp
            ab.pop(ind)
    
    print(ans)
    
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,M = inputMap()
    ab = []
    for i in range(N):
        a,b = inputMap()
        if a > M:
            continue
        ab.append((a,b))
        
    ab = sorted(ab, key=lambda x:(x[0],-x[1]))
    
    ans = 0
    for i in range(1,M+1):
        tmp = 0
        ind = -1
        for j,val in enumerate(ab):
            a,b = val
            if a > i:
                break
            if tmp < b:
                tmp = b
                ind = j
        if ind != -1:
            ans += tmp
            ab.pop(ind)
    
    print(ans)
    
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
import heapq
n,m = 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]))
b = []
heapq.heapify(l)
j = 0
q = 0
for i in range(m):
  key = i+1
  if j >= n+1:
    break
  while j < n:
    if l[j][0] != key:
      break
    heapq.heappush(b,-l[j][1])
    j += 1
  if b != []:
    heapq.heapify(b)
    k = -heapq.heappop(b)
    q += k
print(q)

Submission status: Time Limit Exceeded

Solution code:

#%%
import heapq

N, M = map(int, input().split())
l = [0] * N
for i in range(N):
    l[i] = list(map(int, input().split()))

l.sort()
#print(l)

L = [[] for _ in range(M+1)]
for i in range(N):
    if l[i][0] <= M:
        L[l[i][0]].append(l[i][1]) 

#print(L)


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

print(-sum(ans))

#%%

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().split())
cond = []
for i in range(n):
  a,b = map(int, input().split())
  cond.append([a,b])
cond.sort()
a = [cond[i][0] for i in range(n)]
b = [cond[i][1] for i in range(n)]
money = 0

for t in reversed(range(0,m+1)): 
  a_new = []
  for i in range(len(a)):
    if a[i]<=m-t:
      a_new.append(a[i])
  #その中の最大値を足す
  new_b = b[:len(a_new)]
  if len(new_b)>0:
    b_max = max(new_b)
    b_max_idx = new_b.index(b_max)
    money += max(new_b)
    a.remove(a[b_max_idx])
    b.remove(b[b_max_idx])  
  
print(money)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())

table = [list(map(int, input().split())) for k in range(n)]
table.sort()

ans = 0

for day in range(1, m+1):
    
    choice = False
    pay = 0
    
    for t in table:
        if t[0] > day:
            break
        else:
            if pay < t[1]:
                pay = t[1]
                choice = t
                
    if choice:
        table.remove(choice)
        
    ans += pay
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

ab = [None] * N

for i in range(N):
    a,b = map(int, input().split())
    ab[i] = [a,-b]

q = []
ans = 0
for day in range(1, M+1):
    for i in range(N):
        if ab[i][0] == day:
            heapq.heappush(q, ab[i][1])

    if q != None and len(q) != 0:
        ans += -1 * heapq.heappop(q)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
jobs = []
ans = 0
for i in range(n):
    a, b = map(int, input().split())
    jobs.append([a, b])
for i in range(1, m+1):
    today_income = [job[1] for job in jobs if job[0] <= i]
    if today_income:
        today_max_income = max(today_income)
        max_income_payment_day = [job[0] for job in jobs if job[1] == today_max_income]
        jobs.remove([max(max_income_payment_day), today_max_income])
        ans += today_max_income
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

def solve():
  total=0
  for i in range(m,-1,-1):
#  print("i:"+str(i))
    max=0
    max_j=0
    found=False
    for j in range(len(w)):
      if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
        break
      elif max < w[j][1]:
#        print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
        max=w[j][1]
        max_j=j
        found=True
#  print(str(max) + "の仕事をやる")
    total = total + max
    if found:
#    print(str(max_j)+"番目の仕事を削除")
      w.pop(max_j)
#    print(w)
  return total
      
    
print(solve())

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
w=[]
for i in range(n):
  w.append(list(map(int,input().split())))

w=sorted(w)
#print(w)

total=0
for i in range(m,-1,-1):
#  print("i:"+str(i))
  max=0
  max_j=0
  found=False
  for j in range(len(w)):
    if (m-i)<w[j][0]: #残り日数(m-i)よりも大きい
#      print("残り日数:"+str(m-i) + "　仕事日数:"+str(w[j][0]))
      break
    elif max < w[j][1]:
#      print("最大値:"+str(w[j][1]) + " 添え字:"+str(j))
      max=w[j][1]
      max_j=j
      found=True
#  print(str(max) + "の仕事をやる")
  total = total + max
  if found:
#    print(str(max_j)+"番目の仕事を削除")
    w.pop(max_j)
#    print(w)
      
      
print(total)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = [int(a) for a in input().split()]

AB = []

for _ in range(N):
    a, b = input().split()
    AB.append((int(a), int(b)))

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

used_ind = []
sum = 0

for remained_days in range(M+1):
    max_B = 0
    max_ind = -1

    for i in range(N):
        if i in used_ind:
            continue
        if AB[i][0] > remained_days:
            break
        if AB[i][1] > max_B:
            max_ind = i
            max_B = AB[i][1]
    remained_days -= AB[max_ind][0]
    sum += max_B
    used_ind.append(max_ind)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, M = [int(a) for a in input().split()]

A = []
B = []

for _ in range(N):
    a, b = input().split()
    A.append(int(a))
    B.append(int(b))

used_ind = []
sum = 0

for remained_days in range(M+1):
    max_B = 0
    max_ind = -1

    for i in range(N):
        if i in used_ind:
            continue
        if A[i] > remained_days:
            continue
        if B[i] > max_B:
            max_ind = i
            max_B = B[i]
    remained_days -= A[max_ind]
    sum += max_B
    used_ind.append(max_ind)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter

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

	ab.sort(key=itemgetter(1,0),reverse=True)

	md=[i for i in range(m)]
	
	def searchMd(x):
		for i, d in enumerate(md):
			if d<x:
				continue
			else:
				del(md[i])
				return True
		return False

	ans=0
	for p in ab:
		if searchMd(p[0]-1):
			ans+=p[1]
	print (ans)
main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter

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

	ab.sort(key=itemgetter(1,0),reverse=True)

	md=[0]*m
	
	d=0
	
	def searchMd(x):
		for i in range(x,m):
			if md[i]!=0:
				continue
			else:
				md[i]=1
				return True
		return False

	ans=0
	for p in ab:
		if searchMd(p[0]-1):
			ans+=p[1]
			d+=1
			if d>=m:break
	print (ans)
main()

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
L={}

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

ans=0
tab=[0 for i in range(m)]
for i in range(m):
    if i+1 in L:
        tab.extend(L[i+1])
    tab.sort()
    ans+=tab.pop(-1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from operator import itemgetter

def main():
	input = sys.stdin.readline
	n,m = map(int,input().split())
	ab = [[int(item) for item in input().split()] for _ in range(n)]
	
	ab.sort(key=itemgetter(1,0),reverse=True)
	
	md=[0]*m
	
	def searchMd(x):
		for i in range(x,m):
			if md[i]!=0:continue
			else:
				md[i]=1
				return True
		return False
	ans=0
	for p in ab:
		if searchMd(p[0]-1):ans+=p[1]
	
	print (ans)
main()

Submission status: Time Limit Exceeded

Solution code:
n, m = list(map(int, input().split()))
job_r = [[] for _ in range(m)]

for i in range(n):
    d, r = list(map(int, input().split()))
    if d-1 < m:
        job_r[d-1].append(r)

total_reward = 0
aaa_list = []
for i in range(m):
    aaa_list.extend(job_r[i])
    if len(aaa_list) >0:
        reward = max(aaa_list)
        total_reward += reward
        aaa_list.remove(reward)
print(total_reward)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n,m = map(int,input().split())
data = [tuple(map(int,input().split())) for i in range(n)]
data.sort(key = lambda tup:tup[0])
salary =[]
ans=0
prev=0
memo=0
for day in range(m):
	for i in range(memo,n):
		if data[memo][0]<=(day+1):
			bisect.insort(salary,data[memo][1])
			# salary.append(data[memo][1])
			memo+=1
		else:
			break

	# ans+=salary.pop(max)
	if len(salary)!=0:
		ans+=salary.pop()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math, heapq
from operator import itemgetter as ig
from collections import defaultdict as dd
# 定数
INF = float("inf")
MOD = int(1e9 + 7)
# ヒープ
class heapque:
    def __init__(self, *args):
        self.que = []
        for arg in args:
            self.push(arg)
    def push(self, v):
        heapq.heappush(self.que, v)
    def pop(self):
        return heapq.heappop(self.que)
# 最大公約数 / 最小公倍数
def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2
# 二分探索
def bsr(a, v, lo=0, hi=None):
    if hi == None:
        hi = len(a) - 1
    if hi < lo:
        return lo
    mi = (lo + hi) // 2
    if v < a[mi]:
        return bsr(a, v, lo, mi - 1)
    else:
        return bsr(a, v, mi + 1, hi)
# Union-Find木
class uft:
    def __init__(self, n):
        self.height = [1] * n
        self.group = [-1] * n
    def root(self, v):
        if self.group[v] < 0:
            return v
        self.group[v] = self.root(self.group[v])
        return self.group[v]
    def size(self, v):
        return - self.group[self.root(v)]
    def merge(self, v1, v2):
        v1, v2 = self.root(v1), self.root(v2)
        if v1 == v2:
            return
        if self.height[v1] < self.height[v2]:
            self.group[v2] += self.group[v1]
            self.group[v1] = v2
            self.height[v2] = max(self.height[v1] + 1, self.height[v2])
        else:
            self.group[v1] += self.group[v2]
            self.group[v2] = v1
            self.height[v1] = max(self.height[v1] , self.height[v2] + 1)
# グラフ
class graph:
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
    def append(self, v1, v2, cost=1):
        self.graph[v1].append((v2, cost))
    # 最短経路：ダイクストラ法(Dijkstra's Algorithm)
    def dks(self, v):
        costs = [INF] * self.n
        costs[v] = 0
        done = [False] * self.n
        heap = heapque((0, v))
        while heap.que:
            c_cost, c_index = heap.pop()
            if done[c_index]:
                continue
            done[c_index] = True
            for n_index, n_cost in self.graph[c_index]:
                if c_cost + n_cost < costs[n_index]:
                    costs[n_index] = c_cost + n_cost
                    heap.push((costs[n_index], n_index))
        return costs

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

    ab = sorted(AB)
    heap = heapque()
    v = 0
    for m in range(1, M + 1):
        while ab:
            if ab[0][0] <= m:
                _, b = ab.pop(0)
                heap.push(-b)
            else:
                break
        if heap.que:
            v += -heap.pop()
    print(v)

main()

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
data = [tuple(map(int,input().split())) for i in range(n)]
data.sort(key = lambda tup:tup[0])
salary =[]
ans=0
prev=0
memo=0
for day in range(m):
	for i in range(memo,n):
		if data[memo][0]<=(day+1):
			salary.append(data[memo][1])
			memo+=1
		else:
			break

	# ans+=salary.pop(max)

	ans+=max(salary ,default=0)
	if max(salary ,default=0) !=0:
		del salary[salary.index(max(salary))]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
data = [tuple(map(int,input().split())) for i in range(n)]
data.sort(key = lambda tup:tup[0])
salary =[]
ans=0
prev=0
memo=0
for day in range(m):
	for i in range(memo,n):
		if data[memo][0]<=(day+1):
			salary.append(data[memo][1])
			memo+=1
		else:
			break
	ans+=max(salary ,default=0)
	if max(salary ,default=0) !=0:
		salary[salary.index(max(salary))]=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left as sort

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

n,m = map(int,input().split())
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
list = sorted(list)
for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      sort(temp,list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans += temp.pop()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left as sort

n,m = map(int,input().split())
list = [[int(i) for i in input().split()] for i in range(n)]
temp = [0]
ans = 0
a = 0
list = sorted(list)
for i in range(1,1+m):
  for j in range(a,len(list)):
    if list[j][0] == i:
      sort(temp,list[j][1])
    else:
      a = j
      break
  if len(temp) > 0:
    ans += temp.pop()
print(ans)