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 = map(int, input().split())

rewards = [None] * N
for i in range(N):
    a, b = map(int, input().split())
    rewards[i] = (b, a)

heap = []
total = 0
for day in range(1, M + 1):
    target_rewards = list(filter(lambda x: (day - 1) < x[1] <= day, rewards))

    for reward in target_rewards:
        heapq.heappush(heap, (-reward[0], reward[1]))

    if len(heap) > 0:
        max_reward = heapq.heappop(heap)
        total += -max_reward[0]

print(total)

Submission status: Time Limit Exceeded

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

day_to_cost = {}
for a, b in AB:
    if day_to_cost.get(a) is None:
        day_to_cost[a] = [b]
    else:
        day_to_cost[a].append(b)

ans = 0
candidate = []
for i in range(1, M+1):
    candidate += day_to_cost.get(i, [])
    if candidate:
        work_cost = max(candidate)
        candidate.remove(work_cost)
        ans += work_cost


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
baito = []
for i in range(n):
    baito.append(list(map(int,input().split())))
ans = 0
for j in range(m):
    tmp = 0
    now = ''
    for w in range(n):
            if baito[w][0] <= j+1:
                if tmp < baito[w][1]:
                    tmp = baito[w][1]
                    now = w
    ans += tmp
    if now != '':
        baito[now] = [1000000,1000000]
print(ans)

Submission status: Time Limit Exceeded

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

ans=0
temp=[]
for i in range(1,m+1):
    while absort:
        if absort[0][0]<=i:
            temp.append(absort.pop(0))
        else:
            break
    temp=sorted(temp,key=lambda x:x[1], reverse=True)
    if temp:
        ans+=temp[0][1]
        temp.pop(0)

print(ans)

Submission status: Time Limit Exceeded

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

lists = [[] for _ in range(10**5 + 1)]
availables = []
for A, B in ABs:
    bisect.insort(lists[A], B)

ans = 0
for i in range(1, M + 1):
    availables = list(heapq.merge(availables, lists[i]))
    if availables:
        ans += availables.pop()

print(ans)

Submission status: Time Limit Exceeded

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

import numpy as np
a = np.array(a)
b = np.array(b)
a, b = list(zip(*np.c_[a,b][np.argsort(a)[::-1]]))
a = np.array(a)
b = np.array(b)

r = 0
for i in range(1, m+1):
  b_tmp = b.copy()
  b_tmp[a > i] = 0
  j_max = np.argmax(b_tmp)
  r += b_tmp[j_max]
  b[j_max] = 0

print(r)

Submission status: Time Limit Exceeded

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

import numpy as np
a = np.array(a)
b = np.array(b)
a, b = list(zip(*np.c_[a,b][np.argsort(a)[::-1]]))
a = np.array(a)
b = np.array(b)

r = 0
for i in range(1, m+1):
  b_tmp = b.copy()
  b_tmp[a > i] = 0
  j_max = np.argmax(b_tmp)
  r += np.max(b_tmp)
  b[j_max] = 0

print(r)

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)
def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('- {} -> {}'.format(name, val), file=sys.stderr)
            return None
N, M =LI()
AB = []
for i in range(N):
    AB.append(LI())
AB.sort(key=lambda x: x[0])
perr(AB)
#  AB_dict = {}
#  for ab in AB:
#      if ab[0] not in AB_dict.keys():
#          AB_dict[ab[0]] = [ab[1]]
#      else:
#          AB_dict[ab[0]].append(ab[1])
#  for k in AB_dict.keys():
#      AB_dict[k].sort()
#  perr(AB_dict)

tmp = []
day = 1
ans = 0
crr = 0
while True:
    if day == M + 1:
        break

    if crr < N:
        if AB[crr][0] > day:
            perr('nextday')
            perr(tmp)
            day += 1
            if len(tmp) == 0:
                continue
            tmp.sort()
            ans += tmp.pop()
        else:
            perr('add')
            tmp.append(AB[crr][1])
            crr += 1
    else:
        if len(tmp) == 0:
            break
        tmp.sort()
        for time in range(M-day+1):
            ans += tmp.pop()
            day += 1
        
print(ans)
#  ans = 0
#  tmp = []
#  crr = 0
#  for i in range(1, M+1):
#      if crr < N:
#          if AB[crr][0] == i:
#              while True:
#                  if crr == N:
#                      break
#                  if AB[crr][0] != i:
#                      break
#                  tmp.append(AB[crr][1])
#                  crr += 1

#      perr('tmp')
#      perr(tmp)
#      if len(tmp) == 0:
#          continue

#      max_v = max(tmp)
#      ans += max_v
#      tmp.remove(max_v)
#      #  tmp.sort()
#      #  ans+=tmp.pop()

#  """
#  貪欲か？
#  日が長くて報酬の高いものから選んでいく
#  そうすると
#  10 1
#  9 10
#  みたいなときに10 1のほうから選んでしまうからだめ
#  残り日数を意識
#  残ってる中で最も報酬の高いものから？
#  2 10
#  1 11
#  のパターンで、先に1をやってしまうためだめ
#  2 10
#  1 11
#  1 15
#  のパターンで、1 1をやるのが正解
#  """

#  #  ans = 0
#  #  tmp = []
#  #  for i in range(1, M+1):
#  #      # 残りi日
#  #      if i in AB_dict.keys():
#  #          tmp += AB_dict[i]
#  #      perr('tmp')
#  #      perr(tmp)
#  #      if len(tmp) == 0:
#  #          continue
#  #      tmp.sort()
#  #      #  max_v = max(tmp)
#  #      #  ans += max_v
#  #      ans += tmp.pop()
#  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n,m = map(int, raw_input().split())
lst = [tuple(map(int, raw_input().split())) for _ in xrange(n)]

def fn(d, r, path):
    #print d*' ','day=',d,'max rem=',r,'path=',path
    if d == m + 1:
        # m日後に到達
        #print d*' ','end!'
        return r, path
    # 選べるものの中から選択
    mr = r
    mp = path
    for i in xrange(n):
        if lst[i] in path:
            continue
        a,b = lst[i]
        if a > m - d + 1:
            continue
        #print d*' ','sel',a,b
        rr,rp = fn(d + 1, r + b, path + ((a,b),))
        #print d*' rem=',rr
        if rr > mr:
            mr = rr
            mp = rp
            #print d*' ','update', mr, mp

    return mr, mp

x,y = fn(1, 0, ())
print x
#print y

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
numList=[[0]*2 for i in range(N)]


for i in range(N):
    A,B= map(int, input().split())
    numList[i][0]=A
    numList[i][1]=B

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

sum=0
count=0
k=0
t=-1
while True:
    if(k>=M):break
    k+=1
    for i in range(N):
        if(numList[i][0]<=k):
            if(count<numList[i][1]):
                count=numList[i][1]
                t=i
        else:
            sum+=count
            count=0
            if(t!=-1):
                numList[t][1]=0
                t=-1
            break
    else:
        sum+=count
        count=0
        if(t!=-1):
            numList[t][1]=0
            t=-1
print(sum)

Submission status: Time Limit Exceeded

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

import numpy as np
a = np.array(a)
b = np.array(b)

r = 0
for i in range(1, m+1):
  a, b = list(zip(*np.c_[a,b][np.argsort(a)[::-1]]))
  a = np.array(a)
  b = np.array(b)
  b_tmp = b.copy()
  b_tmp[a > i] = 0
  j_max = np.argmax(b_tmp)
  r += np.max(b_tmp)
  b[j_max] = 0

print(r)

Submission status: Time Limit Exceeded

Solution code:
def calc(N, M, AB):
    day2pay = {
        a: sorted(
            [ab[1] for ab in AB if ab[0] == a],
            key=lambda b: -b)
        for a in range(1, M + 1)
    }
    for a in range(1, M + 1):
        if (not (day2pay[a])):
            day2pay.pop(a)
    result = 0
    d2p_keys = sorted(list(day2pay.keys()))
    d2p_keys.reverse()
    for m in range(M):
        if (not (day2pay)):
            break
        i = d2p_keys[0]
        if (len(d2p_keys) > 1):
            j = d2p_keys[1 if i == M - m else 0]
            if (len(day2pay[j]) > 1 and day2pay[j][1] > day2pay[i][0]):
                result += day2pay[j].pop(0)
            else:
                result += day2pay[i].pop(0)
        else:
            result += day2pay[i].pop(0)
        if (i == M - m or not (day2pay[i])):
            day2pay.pop(i)
            d2p_keys.remove(i)
    return result


(N, M) = tuple([int(s) for s in input().split(' ')])
AB = [tuple([int(s) for s in input().split(' ')]) for _ in range(N)]
print(calc(N, M, AB))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
B = [[] for _ in range(m+1)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m: B[a] += [b]
C = []
s = 0
for i in range(m+1):
    C += B[i]
    C = sorted(C, reverse = True)[:m-i+1]
    if C: s += C.pop(0)
print(s)

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
from collections import deque

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

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

sorted_ab = deque(sorted(ab.items(), key=lambda x: x[0]))
# for e in sorted_ab:
#   print(e)

count = 0
for i in range(1, 1+m):
  max_v = 0
  for j in range(len(sorted_ab)):
    e = sorted_ab[j]
    if e[0] > i:
      break
    if max_v < e[1][0]:
      max_v = e[1][0]
      idx = j
  if max_v > 0:
    sorted_ab[idx][1].popleft()
    if len(sorted_ab[idx][1]) == 0:
      del sorted_ab[idx]
  # print((sorted_ab, max_v))
  count += max_v

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

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

# print(lst)

ans = 0
ind = 0
kouho = []
for i in range(1, m+1):
    tugi = True
    while tugi:
        if ind >= n:
            break
        if lst[ind][0] == i:
            bisect.insort(kouho, lst[ind][1])
            ind += 1
        else:
            tugi = False
    if kouho != []:
        ans += kouho.pop()

print(ans)

Submission status: Time Limit Exceeded

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

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

jobs = sorted(baito, key=lambda x: x[1], reverse=True)

kane = 0
schedule = [0]*(m+1)

for job in jobs:
    day = job[0]
    while(day <= m and schedule[day] != 0):
        day += 1
    if day <= m and schedule[day] == 0:
        schedule[day] = job[1]

print(sum(schedule))

Submission status: Time Limit Exceeded

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

for n in range(N):
    AB[n][0], AB[n][1] = map(int, input().split())
    AB[n][1] = 0 - AB[n][1]
AB.sort()

earn = 0
for m in range(1, M+1):
    max_earn = 0
    max_i = 0
    for i in range(len(AB)):
        if m < AB[i][0] :
            break
        
        if max_earn > AB[i][1]:
            max_earn = AB[i][1]
            max_i = i


    if max_earn != 0:
        earn += AB[max_i][1]
        AB.pop(max_i)

        

        
print(0-earn)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int, input().split()) 
ab=[list(map(int, input().split())) for i in range(n)]
ans=0
 
x=sorted(ab, key=lambda x :x[1])
x.reverse()
 
v=[]
for i in range(1,m+1):
    v.append(i)
 
for i in range(min(m,n)):
    if len(v)==0:
        break
    if x[i][0]<=v[-1]:
        ans+=x[i][1]
        for j in range(m):
            if v[j]>=x[i][0]:
                del v[j]
                break
    else:
        continue
print(ans)

Submission status: Time Limit Exceeded

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

lists = [[] for _ in range(10**5 + 1)]
availables = []
for A, B in ABs:
    bisect.insort(lists[A], B)

ans = 0
for i in range(1, M + 1):
    availables = sorted(chain(availables, lists[i]))
    if availables:
        ans += availables.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10**9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def LIST(): return [int(x) for x in input().split()]
MOD = 10**9 + 7

N, M = LIST()

A = [[] for _ in range(10**4+1)]
for _ in range(N):
    a, b = LIST()
    if a <= M:
        A[b].append(a)

for a in A:
    a.sort(reverse=True)

off = [False] * M
ans = 0

i = 10**4
while i > 0:
    for a in A[i]:
        j = M - a
        while j >= 0:
            if off[j] == False:
                off[j] = True
                ans += i
                break;
            else:
                j -= 1

    if off == [True] * M:
        break;
    else:
        i -= 1

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)]

import heapq
import collections

a_all = []
for i in range(n):
    a_all.append(ab[i][0])

key_all = list(set(a_all))
key_all.sort()

d = dict()
for k in key_all:
    d[k] = []

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



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 sys
from bisect import insort

n, m = map(int, sys.stdin.readline().split())
d = [[] for _ in range(m+1)]
for i in range(n):
    a, b = map(int, sys.stdin.readline().split())
    if a <= m:
        d[a].append(b)

res = 0
resList = []
for i in range(1, m+1):
    if len(d[i]) > 0:
        for j in range(len(d[i])):
            insort(resList, d[i][j])
    if len(resList) > 0:
        res += resList.pop()

print(res)

Submission status: Time Limit Exceeded

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------

N, M = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

A=[]
B=[]
for i in range(N):
	a, b = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7
	A.append([a,b])

B = sorted(A, key=lambda x:(-x[1],-x[0]))

X=[0]*(M+1)

p("A ",A)
p("B ",B)
p("X ",X)


l = 1
r = M+1
for L in B:
	d = L[0]
	m = L[1]
	
	z=r
	for i in range(l+d-1, r):
		if X[i]==0:
			l = min(l, i)
			X[i]=m
			z=r
			break
		
		z=min(i,z)

	r=min(z,r)
			

p("X ",X)

print( sum(X) )

Submission status: Time Limit Exceeded

Solution code:
from functools import cmp_to_key
def cmp(x, y):
  if x[0] != y[0]:
    return y[0] - x[0]
  else:
    return y[1] - x[1]
n, m = map(int, input().split())
data = [[int(e) for e in input().split()] for _ in range(n)]
data.sort(key = cmp_to_key(cmp))
t = [0] * (m + 1)
for i in range(n):
  a, b = data[i]
  for j in range(m, -1, -1):
    if j + a <= m:
      t[j + 1] = max(t[j + 1], t[j] + b)
print(max(t))

Submission status: Time Limit Exceeded

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

import heapq
import collections

a_all = []
for i in range(n):
    a_all.append(ab[i][0])

key_all = list(set(a_all))
key_all.sort()

d = dict()
for k in key_all:
    d[k] = []

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



ans = 0
l = []
import bisect

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

    # print(l)

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

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
B = [[] for _ in range(m+1)]
for _ in range(n):
    a, b = map(int, input().split())
    if a <= m: B[a] += [b]
C = []
s = 0
for i in range(m+1):
    C += B[i]
    C = sorted(C, reverse = True)
    if C: s += C.pop(0)
print(s)

Submission status: Time Limit Exceeded

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

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

val = 0
rest = m
tmp  = -1
tmpa = -1

for i in range(n):
    if rest ==0: break
    max = 0
    for i in range(n):
        if (b[i] > max) or (b[i] == max and a[i] > tmpa):
            max = b[i]; tmp = i; tmpa = a[i]
    if a[tmp] <= rest: val += max ; rest -= 1
    b[tmp] = 0

print(val)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from bisect import insort

n, m = map(int, input().split())
d = [[] for _ in range(m+1)]
for i in range(n):
    a, b = map(int, sys.stdin.readline().split())
    if a <= m:
        d[a].append(b)

res = 0
resList = []
for i in range(1, m+1):
    sub = d[i]
    if len(sub) > 0:
        for j in range(len(sub)):
            insort(resList, sub[j])
    if len(resList) > 0:
        res += resList.pop()

print(res)

Submission status: Time Limit Exceeded

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


class DailyJob:
    def __init__(self, day_, earn):
        self.day = day_
        self.earn = earn


jobs = []
for line in range(N):
    day__, earn_ = map(int, input().split())
    jobs.append(DailyJob(day__, earn_))

maxEarn = 0
for day in range(M):
    jobsCanEarn = list(filter(lambda x: x.day <= M - day, jobs))
    if len(jobsCanEarn) > 0:
        jobToDo = max(jobsCanEarn, key=lambda x: x.day + x.earn)
        maxEarn += jobToDo.earn
        jobs.remove(jobToDo)

print(maxEarn)

Submission status: Time Limit Exceeded

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

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

ans = 0
#print(AB)
for i in range(1, M + 1) :
    maxIncome = 0
    anker = -1
    for j in range(len(AB)) :
        #print(AB[j][0])
        if AB[j][0] <= i :
            if AB[j][1] > maxIncome :
                maxIncome = AB[j][1]
                anker = j
                #print('A', j, maxIncome)
                continue
        else :
            break
    ans += maxIncome
    if len(AB) == 0 :
        break
    if anker != -1:
        AB.pop(anker)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N, M = map(int, input().split())
job_list = {}
kouho = deque()
ans = 0
for _ in range(N):
  A, B = map(int, input().split())
  if A in job_list.keys():
    job_list[A].append(B)
  else:
    job_list[A] = deque([B])
    
for day in range(1, M + 1):
  try: 
    kouho += job_list[day]

  except KeyError:
    pass
  if len(kouho) != 0:
    temp = max(kouho)
    ans += temp
    kouho.remove(temp)

print(ans)

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:
import bisect

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

task.sort()

wa = []
ans = 0
j = 0
for i in range(M):
    while j < N and task[j][0] == i + 1:
        bisect.insort_right(wa, task[j][1])
        j += 1
    if wa:
        ans += wa[-1]
        del (wa[-1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from bisect import insort

n, m = map(int, input().split())
d = [[] for _ in range(m+1)]
for i in range(n):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    if a <= m:
        d[a].append(b)

res = 0
resList = []
for i in range(1, m+1):
    sub = d[i]
    if len(sub) > 0:
        for j in range(len(sub)):
            insort(resList, sub[j])
    if len(resList) > 0:
        res += resList.pop()

print(res)

Submission status: Time Limit Exceeded

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

today = 0

after = []
money = []

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

sum_money = 0
for day in range(m, -1, -1):
    # print('day',day)
    # print('after', after)
    maxi = [-1, 0]
    for i, x in enumerate(after):
        if x <= (m - day):
            if maxi[1] < money[i]:
                maxi = [i, money[i]]
    if maxi[0] == -1:
        continue
    # print('maxi', maxi)
    del after[maxi[0]]
    del money[maxi[0]]
    sum_money += maxi[1]

print(sum_money)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
ab=[]
q=0
for i in range(n):
    p=[int(j) for j in input().split()]
    q=max(q,p[0])
    ab.append(p)
from operator import itemgetter
 
ab.sort(key=itemgetter(1))
ab.reverse()
ab.sort(key=itemgetter(0))
l=[]
import bisect
ans=0
for i in range(1,m+1):
    while True:
        if ab==[]:
            break
        if ab[0][0]<=i:
            insert_index = bisect.bisect_left(l,ab[0][1])  #2
            l.insert(insert_index,ab[0][1])
            del ab[0]
        else:
            break
    if l!=[]:
        ans+=l[-1]
        del l[-1]
        

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = list(map(int,input().split()))
x=[]
for u in range(n):
    x.append(list(map(int,input().split())))
x.sort(key = lambda x:x[1], reverse = True)
q = [-1]*(m)
ans = 0

for i in x:
    if(i[0]>m):
        continue
    else:
        y = m-i[0]

        for o in range(y,-1,-1):
            if(q[o]==-1):
                q[o]=0
                ans+=i[1]
                break

print(ans)

Submission status: Time Limit Exceeded

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

an = []

for _ in range(n):
    a, b = map(int, input().split())
    an.append((a,b))
#an.sort( key = lambda v:v[0]*-1)
#an.sort( key = lambda v:v[1]*-1)
an.sort( key = lambda v: (v[1]*-1, v[0]*-1))
#print('--')
#print(an)
d = 1
ans = 0
while d <= m:
    #print("D is {}".format(d))
    for i, job in enumerate(an):
        if job[0] <= d:
            #print("d is {}".format(d))
            ans += job[1]
            #print("do {} {}".format(job[0], job[1]))
            del an[i]
            #print(an)
            break
    d += 1

print(ans)

Submission status: Time Limit Exceeded

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

x=sorted(ab, key=lambda x :x[1])
x.reverse()

v=[]
for i in range(1,m+1):
    v.append(i)

for i in range(n):
    if len(v)==0:
        break
    for j in range(m):
        if v[-1]>=x[i][0]:
            if v[j]>=x[i][0]:
                ans+=x[i][1]
                del v[j]
                break
        else:
            pass
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
c = []
ans=0
for i in range(n):
    a= list(map(int, input().split()))
    c += [a]
c.sort(key=lambda x:x[0],reverse=True)
c.sort(key=lambda x: x[1], reverse=True)
for i in reversed(range(m+1)):
    for u in range(n):
        if c[u][0] <= i:
            ans += c[u][1]
            c[u][0] = 1000000
            c[u][1]=1000000
            break
print(ans)

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], reverse=True)
#print(ab_list)
val = 0
for rest in range( m, 0, -1):# 残り日数
  	#print("rest" + str(rest))
    index_dic = {}
    count_dic = {}
    for i, ab in enumerate(ab_list):        
        a = ab[0]
        b = ab[1]
        #print("ab", a,b)
        if a > rest:
            continue
        if a in index_dic:
            count_dic[a] += 1
        else:
            index_dic[a] = i
            count_dic[a] = 1
        if count_dic[a] + a > rest:
            index = index_dic[a]
            #print(index, ab_list[index][1])
            val += ab_list[index][1]
            del ab_list[index]
            break
print(val)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
works = [list(map(int, input().split())) for _ in range(N)]
works.sort(key=lambda x:(-x[1], -x[0]))
days = [0] * (M+1)
ans = 0
max_day = M+1
for work in works:
  if work[0] >= max_day:
    continue
  full = True
  for day in range(work[0], max_day):
    if days[day] == 0:
      days[day] = 1
      ans += work[1]
      full = False
      break
  if full:
    max_day = work[0]
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
  ab = list(map(int, input().split()))
  if ab[0] <= M:
    AB.append(ab)

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:
# coding: utf-8
# Your code here!

n, m = map(int,input().split())
joblist = []
for i in range(n):
    a,b = map(int,input().split())
    joblist.append([a,b])
joblist.sort(key=lambda x :x[0])
kouho = []

ans =0

for i in range(1,m+1):
    count=0
    if joblist:
        for j,job in enumerate(joblist):
            if job[0]==i:
                
                kouho.append(job[1])
                
                
            if job[0]!=i:
                if count:
                    for _ in range(count-1):
                        joblist.pop(0)
        if kouho:
            kouho.sort(reverse=True)
            ans += kouho.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

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

N,M = II()
A,B = Line(N)
a = []
for i in range(N):
  a.append([A[i],B[i]])
a.sort(key = lambda x:(-x[1]))

from bisect import bisect_left
s = [i for i in range(1,M+1)]
ans = 0
for i in range(N):
  if a[i][0]<=M:
    place = bisect_left(s,a[i][0])
    if place <= len(s)-1:
      s.pop(place)
      ans += a[i][1]
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 = [0 for _ in range(10**4+1)]
maxi = 0
cnt = 0
for i in range(m+1):
  while True:
    if here == n:
      break
    elif ab[here][0] == -i:
      add = ab[here][1]
      pool[add] += 1
      if add > maxi:
        maxi = add
      here += 1
      cnt += 1
    else:
      break
  if cnt != 0:
    cnt -= 1
    for j in range(10**4):
      '''
      if pool[10**4-j] != 0:
        ans += 10**4-j
        print(10**4-j,i)
        pool[10**4-j] -= 1
        break
      '''
    ans += maxi
    pool[maxi] -= 1
    if pool[maxi] == 0:
      if cnt == 0:
        maxi = 0
      else:
        while True:
          maxi -= 1
          if pool[maxi] != 0:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M= map(int, input().split())
B=[]
for i in range(N):
    B.append([None]*2)
    l=input().split()
    B[i][0]=int(l[1])
    B[i][1]=int(l[0])
B.sort(reverse=True)
h=[0]*(M+1)
a=0
d=M+1
for i in range(N):
    if d>B[i][1]:
      for j in range(B[i][1],d):
        if h[j]==0:
            h[j]=1
            a+=B[i][0]
            break
      else:
        d=B[i][1]
print(a)