Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = -1000000000
for i in range(n):
    now = i
    j = 0
    score = deque([0])
    while j < k:
        score.append(score[j] +c[p[now]-1])
        now = p[now]-1
        j += 1
        if now == i:
            break
    ans = max(ans, max(list(score)[1:]))
    if j < k and score[-1] > 0:
        loop_num = k//j
        rest = k%j
        ans = max(ans, max(list(score)[:rest+1])+loop_num*score[-1] )
        ans = max(ans, max(list(score))+(loop_num-1)*score[-1] )
print(ans)
        

Fixed submission:
from collections import deque

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = -1000000000
for i in range(n):
    now = i
    j = 0
    score = deque([0])
    while j < k:
        score.append(score[j] +c[p[now]-1])
        now = p[now]-1
        j += 1
        if now == i:
            break
    ans = max(ans, max(list(score)[1:]))
    if j < k and score[-1] > 0:
        loop_num = k//j
        rest = k%j
        ans = max(ans, max(list(score)[:rest+1])+loop_num*score[-1] )
        ans = max(ans, max(list(score))+(loop_num-1)*score[-1] )
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# import functools
# import operator
# prod = functools.partial(functools.reduce, operator.mul)

N, K = map(int, input().split())
A = list(map(int, input().split()))

score = [1 for _ in range(N)]
for i in range(K):
    for j in range(i+1):
        score[j] *= A[i]
# print(score)

# pre_score = prod(A[:K])
for i in range(K, N):
    for x in range(K):
        score[i-x] *= A[i]
    # score = prod(A[i-K+1:i+1])
    # print(score[i-K+1] , score[i-K])
    if score[i-K+1] > score[i-K]:
        print('Yes')
    else:
        print('No')
    # pre_score = score
# print(score)

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

for i in range(K, N):
    if A[i] > A[i-K]:
        print('Yes')
    else:
        print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import functools
import operator
prod = functools.partial(functools.reduce, operator.mul)

N, K = map(int, input().split())
A = list(map(int, input().split()))

# score = 1
# for i in range(K):
#     score *= A[i]
pre_score = prod(A[:K])
for i in range(K, N):
    score = prod(A[i-K+1:i+1])
    if score > pre_score:
        print('Yes')
    else:
        print('No')
    pre_score = score

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

for i in range(K, N):
    if A[i] > A[i-K]:
        print('Yes')
    else:
        print('No')

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

score = 1
for i in range(K):
    score *= A[i]
pre_score = score
for i in range(K, N):
    score = score / A[i-K] * A[i]
    if score > pre_score:
        print('Yes')
    else:
        print('No')
    pre_score = score

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

for i in range(K, N):
    if A[i] > A[i-K]:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
row = [A[0], A[1]]
min_list =[A[1], A[1]]
ans = A[0]
for i in range(2, N):
    index = np.argmax(min_list)
    ans += min_list[index]
    if index == len(min_list)-1:
        l, r = row[index], row[0]
    else:
        l, r = row[index], row[index+1]
    row.insert(index+1, A[i])
    min_list[index] = A[i]
    min_list.insert(index+1, A[i])
print(ans)



Fixed submission:
# import numpy as np
from collections import deque

N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
# row = [A[0], A[1]]
min_list = deque([A[1], A[1]])
ans = A[0]
for i in range(2, N):
    # index = np.argmax(min_list)
    ans += min_list.popleft()
    # if index == len(min_list)-1:
    #     l, r = row[index], row[0]
    # else:
    #     l, r = row[index], row[index+1]
    # row.insert(index+1, A[i])
    min_list.append(A[i])
    min_list.append(A[i])
    # min_list.insert(index+1, A[i])
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    mod = int(1e9+7)
    p = []
    m = []
    for a in A:
        if a >= 0:
            p.append(a)
        else:
            m.append(a)
    p.sort(reverse=True)
    m.sort(reverse=False)
    ans = 1
    if sum(np.sign(A)) == -N and K%2 == 1:
        A = sorted(A, reverse=True)
        for i in range(K):
            ans = (ans*A[i])%mod
        print(ans)
        return

    m_pair = []
    p_pair = []
    if K%2 == 1:
        ans *= p.pop(0)%mod
    for i in range(0, len(m), 2):
        if i+1 < len(m):
            m_pair.append(m[i]*m[i+1])
        # else:
        #     m_pair.append(m[i])
    for i in range(0, len(p), 2):
        if i+1 < len(p):
            p_pair.append(p[i]*p[i+1])
        # else:
        #     p_pair.append(p[i])
    pairs = m_pair[:]
    pairs.extend(p_pair)
    pairs.sort(reverse=True)
    for i in range(K//2):
        ans = (ans*pairs[i])%mod
    print(ans)

main()


Fixed submission:
import numpy as np

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    mod = int(1e9+7)
    p = []
    m = []
    for a in A:
        if a >= 0:
            p.append(a)
        else:
            m.append(a)
    p.sort(reverse=True)
    m.sort(reverse=False)
    ans = 1
    if sum(np.sign(A)) == -N and K%2 == 1:
        A = sorted(A, reverse=True)
        for i in range(K):
            ans = (ans*A[i])%mod
        print(ans)
        return
    if N == K:
        for i in range(K):
            ans = (ans*A[i])%mod
        print(ans)
        return

    m_pair = []
    p_pair = []
    if K%2 == 1:
        ans *= p.pop(0)%mod
    for i in range(0, len(m), 2):
        if i+1 < len(m):
            m_pair.append(m[i]*m[i+1])
        # else:
        #     m_pair.append(m[i])
    for i in range(0, len(p), 2):
        if i+1 < len(p):
            p_pair.append(p[i]*p[i+1])
        else:
            p_pair.append(p[i])
    pairs = m_pair[:]
    pairs.extend(p_pair)
    pairs.sort(reverse=True)
    for i in range(K//2):
        ans = (ans*pairs[i])%mod
    print(ans)

main()
Buggy python submission (verdict: Runtime Error):
import math
N = int(input())
if math.log(25*N+26, 26) == int(math.log(25*N+26, 26)):
    d = int(math.log(25*N+26, 26)) -1
else:
    d = int(math.log(25*N+26, 26))
pre = (pow(26,d)-26)//25
diff = N - pre
ans = [97 for _ in range(d)]
for i in range(d):
    if i == d-1:
        ans[i] += diff -1
        break
    if diff % pow(26, d-1-i) == 0:
        x = 25
    else:
        x = diff // pow(26, d-1-i)
    ans[i] += x
    diff -= x*pow(26, d-1-i)
for a in ans:
    print(chr(a), end='')

Fixed submission:
N = int(input())
ans = ''
while N >0:
    N -= 1
    ans += chr(97 + N%26)
    N //= 26
print(ans[::-1])

Buggy python submission (verdict: Runtime Error):
import math
N = int(input())

d = int(math.log(25*N+26, 26)-0.0000001)
pre = (pow(26,d)-26)//25
diff = N - pre
ans = [97 for _ in range(d)]
# print(d, diff)
for i in range(d):

    if i == d-1:
        ans[i] += diff -1
        break
    if diff % pow(26, d-1-i) == 0:
        x = 25
    else:
        x = diff // pow(26, d-1-i)
    # x = diff//26
    ans[i] += x
    # if x == 0:
    #     continue
    diff -= x*pow(26, d-1-i)
    # print(x, diff, pow(26, d-1-i))
# print(ans)
for a in ans:
    print(chr(a), end='')

Fixed submission:
N = int(input())
ans = ''
while N >0:
    N -= 1
    ans += chr(97 + N%26)
    N //= 26
print(ans[::-1])

Buggy python submission (verdict: Runtime Error):
import math
N = int(input())

d = int(math.log(25*N+26, 26)-0.0000001)
pre = (pow(26,d)-26)//25
diff = N - pre
ans = [97 for _ in range(d)]
# print(d, diff)
for i in range(d):
    # print(i, diff)
    # if diff <= 26:
    #     ans += chr(97+diff-1)
    #     break
    if i == d-1:
        ans[i] += diff -1
        break
    if diff % pow(26, d-1-i) == 0:
        x = 25
    else:
        x = diff // pow(26, d-1-i)
    # x = diff//26
    ans[i] += x
    if x == 0 :
        continue
    diff -= x*pow(26, d-1-i)
    # print(x, diff, pow(26, d-1-i))
# print(ans)
for a in ans:
    print(chr(a), end='')

Fixed submission:
N = int(input())
ans = ''
while N >0:
    N -= 1
    ans += chr(97 + N%26)
    N //= 26
print(ans[::-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
count = [0 for _ in range(int(1e5))]
for a in A:
    count[a] += 1    

Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    S += (c-b) * count[b]
    count[c] += count[b]
    count[b] = 0
    print(S)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
count = [0 for _ in range(int(1e5)+1)]
for a in A:
    count[a] += 1    

Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    S += (c-b) * count[b]
    count[c] += count[b]
    count[b] = 0
    print(S)

Buggy python submission (verdict: Time Limit Exceeded):
import collections

N = int(input())
A = list(map(int, input().split()))
C = collections.Counter(A)
Q = int(input())
X = [[] for _ in range(Q)]
for i in range(Q):
    X[i] = list(map(int, input().split()))

for i in range(Q):
    b, c = X[i]
    for j in range(C[b]):
        A.remove(b)
        A.append(c)
    C = collections.Counter(A)
    print(sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
count = [0 for _ in range(int(1e5)+1)]
for a in A:
    count[a] += 1    

Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    S += (c-b) * count[b]
    count[c] += count[b]
    count[b] = 0
    print(S)
Buggy python submission (verdict: Runtime Error):
X, N = map(int, input().split())
p = list(map(int, input().split()))

p.sort()
def main():
    l = [True for _ in range(100)]
    for x in p:
        l[x-1] = False
    diff = 0
    while True:
        if l[X-1 -diff]:
            print(X -diff)
            return
        elif l[X-1 +diff]:
            print(X +diff)
            return
        diff += 1

main()

Fixed submission:
X, N = map(int, input().split())
p = list(map(int, input().split()))

p.sort()
def main():
    l = [True for _ in range(100)]
    for x in p:
        l[x-1] = False
    diff = 0
    while True:
        if X-1 -diff < 0:
            print(0)
            return
        elif l[X-1 -diff]:
            print(X -diff)
            return
        elif X-1 +diff >= 100:
            print(101)
            return
        elif l[X-1 +diff]:
            print(X +diff)
            return
        diff += 1

main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
A.sort()
used = [0 for _ in range(int(1e6+1))]

ans = 0
for i in A:
    if used[i] == 1:
        used[i] += 1
        continue
    for j in range(i, len(used), i):
        used[j] += 1
for a in A:
    if used[a] == 1:
        ans += 1
print(ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A.sort()
used = [0 for _ in range(int(1e6+1))]

ans = 0
for i in A:
    if used[i] > 0:
        used[i] += 1
        continue
    for j in range(i, len(used), i):
        used[j] += 1
for a in A:
    if used[a] == 1:
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

ans = 1
for a in A:
    ans *= a
if ans > 1e18:
    ans = -1
print(ans)

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
def main():
    if c[0] >0:
        print(0)
        return

    ans = 1
    for a in A:
        ans *= a
        if ans > 1e18:
            ans = -1
            break
    if ans > 1e18:
        ans = -1
    print(ans)
main()
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

if N > K:
    ans = 0
    while K > 0:
        ans = A[ans] -1
        K -= 1
    print(ans+1)
else:
    count = 0
    a = 0
    visited = [0]
    while True:
        a = A[a]-1
        count += 1
        if a in visited:
            break
        visited.append(a)
    before_roop = visited.index(a)
    cycle = len(visited) - before_roop
    mod = (K - before_roop)%cycle
    print(visited[before_roop + mod] +1)


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))

if N > K:
    ans = 0
    while K > 0:
        ans = A[ans] -1
        K -= 1
    print(ans+1)
else:
    count = 0
    a = 0
    # visited = [0]
    is_visited = [0 for _ in range(N)]
    is_visited[0] = 1
    while True:
        a = A[a]-1
        count += 1
        if is_visited[a] > 0:
            before_roop = is_visited[a]-1
            cycle = count - before_roop
            break
        is_visited[a] = count+1
        # if a in visited:
        #     break
        # visited.append(a)
    # before_roop = visited.index(a)
    # cycle = len(visited) - before_roop
    mod = (K - before_roop)%cycle
    # print(visited[before_roop + mod] +1)
    print(is_visited.index(is_visited[a] +mod) +1)
Buggy python submission (verdict: Time Limit Exceeded):
A, B, N = map(int, input().split())
ans = 0
for i in range(N+1):
    x = (A*i)//B - A*(i//B)
    ans = max(ans, x)
print(ans)

Fixed submission:

A, B, N = map(int, input().split())

if N >= B:
    print((A*(B-1))//B)
else:
    print((A*N)//B - A*(N//B))
Buggy python submission (verdict: Runtime Error):
N, K, C = map(int, input().split())
S = input()
L, R = [0]*N, [0]*N
i = 0
count = 1
while count <= K:
    if S[i] == 'x':
        i += 1
        continue
    L[i] = count
    count += 1
    i += C+1
i = N-1
count = K
while count >= 0:
    if S[i] == 'x':
        i -= 1
        continue
    R[i] = count
    count -= 1
    i -= C+1
for i in range(N):
    if L[i] == 0:
        continue
    if L[i] == R[i]:
        print(i+1)

Fixed submission:
N, K, C = map(int, input().split())
S = input()
L, R = [0]*N, [0]*N
i = 0
count = 1
while count <= K:
    if S[i] == 'x':
        i += 1
        continue
    L[i] = count
    count += 1
    i += C+1
i = N-1
count = K
while count > 0:
    if S[i] == 'x':
        i -= 1
        continue
    R[i] = count
    count -= 1
    i -= C+1
for i in range(N):
    if L[i] == 0:
        continue
    if L[i] == R[i]:
        print(i+1)
Buggy python submission (verdict: Runtime Error):
from scipy.special import comb

n, a, b = map(int, input().split())
print((2**n -1 -comb(n, a, exact=True)- comb(n, b, exact=True))%(10**9+7))

Fixed submission:
n, a, b = map(int, input().split())
mod = int(1e9+7)

def comb(r):
    bunshi = 1
    for i in range(n-r+1,n+1):
        bunshi = bunshi*i%mod
    bunbo = 1
    for i in range(1,r+1):
        bunbo = bunbo * i %mod
    return bunshi*pow(bunbo, mod-2,mod)%mod

ans = pow(2,n,mod) -1 - comb(b)-comb(a)
ans = (ans +2*mod)%mod
print(ans)

Buggy python submission (verdict: Runtime Error):
from scipy.special import comb

n, a, b = map(int, input().split())
total = 0
for i in range(1,n+1):
    if i == a or i == b:
        continue
    total += comb(n, i, exact=True)
print(total%(10**9+7))

Fixed submission:
n, a, b = map(int, input().split())
mod = int(1e9+7)

def comb(r):
    bunshi = 1
    for i in range(n-r+1,n+1):
        bunshi = bunshi*i%mod
    bunbo = 1
    for i in range(1,r+1):
        bunbo = bunbo * i %mod
    return bunshi*pow(bunbo, mod-2,mod)%mod

ans = pow(2,n,mod) -1 - comb(b)-comb(a)
ans = (ans +2*mod)%mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a_n = [int(i) for i in input().split()]

def main():
    for i in range(len(a_n)):
        for j in range(i):
            if a_n[i] == a_n[j]:
                print('NO')
                return 0
    print('YES')
    return 0

main()

Fixed submission:
n = int(input())
a_n = [int(i) for i in input().split()]

def has_duplicates(seq):
    return len(seq) != len(set(seq))

if has_duplicates(a_n):
    print('NO')
else:
    print('YES')
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2

def test3():
    e_n = list(map(expected, p_n))
    s_n = []
    for i, e in enumerate(e_n):
        if i == 0:
            s_n.append(e)
            continue
        s_n.append(s_n[i-1] + e)
    result = []
    for i in range(len(s_n)):
        if i+k >= n:
            continue
        result.append(s_n[i+k]-s_n[i])
    print(max(result))      

test3()

Fixed submission:
n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2

def test3():
    e_n = list(map(expected, p_n))
    s_n = []
    for i, e in enumerate(e_n):
        if i == 0:
            s_n.append(e)
            continue
        s_n.append(s_n[i-1] + e)
    result = []
    result.append(s_n[k-1])
    for i in range(len(s_n)):
        if i+k >= n:
            continue
        result.append(s_n[i+k]-s_n[i])
    print(max(result))      
test3()

Buggy python submission (verdict: Time Limit Exceeded):

n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2

e_n = list(map(expected, p_n))
result = []
for i in range(n-k+1):
    window_total = 0
    for j in range(k):
        window_total +=e_n[i+j]
    result.append(window_total)
print(max(result))

Fixed submission:
n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2

def test3():
    e_n = list(map(expected, p_n))
    s_n = []
    for i, e in enumerate(e_n):
        if i == 0:
            s_n.append(e)
            continue
        s_n.append(s_n[i-1] + e)
    result = []
    result.append(s_n[k-1])
    for i in range(len(s_n)):
        if i+k >= n:
            continue
        result.append(s_n[i+k]-s_n[i])
    print(max(result))      
test3()

Buggy python submission (verdict: Runtime Error):
import pandas as pd

n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2*k

s = pd.Series(list(map(expected, p_n)))

print(s.rolling(window=k).mean().max())

Fixed submission:
n, k = map(int, input().split())
p_n = [int(i) for i in input().split()]

def expected(x):
    return (x+1)/2

def test3():
    e_n = list(map(expected, p_n))
    s_n = []
    for i, e in enumerate(e_n):
        if i == 0:
            s_n.append(e)
            continue
        s_n.append(s_n[i-1] + e)
    result = []
    result.append(s_n[k-1])
    for i in range(len(s_n)):
        if i+k >= n:
            continue
        result.append(s_n[i+k]-s_n[i])
    print(max(result))      
test3()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = int(1e9+7)
A.sort(reverse=True)
for i in range(N):
    A[i] = format(A[i], 'b')
    A[i] = A[i].zfill(len(A[0]))
ans = 0
for i in range(len(A[0])):
    count_1 = 0
    count_0 = 0
    for a in A:
        if a[i] == '0':
            count_0 += 1
        else:
            count_1 += 1
    ans += count_0 * count_1 * pow(2, len(A[0])-i-1, mod) 
    ans %= mod
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
mod = int(1e9+7)
A.sort(reverse=True)
for i in range(N):
    A[i] = format(A[i], 'b')
    A[i] = A[i].zfill(len(A[0]))
ans = 0
for i in range(len(A[0])):
    count_1 = 0
    count_0 = 0
    for a in A:
        if a[i] == '0':
            count_0 += 1
        else:
            count_1 += 1
    ans += count_0 * count_1 * pow(2, len(A[0])-i-1, mod) 
    ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int, input().split()))
L.sort()

count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        a = L[i]
        b = L[j]
        low = max(a-b, b-a)
        hi = a+b
        for k in range(j+1, N):
            c = L[k]
            if low < c and c < hi:
                count += 1
            elif c >= hi:
                break
print(count)

Fixed submission:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()

count = 0
for i in range(N):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        if k > j:
            count += k -j -1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
ans = 0
for i in range(N):
    ans += i
print(ans)

Fixed submission:
N = int(input())
print(N*(N-1)//2)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
ans = 0
for i in range(N):
    s_i = ''.join(sorted(input()))
    if s_i in s:
        ans += s.count(s_i)
    s.append(s_i)

# s.sort()
# ans = 0
# print(s)
# for i in range(N):
#     for j in range(i+1, N):
#         if s[i] == s[j]:
#             ans += 1
#         elif s[i] < s[j]:
#             print(s[i], s[j])
#             break
print(ans)

Fixed submission:
import bisect
N = int(input())
s = []
for i in range(N):
    s_i = ''.join(sorted(input()))
    s.append(s_i)

s.sort()
ans = 0
for i in range(N):
    ans += bisect.bisect_right(s, s[i])-i-1
    # for j in range(i+1, N):
    #     if s[i] == s[j]:
    #         ans += 1
    #     elif s[i] < s[j]:
    #         break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = []
for i in range(N):
    s.append(''.join(sorted(input())))
s.sort()
ans = 0
for i in range(N):
    for j in range(i+1, N):
        if s[i] == s[j]:
            ans += 1
        elif s[i] < s[j]:
            break
print(ans)

Fixed submission:
import bisect
N = int(input())
s = []
for i in range(N):
    s_i = ''.join(sorted(input()))
    s.append(s_i)

s.sort()
ans = 0
for i in range(N):
    ans += bisect.bisect_right(s, s[i])-i-1
    # for j in range(i+1, N):
    #     if s[i] == s[j]:
    #         ans += 1
    #     elif s[i] < s[j]:
    #         break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N, M = map(int, input().split())
X = []
for i in range(N):
    X.append(list(map(int, input().split())))
X.sort(key=lambda x: (-x[1], x[0]))

days = [i for i in range(1, M+1)]
ans = 0
for a, b in X:
    if len(days) == 0:
        break
    if days[-1] < a:
        continue
    index = bisect.bisect_right(days, a)
    if index > 0 and days[index-1] >= a:
        days.pop(index-1)
    elif index > 0 and days[index-1] <a:
        days.pop(index) 
    elif index == 0 and days[0] >= a:
        days.pop(0)
    else:
        continue
    ans += b
print(ans)

Fixed submission:
import bisect
import heapq
N, M = map(int, input().split())
X = [[] for _ in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    if a <= M:
        X[a-1].append(-b)
ans = 0
heap = []
for i in range(M):
    for b in X[i]:
        heapq.heappush(heap, b)
    if len(heap) > 0:
        ans -= heapq.heappop(heap)
print(ans)

def TLE():
    X.sort(key=lambda x: (-x[1], x[0]))
    days = [i for i in range(1, M+1)]
    ans = 0
    for a, b in X:
        if len(days) == 0:
            break
        if days[-1] < a:
            continue
        index = bisect.bisect_right(days, a)
        if index > 0 and days[index-1] >= a:
            days.pop(index-1)
        elif index > 0 and days[index-1] <a:
            days.pop(index) 
        elif index == 0 and days[0] >= a:
            days.pop(0)
        else:
            continue
        ans += b
    print(ans)
# TLE()
Buggy python submission (verdict: Runtime Error):
import math
a, b, c, d = map(int, input().split())

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

def main():
    left = (b -int(b/c) -int(b/d) +int(b/lcm(c,d)))
    right = ((a-1) -int((a-1)/c) -int((a-1)/d) +int((a-1)/lcm(c,d)))
    print(left - right)
main()

Fixed submission:
from fractions import gcd

A, B, C, D = map(int, input().split())
l = C * D // gcd(C, D)
x = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // l
y = B - B // C - B // D + B // l
print(y - x)