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:
def main():
    [N, M] = [int(number) for number in input().split()]
    part_times = []
    for i in range(N):
        job= [int(number) for number in input().split()]
        part_times.append(job)
    part_times = [part_time for part_time in sorted(part_times, key = lambda x: x[1], reverse=True) if part_time[0] <= M]
    money = 0
    #day = 0
    #earns = []
    for day in range(M):
        try:
            todays_part = list(filter(lambda x: x[0] <= day + 1, part_times))[0]
            money += todays_part[1]
            part_times.remove(todays_part)
        except:
            continue

    """
    for [income_day, earn] in part_times:
        remain_day = M - day
        if remain_day >= income_day:
            money += earn
        else:
            min_earn = min(earns)
            if earn > min_earn:
                money += earn
                money -= min_earn
                earns.remove(min_earn)
        earns.append(earn)
        day += 1
    """
    print(money)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

lis = [[1,0]]
ans = 0
for i in range(1,m+1):
  for ab in AB:
    if ab[0]==i:
      lis.append(ab)
    else:
      pass
  lis.sort(key = lambda x:x[1],reverse = True)
  ans += lis[0][1]
  lis[0][1] = 0
print(ans)

Submission status: Time Limit Exceeded

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

ans=0
N,M=map(int,input().split())
Ar=[[] for _ in range(M)]
for _ in range(N):
      A,B=map(int,input().split())
      if A <= M:
          Ar[A-1].append(B)
An=[]
for i in range(M):
      An += Ar[i]
      if len(An)>0:
          ans+=An.pop(An.index(max(An))) 
print(ans)

Submission status: Time Limit Exceeded

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

ans=0
N,M=map(int,input().split())
Ar=[[] for _ in range(M)]
for _ in range(N):
      A,B=map(int,input().split())
      if A <= M:
          Ar[A-1].append(B)
An=[]
for i in range(M):
      An += Ar[i]
      if len(An)>0:
          ans+=An.pop(An.index(max(An))) 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
import heapq
from collections import defaultdict
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N,M = [int(n) for n in input().split()]
    # {期限:価値}
    jobs = defaultdict(list)
    v = 0
    for i in range(N):
        a,b = [int(n) for n in input().split()]
        if a <= M:
            jobs[a].append(-b)
    cand = []
    ans = 0
    for i in range(1,M+1):
        if i in jobs:
            cand += jobs[i]
            heapq.heapify(cand)
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
import heapq
from collections import defaultdict
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N,M = [int(n) for n in input().split()]
    # {期限:価値}
    jobs = defaultdict(list)
    v = 0
    for i in range(N):
        a,b = [int(n) for n in input().split()]
        if a <= M:
            jobs[a].append(-b)
    cand = []
    ans = 0
    for i in range(1,M+1):
        if i in jobs:
            cand += jobs[i]
        # debug(cand,"cand")
        heapq.heapify(cand)
        if len(cand) != 0:
            ans -= cand[0]
            heapq.heappop(cand)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

def main():
    N,M = map(int,input().split())
    paylist = [[] for _ in range(M)]
    for _ in range(N):
        a,b = map(int,input().split())
        if M >= a:
            paylist[a-1].append(b)

    work = []
    payment = 0
    for i in range(M):
        work += paylist[i]
        work.sort()
        if work != []:
            payment += work.pop()

    print(payment)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    N,M = map(int,input().split())
    paylist = [[] for _ in range(M)]
    for _ in range(N):
        a,b = map(int,input().split())
        if M >= a:
            paylist[a-1].append(b)

    work = []
    payment = 0
    for i in range(M):
        work += paylist[i]
        work.sort()
        if work != []:
            payment += work.pop()

    print(payment)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

    X.sort(key=lambda X:X[0])
    day = 1
    work = []
    payment = 0
    while day <= M:
        for i in range(N):
            if X[i][0] == day:
                work.append(X[i][1])
                work.sort()
        if work != []:
            payment += work.pop()
        day += 1

    print(payment)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

n, m = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
ls.sort(key=lambda x: x[0])
la = [x[0] for x in ls]
lb = [x[1] for x in ls]

selected = []
res = 0
for i in range(1, m + 1):
    k = bisect.bisect(la, i)
    bs = lb[:k]
    index = -1
    temp = 0
    for j, b in enumerate(bs):
        if j in selected:
            continue
        if temp < b:
            index = j
            temp = b
    if index >= 0:
        selected.append(index)
        res += temp

print(res)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ansList = []
newList = []
jobList = [[] for _ in range(100001)]
for i in range(N):
    A, B = map(int, input().split())
    jobList[A-1].append(B)

for j in range(M):
    newList = newList + jobList[j]
    newList.sort()
    try:
        ansList.append(newList.pop())
    except IndexError:
        ansList.append(0)
print(sum(ansList))

Submission status: Time Limit Exceeded

Solution code:
#D問題
import heapq
N,M = map(int,input().split())
AB = []
for i in range(N):
    ab = list(map(int,input().split()))
    heapq.heappush(AB,ab)
    
ans = 0
cand = []
for i in range(M):
    for ab in AB:
        if ab[0] <= (i+1):
            heapq.heappush(cand,10**5-ab[1])
            ind = AB.index(ab)
            del AB[ind]
    
    if len(cand) >= 1:
        cd = heapq.heappop(cand)
        ans+=(10**5-cd)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lis = [[] for _ in range(10**5)]
for _ in range(n):
  a,b = map(int,input().split())
  lis[a-1].append(b)
ans = 0
li = []
for i in range(m):
  li += lis[i]
  li.sort(reverse=True)
  try:
    ans += li[0]
    del(li[0])
  except:
    continue
print(ans)

Submission status: Time Limit Exceeded

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


def main(A, m):
    # print(A)
    count = 0
    max_list = [0]*m
    for i in range(m):
        A[i] = sorted(A[i])
        max_list[i] = A[i].pop()
        mx = max(max_list)
        # print(max_list)
        for j in range(m):
            if mx == max_list[j]:
                if A[j] != []:
                    max_list[j] = A[j].pop()
                else :
                    max_list[j] = 0
                break
        count += mx
    return count


if __name__ == "__main__":
    n, m = map(int, input().split())
    A = [[0] for _ in range(m)]
    for _ in range(n):
        a, b = map(int, input().split())
        if a <= m:
            A[a-1].append(b)
    print(main(A, m))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
baitos = [list(map(int, (input().split()))) for _ in range(n)]
baitos = sorted(baitos, key=(lambda x: x[0]))

max = 0
available_works = []
for shigotobi in reversed(range(m)):
    nokori = m-shigotobi
    #kuwaeru
    for baito_index in range(len(baitos)):
        if baitos[baito_index][0] <= nokori:
            available_works.append(baitos[baito_index])
        else:
            baitos = baitos[baito_index:]
            break

    #itibanniiyatu
    max_now = 0
    max_index = None

    for work_index in range(len(available_works)):
        if available_works[work_index][1] >= max_now:
            max_index = work_index
            max_now = available_works[max_index][1]

    if max_index is not None:
        max += max_now
        available_works.pop(max_index)
        max_index = None

print(max)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

for i in range(N):
    a,b = list(map(int,input().split()))
    if a <= M:
        A[a-1].append(-b)
    
ans = 0
cand = []

for i in range(M):
    
    for j in A[i]:
        cand.append(j)
        
    if cand:
        heapq.heapify(cand)
        ans -= heapq.heappop(cand)
    
print(ans)

Submission status: Time Limit Exceeded

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

N, M = [int(x) for x in input().split()]
A = {}
for _ in range(N):
    x, y = [int(x) for x in input().split()]
    if x  not in A:
       A[x] = []
    A[x].append(y)

for key in A:
    A[key].sort()


total = 0
for x in range(1, M + 1):
    max = 0
    max_index = -1
    for k, v in sorted(A.items()):
        if k > x:
            break
        if v[-1] > max:
            max = v[-1]
            max_index = k
    if max_index != -1:
        total += A[max_index].pop()
        if len(A[max_index]) == 0:
            A[max_index].append(-1)


print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq
from collections import defaultdict
n, m = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

#print(dic)
L =[]
money = 0
for d in range(1, m+1):
    dic[d] = map(lambda x:-x, dic[d])
    L+=dic[d]
    heapq.heapify(L)
    if L:
        money +=heapq.heappop(L)
print(-money)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq
import numpy as np
from collections import defaultdict
n, m = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

#print(dic)
L =[]
money = 0
for d in range(1, m+1):
    L+=list(np.array(dic[d])*(-1))
    heapq.heapify(L)
    if L:
        money +=heapq.heappop(L)
print(-money)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
from operator import itemgetter

N, M = [int(x) for x in input().split()]
A = []
for _ in range(N):
    A.append([int(x) for x in input().split()])

A_sorted = sorted(A, key=itemgetter(0))

total = 0
for x in range(1, M + 1):
    max = 0
    max_index = -1
    for y, row in enumerate(A_sorted):
        if row[0] <= x:
            if row[1] > max:
                max = row[1]
                max_index = y
        else:
            break
    if max_index != -1:
        total += max
        A_sorted.pop(max_index)

print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
aa = [[] for i in range(m)]
ans = 0
max_b = [0] * m
for i in range(n):
    a,b = inpl()
    if a <= m:
        aa[a-1].append(b)
for i in range(m):
    if aa[i]:
        max_b[i] = max(aa[i])

for i in range(m):
    mmm = 0
    max_i = -1
    for j in range(i+1):
        if max_b[j] > mmm:
            mmm = max_b[j]
            max_i = j
    ans += mmm
    if len(aa[max_i]) and max_i != -1:
        aa[max_i].remove(mmm)
        if aa[max_i]:
            max_b[max_i] = max(aa[max_i])
        else :
            max_b[max_i] = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n,m = inpl()
aa = [[] for i in range(m)]
ans = 0
for i in range(n):
    a,b = inpl()
    if a <= m:
        aa[a-1].append(b)
for i in range(m):
    if aa[i]:
        aa[i].sort()
        ans += aa[i].pop()
        if i < m-1:
            for j in aa[i]:
                aa[i+1].append(j)
print(ans)

Submission status: Time Limit Exceeded

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

N, M = [int(x) for x in input().split()]
A = []
for _ in range(N):
    A.append([int(x) for x in input().split()])

total = 0

for x in range(1, M + 1):
    max = 0
    max_index = -1
    for y, row in enumerate(A):
        if row[0] <= x and row[1] > max:
            max = row[1]
            max_index = y
    if max_index != -1:
        total += max
        A.pop(max_index)

print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

input=sys.stdin.readline

N, M = [int(x) for x in input().split()]
A = []
for _ in range(N):
    A.append([int(x) for x in input().split()])

total = 0

for x in range(1, M + 1):
    max = 0
    max_index = -1
    for y, row in enumerate(A):
        if row[0] <= x and row[1] > max:
            max = row[1]
            max_index = y
    if max_index != -1:
        total += max
        A.pop(max_index)

print(total)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
n,m=map(int,input().split())
works = []
for _ in range(n):
	a,b=map(int,input().split())
	if a>m:
		continue
	works.append((a,b))
works.sort()
q=[]
ans=0

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
val =[[]for _ in range(10**5+1)]
for _ in range(n):
	a,b=map(int,input().split())
	val[a].append(b)
mval = [0]*(10**5
+1)
for i in range(1,m+1):
	val[i].sort(reverse=True)
	mval[i] = val[i][0] if val[i] else 0
ans = 0
for dayleft in range(1,m+1):
	cidx = mval.index(max(mval[1:dayleft+1]))
	if cidx == 0:
		continue
	ans += mval[cidx]
	del val[cidx][0]
	mval[cidx] = 0 if val[cidx]==[] else val[cidx][0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
l = input().split(" ")
N = int(l[0])
M = int(l[1])

jobs = []
for i in range(0, N):
    l = input().split(" ")
    jobs.append((int(l[0]), int(l[1])))

jobs.sort(key=lambda x: x[0])
jobs.sort(key=lambda x: x[1], reverse=True)

incomes = 0
for i in range(M-1, -1, -1):
    allowed = M - i
    for j in range(0, len(jobs)):
        if jobs[j][0] <= allowed:
            incomes = incomes + jobs[j][1]
            jobs.pop(j)
            break

print(incomes)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
n, m = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

L =[]
money = 0
for d in range(1, m+1):
    if dic[d]:
        L+= dic[d]
    if L:
        L.sort()
        money += L[-1]
        L.pop()
print(money)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
n, m = map(int, sys.stdin.readline().split())
a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dic = defaultdict(list)
for ele in a_b:
    dic[ele[0]].append(ele[1])

L =[]
money = 0
for d in range(1, m+1):
    L+= dic[d]
    if L:
        max_money = max(L)
        money +=max_money
        L.remove(max_money)
print(money)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
AB= []
for i in range(N):
    AB.append([int(j) for j in input().split()])
 
AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, key=lambda x: x[1], reverse=True)
#お金が高い＞日にちがかからない　の順にソート
 
for i in range(N):  #同じ仕事があっても消せるようにIDを振る
    AB[i].append(i)
 
money = 0
 
def NdayOK(n):  #あとn日残されているときに選べる仕事のリスト
    nd = []
    for i in range(N):
        if AB[i][0] <= n:
            nd.append(AB[i][2])
    return(nd)
 
checker = []
for i in range(M+1):
    NOi = NdayOK(i)
    for j in NOi:
        if AB[j][2] not in checker:
            money += AB[j][1]
            checker.append(AB[j][2])
            break
 
print(money)

Submission status: Time Limit Exceeded

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

AB = sorted(AB, key=lambda x: x[0])
AB = sorted(AB, key=lambda x: x[1], reverse=True)
#お金が高い＞日にちがかからない　の順にソート

for i in range(N):  #同じ仕事があっても消せるようにIDを振る
    AB[i].append(i)

money = 0

def NdayOK(n):  #あとn日残されているときに選べる仕事のリスト
    nd = []
    for i in range(N):
        if AB[i][0] <= n:
            nd.append(AB[i][2])
    return(nd)

checker = []
for i in range(M+1):
    NOi = NdayOK(i)
    for j in NOi:
        if AB[j][2] not in checker:
            money += AB[j][1]
            checker.append(AB[j][2])
            break

print(money)

Submission status: Time Limit Exceeded

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

AB = sorted(AB, key=lambda x: x[0], reverse=False)
AB = sorted(AB, key=lambda x: x[1], reverse=True)

for i in range(N):  #同じ仕事があっても消せるようにIDを振る
    AB[i].append(i)

money = 0

def NdayOK(n):  #あとn日残されているときに選べる仕事のリスト
    nd = []
    for i in range(N):
        if AB[i][0] <= n:
            nd.append(AB[i])
    return(nd)

checker = []
for i in range(M+1):
    NOi = NdayOK(i)
    for j in range(len(NOi)):
        if NOi[j][2] not in checker:
            money += NOi[j][1]
            checker.append(NOi[j][2])
            break

print(money)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = np.array([])
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
#a = list(a)
#b = list(b)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if a[j] + i == m:
			l = np.append(l,b[j])
			count += 1
		else:
			break
	if l.shape[0] != 0:
		maximum = np.max(l)
		maximum_index = np.argmax(l)
		ans += maximum
		l = np.delete(l,maximum_index)

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
#a = list(a)
#b = list(b)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break

	if len(l) != 0:
		maximum = max(l)
		ans += maximum
		l.pop(l.index(maximum))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readlines()
n,m = map(int,s[0].split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(1,n+1):
	a_dash,b_dash = map(int,s[i].split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break

	if len(l) != 0:
		maximum = max(l)
		ans += maximum
		l.pop(l.index(maximum))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)
a_length = len(a)

for i in reversed(range(m)):
	for j in range(count,a_length):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		maximum = max(l)
		ans += maximum
		l.pop(l.index(maximum))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,len(a)):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		maximum = max(l)
		ans += maximum
		l.pop(l.index(maximum))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,len(a)):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,count+a.count(m-i)):
		if a[j] + i == m:
			l.append(b[j])
			count += 1

	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	l_dash = [ b[j] for j in range(count,count+a.count(m-i)) if a[j]+i==m ]
	l.extend(l_dash)
	count += len(l_dash)
	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	l_dash = [ b[j] for j in range(count,len(a)) if a[j]+i==m ]
	l.extend(l_dash)
	count += len(l_dash)
	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,len(a)):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,len(a)):
		if a[j] + i <= m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		ans += max(l)
		l.pop(l.index(max(l)))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
l = []
ans = 0
count = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

c = zip(a,b)
c = sorted(c)
a,b = zip(*c)
a = list(a)
b = list(b)

for i in reversed(range(m)):
	for j in range(count,len(a)):
		if a[j] + i == m:
			l.append(b[j])
			count += 1
		else:
			break
	if len(l) != 0:
		ans += max(l)
		#index = b.index(max(l))
		#a.pop(index)
		#b.pop(index)
		l.pop(l.index(max(l)))
	#l.clear()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a = []
b = []
c = 0
l = []
ans = 0

for i in range(n):
	a_dash,b_dash = map(int,input().split())
	a.append(a_dash)
	b.append(b_dash)

for i in reversed(range(m)):
	for j in range(n-c):
		if a[j] + i <= m:
			l.append(b[j])
	if len(l) != 0:
		ans += max(l)
		index = b.index(max(l))
		a.pop(index)
		b.pop(index)
		c += 1
	l.clear()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
ans = 0
hq = []
for i in range(1, m+1):
    for a, b in ab:
        if a == i:
            heappush(hq, (-b, a))
        
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

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

ans = 0
hq = []
for i in range(1, m+1):
    for a, b in ab:
        if a == i:
            heappush(hq, (-b, a))
    
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

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

ans = 0
hq = []
for i in range(1, m+1):
    for a, b in ab:
        if a == i and not -b in hq:
            heappush(hq, (-b, a))
    
    if len(hq) != 0:
        ans -= heappop(hq)[0]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def slove():
    import sys, heapq
    input = sys.stdin.readline
    n, m = list(map(int, input().rstrip('\n').split()))
    ab = []
    for i in range(n):
        a, b = list(map(int, input().rstrip('\n').split()))
        ab.append([a, b])
    heapq.heapify(ab)
    s_q = []
    heapq.heapify(s_q)
    d = m
    r = 0
    while True:
        while True:
            if len(ab) != 0:
                p = heapq.heappop(ab)
                if p[0] + d <= m:
                    heapq.heappush(s_q, -p[1])
                else:
                    heapq.heappush(ab, p)
                    break
            else:
                break
        if len(s_q) != 0:
            mr = -heapq.heappop(s_q)
            r += mr
        d -= 1
        if d == -1:
            break
    print(r)


if __name__ == '__main__':
    slove()

Submission status: Time Limit Exceeded

Solution code:
import heapq
N,M = map(int,input().split())
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l)
heap = []
a = 0
b = 0
for i in range(1,M+1):
        while l and l[a][0] <= i:
            heapq.heappush(heap, -(l[a][1]))
        if heap :
            b -= (heapq.heappop(heap))
print(b)

Submission status: Time Limit Exceeded

Solution code:
import heapq
N,M = map(int,input().split())
l = [tuple(map(int,input().split())) for _ in range(N)]
l = sorted(l)
heap = []
a = 0
b = 0
for i in range(1,M+1):
        while l and l[a][0] <= i:
            heapq.heappush(heap, -(l[a][1]))
        if heap :
            b -= (heapq.heappop(heap))
print(b)

Submission status: Time Limit Exceeded

Solution code:
import heapq
N,M = map(int,input().split())
l = [list(map(int,input().split())) for _ in range(N)]
l = sorted(l)
heap = []
a = 0
b = 0
for i in range(1,M+1):
    while l[a][0] <= i:
        a = 0
        heapq.heappush(heap,-(l[a][1]))
        a += 1
    if heap == []:
        continue
    else:
        b += -(heapq.heappop(heap))
print(b)

Submission status: Time Limit Exceeded

Solution code:
import heapq

n,m=map(int,input().split())
works={}
for i in range(n):
    day,pay=map(int,input().split())
    works.setdefault(day,[]).append(pay)
sorted_works=sorted(works.items(), key=lambda x:x[0])

heapls=[]
heapq.heapify(heapls)
ans=0
for i in range(m):
    day=i+1
    if sorted_works!=[] and sorted_works[0][0]==day:
        jobs_for_day=sorted_works.pop(0)
        for j in jobs_for_day[1]:
            heapq.heappush(heapls,(-j,j),)
    if heapls!=[]:
        tmp=heapq.heappop(heapls)
        ans+=tmp[1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
a = []
b = []
for i in range(N):
    L = list(map(int, input().split()))
    a.append(L[0])
    b.append(L[1])
a, b = map(list, zip(*sorted(zip(a, b))))

ans = 0
for i in range(M + 1):
    if len(a) == 0:
        break
    elif i >= a[0]:
        for j in range(len(a)):
            if i < a[j]:
                break
        else:
            j = j + 1
        ma = b.index(max(b[0:j]))
        ans += b.pop(ma)
        a.pop(ma)
print(ans)

Submission status: Time Limit Exceeded

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


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

max_m = [0]*(m+1)
m_list = [[0] for _ in range(m+1)]

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

    if a <= m:
        m_list[a].append(b)

all_r = 0

for i in range(1, m+1):
    max_m[i] = max(m_list[i])

    max_ind = 1
    max_v = max_m[1]
    for j in range(2, i+1):
        if max_m[j] >= max_v:
            max_ind = j
            max_v = max_m[j]

    ind = m_list[max_ind].index(max_v)
    all_r += m_list[max_ind].pop(ind)
    if len(m_list[max_ind]) == 0:
        m_list[max_ind].append(0)

    max_m[max_ind] = max(m_list[max_ind])

print(all_r)