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 bisect
n,m=map(int, input().split())
l={}
for i in range(n):
    a,b=map(int, input().split())
    if not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=[]
for i in range(1,m+1):
    if i in l:
        for j in l[i]:
            bisect.insort_left(target, j)

    if not target:
        val=0
    else:
        val=target.pop()
    res+=val
    #print(target, max(target))

print(res)

Submission status: Time Limit Exceeded

Solution code:
def A():
    a, b = map(int, input().split())
    lt = [
        a + b,
        a - b,
        a * b
    ]
    print(max(lt))


def B():
    k, x = map(int, input().split())

    answer = ''
    for i in range(2*k-1):
        answer += str(x-k+1+i) + ' '

    answer = answer[:-1]
    print(answer)


def C():
    N = int(input())
    literals = [set(input()) for _ in range(N)]

    answer = 0
    for i in range(N-1):
        for j in range(i+N):
            if literals[i] == literals[j]:
                answer += 1

    answer = int(answer/2)
    print(answer)


def D():
    N, M = map(int, input().split())
    jobs = [list(map(int, input().split())) for _ in range(N)]
    jobs.sort(key=lambda x: x[1], reverse=True)
    # rewards = [job[0] for job in jobs]
    # intarvals = [job[1] for job in jobs]

    reward = 0
    for i in range(1, M+1):
        # acceptable_jobs = [job for job in jobs if job[1] <= i]
        # target_job = [job[0]]
        for j in range(len(jobs)):
            if jobs[j][0] <= i:
                target = jobs.pop(j)
                reward += target[1]
                break
    print(reward)


if __name__ == '__main__':
    D()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    N, M = LMIIS()
    dp = [0] * (M+1)

    DV = [[0] for _ in range(M+1)]

    for _ in range(N):
        A,B = LMIIS()
        if A > M:
            continue
        DV[A].append(B)

    for l in DV:
        l.sort()
    ans = 0
    tmpL = []
    for l in DV:

        tmpL += l
        tmpL.sort()
        ans += tmpL.pop()
    print(ans)
        
        



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

Submission status: Time Limit Exceeded

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[0],reverse=True)
b = [i[1] for i in ab]
b_set = list(set(b))
d = {}
for i in b_set:
    d[i] = []
for i in ab:
    d[i[1]].append(i[0])
dk = sorted(list(d.keys()),reverse=True)
ans = 0
for i in range(1,m+1):
    x = 0
    for j in dk:
        if d[j]:
            if d[j][-1] <= i:
                y = d[j].pop(-1)
                x = j
                break
    ans += x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ABs = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(1, M + 1):
    max_ = [-1, 0]
    for AB in ABs:
        if AB[0] <= i and AB[1] > max_[1]:
            max_ = AB
    ans += max_[1]
    if max_[0] != -1:
        ABs.remove(max_)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
ab=[]
for i in range(n):
    ab.append([int(x) for x in input().split()])
ab = sorted(ab, key=lambda e: (-e[1], e[0]))
scores=([0]*m)
for i in range(n):
    if (m-ab[i][0]>-1):
        for j in range(m-ab[i][0],-1,-1):
            if (scores[j]==0):
                scores[j]=ab[i][1]
                break
print(sum(scores))

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N, M = map(int, input().split())
job_list = []
for i in range(M):
    job_list.append([])
for i in range(N):
    day, pay = map(int, input().split())
    if(day - 1 < M):
        job_list[day-1].append(pay)

now_date = 0
money = 0
now_job_list = []
for i in range(M):
    now_job_list.extend(job_list[i])
    if(len(now_job_list) > 0):
        now_job_list.sort()
        money += now_job_list.pop()
print(money)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
ba = []
days = [0 for _ in range(m + 1)]
for _ in range(n):
    a,b = map(int,input().split())
    ba.append([b,a])
ba.sort(reverse=True)
cnt = 0
for BA in ba:
    BA1 = BA[1]
    BA2 = BA[0]
    for i in range(m - BA1 + 1):
        if days[BA1 + i] == 0:
            days[BA1 + i] = BA2
            cnt += BA2
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

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

s = [i for i in range(1, M + 1)]
ret = 0
for a, b in AB :
    if a > s[-1] :
        continue
    
    a = bisect_left(s, a)
    s = s[:a] + s[a+1:]
    ret += b
    if not s :
        break
print(ret)

Submission status: Time Limit Exceeded

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

ans = 0

for j in range(1,m+1):
    # aがj以下で最大のbをもつもの
    bmax = 0
    bmaxind = n
    for i in range(n):
        if a[i] <= j and b[i] > bmax:
            bmax = b[i]
            bmaxind = i
    ans += bmax
    if bmaxind < n:
        a[bmaxind] = float('inf')
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split(" "))
xys = [tuple(map(int, input().split(" "))) for i in range(n)]
xys = sorted(xys, key=lambda x: (-x[0], x[1]))

dp = [[0 for x in range(m)] for y in range(n)]

for i in range(n):
    a, b = xys[i]
    # print(i, a, b)
    if i == 0:
        for j in range(0, m - a + 1):
            dp[i][j] = b
    else:
        dp[i][0] = max(dp[i - 1][0], b)
        for j in range(1, m - a + 1):
            dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + b)
    # print(dp[i])

# print(dp)
print(max(max(x) for x in dp))

Submission status: Time Limit Exceeded

Solution code:
import bisect
n,m=map(int, input().split())
l={}
for i in range(n):
    a,b=map(int, input().split())
    if not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=[0]*m
for i in range(1,m+1):
    if i in l:
        for j in l[i]:
            bisect.insort_left(target, j)
    val=target.pop()
    res+=val
    #print(target, max(target))

print(res)

Submission status: Time Limit Exceeded

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

d = dict()
key_all = []
for i in range(n):
    a = ab[i][0]
    b = ab[i][1]

    if a not in key_all:
        d[a] = [b]
        key_all.append(a)
    else:
        d[a].append(b)

key_all.sort()

ans = 0
l = []
import bisect

for i in range(1, m+1):
    if key_all != []:
        if i == key_all[0]:
            for v in d[i]:
                bisect.insort(l, v)

            key_all.pop(0)

    # print(l)

    if l != []:
        ans += l[-1]
        l.pop()
print(ans)

Submission status: Time Limit Exceeded

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


jobs = [[] for _ in range(m)]
for a, b in ab:
    if a <= m:
        jobs[a - 1].append(b)

ans = 0
available = []
for job in jobs:
    available += job
    available.sort()
    if available:
        ans += available.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N, M = map(int, input().split())
ab_list = [ list(map(int, input().split())) for i in range(N)]

score_list = [[] for i in range(M+1)]


for ab in ab_list:
    if ab[0] <= M:
        score_list[ab[0]].append(ab[1])

ans = 0
for i in range(0, M+1):
    if len(score_list[i]) == 0:
        continue
    scores = deque(sorted(score_list[i]))
    ans += scores.pop()
    if i < M and len(scores) > 0:
        score_list[i+1].extend(scores)

print(ans)

Submission status: Time Limit Exceeded

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

d = dict()
key_all = []
for i in range(n):
    a = ab[i][0]
    b = ab[i][1]

    if a not in key_all:
        d[a] = [b]
        key_all.append(a)
    else:
        d[a].append(b)

key_all.sort()

ans = 0
l = []
import bisect

for i in range(1, m+1):
    if key_all != []:
        if i == key_all[0]:
            for v in d[i]:
                bisect.insort(l, v)

            key_all = key_all[1:]

    # print(l)

    if l != []:
        ans += l[-1]
        l.pop()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
arr=sorted(arr,reverse=True,key=lambda x:x[0])
q=[]
tmp=0
cnt=0
for a,b in arr:
  if tmp!=a:
    tmp=a
    cnt=0
  if m-tmp<cnt:
    continue
  heapq.heappush(q,(-b,a))
  cnt+=1
ans=0
cnt=0
box=[0]*(m+1)
while len(q)!=0:
  if cnt>=m:
    break
  tmp,date=heapq.heappop(q)
  if box[date]==0:
    box[date]=1
    ans-=tmp
    cnt+=1
  else:
    pos=date
    while 1:
      if pos==m:
        break
      pos+=1
      if box[pos]==0:
        box[pos]=1
        ans-=tmp
        cnt+=1
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
ab = []
for i in range(n):
  a,b = map(int,input().split())
  ab.append([-a,b])
ab.sort()
ab.reverse()
#print(ab)
here = 0
ans = 0
pool = []
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      pool.append(ab[here][1])
      here += 1
    else:
      break
  pool.sort()
  try:
    ans += pool[-1]
    del pool[-1]
  except:
    pass
print(ans)

Submission status: Time Limit Exceeded

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


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):

    jobs_time = collections.defaultdict(list)
    for a,b in zip(A,B):
        jobs_time[b].append(a)

    jobs_time = sorted(jobs_time.items(),reverse=True)
    job_list = [0] * M

    for k,v in jobs_time: 
        v.sort()
        for vv in v:
            if vv <= M:
                for i in range(vv-1,len(job_list)):
                    if job_list[i] < k:
                        job_list[i] = k
                        break
            else:
                break

        if all(j>0 for j in job_list):
            break

    print(sum(job_list))

    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve():
    n, m = map(int, input().split())
    X = [0] + [-1] * 100010
    AB = sorted(list(map(int, input().split())) for _ in range(n))[::-1]
    for i, (a, b) in enumerate(AB):
        for j in range(min(i, m - a), -1, -1):
            if X[j] != -1:
                X[j + 1] = max(X[j + 1], X[j] + b)
    print(max(X))
solve()

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True)
x = [0] * M
cnt = 0
cntIN = M
preB = 0
for a, b in AB:
    if preB != b:
        k = M
    i = min(M-a, k)
    while i>=0 and x[i] != 0:
        i -= 1
    else:
        if i >= 0:
            x[i] = b
            k = i - 1
            cntIN -= 1
            if cntIN <= 0:
                break

print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ptj = []
ck = [0] * M
S = 0

for i in range(N):
  A, B = map(int, input().split())
  ptj.append((B,A))

ptj.sort()
ptj.reverse()

for i in range(N):
  jb = ptj[i]
  a = jb[1]
  b = jb[0]
  
  if min(ck[a-1:M]+[1]) == 0:
    ck[ck[a-1:M].index(0)+a-1] = 1
    S += b
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

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

s = [i for i in range(1, M + 1)]
ret = 0
for a, b in AB :
    if not s :
        break
    if a > s[-1] :
        continue
    
    a = bisect_left(s, a)
    s.pop(a)
    ret += b
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
import sys
import math

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


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

    AB = sorted(AB, key=lambda it: it[1])

    ARR = [[] for _ in range(M + 1)]
    for a, b in AB:
        if a > M:
            continue
        ARR[a].append(b)
    # print("ARR", ARR)

    res = 0
    q = []
    for i in range(1, M + 1):
        current = ARR[i]
        if len(current) != 0:
            if len(q) != 0:
                if current[-1] > q[-1]:
                    res += current.pop()
                else:
                    res += q.pop()
            else:
                res += current.pop()
            q = sorted(q + current)
            q = q[-(M - i):]
        else:
            if len(q) != 0:
                res += q.pop()
            else:
                pass
        # q += current
        # q = sorted(q)
        # # print(q)
        # if len(q) != 0:
        #     mm = q.pop()
        #     res += mm
        # q = q[-(M - i):]
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n ,m = map(int,input().split())
a = [list(map(int, input().split())) for i in range(n)]
a = sorted(a)
che = []
ans = 0
for i in range(1,m+1):
    count = 0
    for k in range(len(a)):
        k -= count
        if a[k][0] <= i:
            che.append(a.pop(k)[1])
            count += 1
        else:
            break;
    if len(che) > 0:
        ans += max(che)
        che.remove(max(che))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = list(map(int,input().split()))
dc = {}
for i in range(n):
    a,b = list(map(int,input().split()))
    if dc.get(a):
        dc[a] = dc.get(a)+[b]
    else:
        dc[a] = [b]

# print(dc)

ans = 0
cands = []
for i in range(1,m+1):
    p = dc.get(i)
    if p:
        cands.extend(p)
    if len(cands) > 0:
        q = max(cands)
        cands.remove(q)
        ans += q
    # print("---",cands)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N,M = map(int,input().split())
D = [[] for i in range(M)]
for i in range(N):
    A,B = map(int,input().split())
    if A <= M:
        D[A-1].append(B)
count = 0
Tax = []
len = 0
for i in range(M):
    for j in D[i]:
        bisect.insort_left(Tax,j)
        len += 1
    if not Tax:
        continue
    count += Tax.pop(len-1)
    len -= 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

N, M = map(int, input().split())
q = list()
for _ in range(N):
    A, B = map(int, input().split())
    deadline = M - A
    reward_inv = -B
    if deadline >= 0:
        heappush(q, (reward_inv, deadline))

ans = [0] * (M + 1)
while len(q) > 0:
    reward_inv, deadline = heappop(q)
    while ans[deadline] != 0 and deadline > 0:
        deadline -= 1
    if ans[deadline] < -reward_inv:
        ans[deadline] = -reward_inv

print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
A = []
for i in range(N):
    a, b = map(int, input().split())
    A.append([a, b])
A = sorted(A, key = lambda x : x[0])
# print(A)
ans = 0

for i in range(M - 1, -1, -1):
    maximum_rwd = 0
    maximum_id = 0
    for j in range(len(A)):
        if A[j][0] > M - i:
            break
        # A[j]報酬までの時間、
        elif maximum_rwd < A[j][1]:
            maximum_id = j
            maximum_rwd = A[j][1]
    if maximum_rwd > 0:
        ans += maximum_rwd
        A.pop(maximum_id)
print(ans)

Submission status: Time Limit Exceeded

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

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

import numpy as np
x = np.array(a)
y = np.array(b)
order = np.argsort(-y)
x_sorted = x[order]
y_sorted = y[order]

used = [0 for _ in range(n)]

tmp = 0
res = 0
for day in range(m, 0, -1):
    thres = m - day + 1
#     print(day, thres)
    for i in range(n):
        if used[i]:
            continue
        if x_sorted[i] <= thres:
#             print("OK")
            used[i] = 1
            res += y_sorted[i]
            break
print(res)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

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

AB.sort(reverse=True, key=itemgetter(1))

res = [0 for _ in range(M)]

for i, (a, b) in enumerate(AB):
    p = M - a
    while p >= 0 and res[p] != 0:
        p -= 1
    if p >= 0:
        res[p] = b

print(sum(res))

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=sorted(c[i],reverse=True)
    if l:
      if max(j)>max(l):
        k+=max(j)
        if len(j)>1:
          l=l+j[1:]
      else:
        k+=max(l)
        del l[l.index(max(l))]
        l=l+j
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=max(l)
      del l[l.index(max(l))]
  if len(l)>m:
    l=l[:m]
print(k)

Submission status: Time Limit Exceeded

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

workList = list()
for _ in xrange(N):
    workList.append(map(int, raw_input().split()))

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

wage = 0
availableDay = set(xrange(1, M+1))
while workList:
    a, b = workList.pop(0)
    tmpList = [x for x in availableDay if x >= a]
    if tmpList:
        availableDay.remove(min(tmpList))
        wage += b

print wage

Submission status: Time Limit Exceeded

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

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

ab_list = sorted(ab_list, key=lambda x: -x[1])

c = 0
for i in range(1, m+1):
    for j, (a, b) in enumerate(ab_list):
        if a <= i:
            c += b
            ab_list.pop(j)
            break

print(c)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort()
dp = [0] * (M+1)
for i in range(1, M+1):
    tmp = 0
    idx = -1
    for idxab, ab in enumerate(AB):
        if ab[0] <= i:
            if tmp < ab[1]:
                idx = idxab
                tmp = ab[1]
        else:
            break
    if idx >= 0:
        AB[idx][1] = 0
    dp[i] = dp[i-1] + tmp

print(dp[len(dp)-1])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

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

s = [i for i in range(1, M + 1)]
ret = 0
for a, b in AB :
    if not s :
        break
    a = bisect_left(s, a)
    if a >= len(s) :
        continue
    
    s.pop(a)
    ret += b
print(ret)

Submission status: Time Limit Exceeded

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

a_b_list = []


for i in range(N):
    a, b = list(map(int, input().split()))
    a_b_list.append((a, b))


sort_a_b_list = sorted(a_b_list, key = lambda x: (x[0], x[1]), reverse=True)

total_value = 0
memo_list = []
memo_index = len(sort_a_b_list) + 1000
for day in range(M, 0, -1):
    max_value = 0
    for index in range(len(sort_a_b_list)):
        if index in memo_list:
            continue
        a_b = sort_a_b_list[index]
        if a_b[0] <= day:
            if max_value < a_b[1]:
                max_value = a_b[1]
                memo_index = index
    total_value += max_value
    memo_list.append(memo_index)

print(total_value)

Submission status: Time Limit Exceeded

Solution code:
N,M=(int(x) for x in input().split())
L=[]
D=[0]*M

for i in range(N):
    L.append(tuple(int(x) for x in input().split()))

L = sorted(L, key=lambda x:-x[1])
pay=0
for ab in L:
    if ab[0]<=M:
        for i in range(M-ab[0]+1):
            if D[M-ab[0]-i]==0:
                D[M-ab[0]-i]=1
                pay+=ab[1]
                break

print(pay)

Submission status: Time Limit Exceeded

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

ab.sort(key=lambda x: -x[1])

rew = 0
for i in range(1, m+1):
    for j in range(len(ab)):
        a, b = ab[j]
        if a <= i:
            rew += b
            ab.pop(j)
            break

print(rew)

Submission status: Time Limit Exceeded

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

ans=0
for d in range(m-1, -1, -1):
    nokori=m-d
    best=0
    idx=n+2
    for i in range(len(day_pay)):
        day, pay=day_pay[i]
        if day>nokori:
            break
        else:
            if pay>=best:
                best=pay
                idx=i
            else:
                continue
    if idx<=len(day_pay):
        del day_pay[idx]
    ans+=best

print(ans)

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 not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=[0]*m
for i in range(1,m+1):
    if i in l:
        target.extend(l[i])
    target.sort()
    val=target.pop()
    res+=val
    #print(target, max(target))

print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n,m = (int(num) for num in input().split())
ab = [[0,0] for _ in range(n)]
for i in range(n):
    a,b = (int(num) for num in input().split())
    ab[i][0] = a
    ab[i][1] = b
#print(ab)
ab.sort(key = lambda x: x[1], reverse=True)


days = [i+1 for i in range(m)]
leng = m
ans = 0
for item in ab:
    a = item[0]
    b = item[1]
    idx = bisect.bisect_left(days,a)
    if idx < leng:
        days.pop(idx)
        leng -= 1
        ans += b
print(ans)

Submission status: Time Limit Exceeded

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


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):

    jobs_time = collections.defaultdict(list)
    for a,b in zip(A,B):
        jobs_time[b].append(a)

    jobs_time = sorted(jobs_time.items(),reverse=True)
    job_list = [0] * M

    for k,v in jobs_time: 
        v.sort()
        for vv in v:
            if vv <= M:
                for i in range(vv-1,len(job_list)):
                    if job_list[i] < k:
                        job_list[i] = k
                        break

    print(sum(job_list))

    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,input().split())
C = []
for _ in range(N):
    A,B = map(int,input().split())
    C.append([A,B])
C = sorted(C,key=lambda x:x[0],reverse=True)
C = sorted(C,key=lambda x:x[1],reverse=True)
rev = 0
hist = [0 for _ in range(M+1)]
for x in C:
    A,B = x[0],x[1]
    for i in range(M-A,-1,-1):
        if hist[i]==0:
            rev += B
            hist[i]=1
            break
print(rev)

Submission status: Time Limit Exceeded

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

A = [None] * N
B = [None] * N
C = [True] * N

for n in range(N):
    a, b = map(int, input().split())
    A[n] = a
    B[n] = b

zipped  = zip(A, B)
zipped  = sorted(zipped, key=lambda x: [x[1], x[0]], reverse=True)
A, B = map(list, zip(*zipped))

R = 0

for k in range(M+1)[::-1]:
    for i, a in enumerate(A):
        if a <= k and C[i]:
            R    += B[i]
            C[i]  = False
            break

print(R)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N,M = map(int,input().split())
AB = list(list(map(int,input().split()))for i in range(N))
AB.sort()
can = []
p = 0
ans = 0
for i in range(M+1):
    while p<N and (AB[p][0] <= i) :
        index = bisect.bisect_left(can,AB[p][1])
        can.insert(index,AB[p][1])
        p += 1
        
    if can != []:
        ans += can.pop(-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
l = [[int(a) for a in input().split()] for i in range(N)]
l = sorted(l, key=lambda x: (-1*x[1], -1*x[0]))
ans = 0
j = 0
M += 1
for i in range(1, M+1):
    for k in range(j, N):
        a, b = l[j]
        if a + i <= M:
            ans += b
            j += 1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(s) for s in input().split()]

Bs = [[] for _ in range(M)]

for _ in range(N):
    A, B = [int(s) for s in input().split()]
    if A <= M:
        Bs[A - 1].append(B)


t = 0
J = []
for m in range(M):
    J = list(reversed(sorted(J + Bs[m])))

    if len(J) != 0:
        t += J[0]
        J = J[1:]

print(t)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(sorted(AB, key=lambda x: x[0], reverse=True), key=lambda x: x[1], reverse=True)
x = [0] * M
cnt = 0
cntIN = M
for a, b in AB:
    i = M-a
    while i>=0 and x[i] != 0:
        i -= 1
    else:
        if i >= 0:
            x[i] = b
            cntIN -= 1
            if cntIN <= 0:
                break

print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N ,M = ints_raw()
    days = [0]*M
    ABs = [ints_raw() for _ in range(N)]
    ABs.sort(key=lambda x:x[1],reverse=True)
    ans = 0
    for AB in ABs:
        A,B = AB
        if A > M:
            continue
        else:
            C = M-A
            while days[C]==1 and C>=0:
                C-=1
            if C>=0:
                days[C]=1
                ans+=B
    return ans

    
print(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 not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=[0]
for i in range(1,m+1):
    if i in l:
        target.extend(l[i])
    val=max(target)
    if val != 0:
        target.remove(val)
    res+=val
    #print(target, max(target))

print(res)

Submission status: Time Limit Exceeded

Solution code:
n,m=[int(i) for i in input().split()]
c={}
for _ in range(n):
  a,b=[int(i) for i in input().split()]
  if a in c:
    c[a].append(b)
  else:
    c[a]=[b]
k=0
l=[]
for i in range(m+1):
  if i in c:
    j=sorted(c[i],reverse=True)
    if l:
      if j[0]>l[0]:
        k+=j[0]
        if len(j)>1:
          l=sorted(l+j[1:],reverse=True)
      else:
        k+=l[0]
        l=sorted(l[1:]+j,reverse=True)
    else:
      k+=j[0]
      l=j[1:]
  else:
    if l:
      k+=l[0]
      l=l[1:]
  if len(l)>m:
    l=l[:m]
print(k)

Submission status: Time Limit Exceeded

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

AB.sort(reverse = True, key = lambda x:x[1])
ans = 0
for i in range(1, M + 1):
  for j in range(len(AB)):
    if AB[j][0] <= i:
      ans += AB.pop(j)[1]
      break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
ans = 0

for i in range(M - 1, -1, -1):
    maximum_rwd = 0
    maximum_id = 0
    for j in range(len(A)):
        # A[j]報酬までの時間、
        if A[j] <= M - i and maximum_rwd < B[j]:
            maximum_id = j
            maximum_rwd = B[j]
    if maximum_rwd > 0:
        ans += maximum_rwd
        A.pop(maximum_id)
        B.pop(maximum_id)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
n, m = map(int, input().split())
ba = [None] * n
for i in range(n):
    a, b = map(int, input().split())
    ba[i] = [b, a]
ba.sort(reverse=True)
li = [0] * m
for i in range(n):
    b, a = ba[i]
    for j in range(m - a + 1)[::-1]:
        if li[j] == 0:
            li[j] = b
            break
print(sum(li))