Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input
#import numpy as np

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    new_elms = sorted(values[i], reverse=True)[:M-i+1]
    elms += new_elms
    if len(elms) >= 1:
        max_val = max(elms)
        out += max_val
        elms.remove(max_val)

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
#input = raw_input
#import numpy as np

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    new_elms = sorted(values[i], reverse=True)[:M-i+1]
    elms += new_elms
    if len(elms) >= 1:
        max_val = max(elms)
        out += max_val
        elms.remove(max_val)

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
#input = raw_input
#import numpy as np

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    elms += values[i]
    if len(elms) >= 1:
        max_val = max(elms)
        out += max_val
        elms.remove(max_val)

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Runtime Error):
#input = raw_input
import numpy as np

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

pairs = []
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        pairs.append([A, B])

pairs = sorted(pairs, key=lambda elm: -elm[1])
#print(pairs)

out = 0

#not_done = [True for _ in range(M)]

#counts = [0 for _ in range(M)]
cumsum_counts = np.zeros(M, dtype=int)

all_sum = 0

ue_end_i = M-1
for A, B in pairs:
    ue_start_i = M-A+1
    if ue_start_i <= ue_end_i:
        ue_limit = ue_end_i - ue_start_i + 1
        #ue_sum = sum(counts[ue_start_i:])
        ue_sum = cumsum_counts[ue_start_i]
        hamidashi = max(ue_sum - ue_limit, 0)
    else:
        # ue_start_i == M
        hamidashi = 0
    #print("hamidashi", hamidashi)

    cur_limit = M- A + 1 - hamidashi # 0 ~ M-A
    sudeni = all_sum - ue_sum
    aki = cur_limit - sudeni

    if aki >= 1:
        out += B
        #counts[M-A] += 1
        cumsum_counts[M-A:] += 1
        all_sum += 1

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Runtime Error):
#input = raw_input
import numpy as np

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

pairs = []
for i in range(N):
    pairs.append(list(map(int, input().split())))

pairs = sorted(pairs, key=lambda elm: -elm[1])
#print(pairs)

finish_day = -1
out = 0

#not_done = [True for _ in range(M)]

counts = [0 for _ in range(M)]
cumsum_counts = np.zeros(0, dtype=int)

all_sum = 0

ue_end_i = M-1
for A, B in pairs:
    if A > M:
        continue
    ue_start_i = M-A+1
    if ue_start_i <= ue_end_i:
        ue_limit = ue_end_i - ue_start_i + 1
        #ue_sum = sum(counts[ue_start_i:])
        ue_sum = cumsum(ue_start_i)
        hamidashi = max(ue_sum - ue_limit, 0)
    else:
        # ue_start_i == M
        ue_sum = 0
        hamidashi = 0
    #print("hamidashi", hamidashi)

    cur_limit = M- A + 1 - hamidashi # 0 ~ M-A
    sudeni = all_sum - ue_sum
    aki = cur_limit - sudeni

    if aki >= 1:
        out += B
        #counts[M-A] += 1
        cumsum_counts[M-A:] += 1
        all_sum += 1

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
input = raw_input

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

pairs = []
for i in range(N):
    pairs.append(list(map(int, input().split())))

pairs = sorted(pairs, key=lambda elm: -elm[1])
#print(pairs)

finish_day = -1
out = 0

not_done = [True for _ in range(M)]

for A, B in pairs:
    for i in range(M-A, -1, -1):
        if not_done[i]:
            out += B
            #print(A, B)
            not_done[i] = False
            break

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

pairs = []
for i in range(N):
    pairs.append(list(map(int, input().split())))

pairs = sorted(pairs, key=lambda elm: -elm[1])
#print(pairs)

finish_day = -1
out = 0

not_done = [True for _ in range(M)]

for A, B in pairs:
    for i in range(M-A, -1, -1):
        if not_done[i]:
            out += B
            #print(A, B)
            not_done[i] = False
            break

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

pairs = []
for i in range(N):
    pairs.append(list(map(int, input().split())))

pairs = sorted(pairs, key=lambda elm: [-elm[1], -elm[0]] )
#print(pairs)

finish_day = -1
out = 0

not_done = [True for _ in range(N)]

for A, B in pairs:
    for i in range(M-A, -1, -1):
        if not_done[i]:
            out += B
            #print(A, B)
            not_done[i] = False
            break

print(out)

Fixed submission:
#input = raw_input
#import numpy as np

from heapq import heappush, heappop

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

values = [[] for i in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        values[A].append(-B)

#print(values)
out = 0
elms = []
for i in range(1, M+1):
    #print("values[i]", values[i])
    for elm in values[i]:
        heappush(elms, elm)
        
    if len(elms) >= 1:
        max_val = - heappop(elms)
        #print("mval", max_val)
        out += max_val

print(out)