Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    print(loop[x+1])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Fixed submission:
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    print(loop[K])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K <= x:
    print(loop[x+1])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Fixed submission:
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    print(loop[K])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    print(loop[x+1])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])

Fixed submission:
N, K = map(int, input().split())
A = [-1] + list(map(int, input().split()))

visited = set([1])
loop = [1]
now = 1

for _ in range(N):
    now = A[now]
    if now in visited:
        break
    else:
        loop.append(now)
        visited.add(now)

x = loop.index(now)

if K < x:
    print(loop[K])
else:
    K -= x
    loop = loop[x:]
    K %= len(loop)
    print(loop[K])
Buggy python submission (verdict: Time Limit Exceeded):
# D - Pairs

import numpy as np

N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))), np.int64)

n_zero = len(A[A == 0])

def n_pairs_less_than_or_equal_to_border(product_border):
    n_pairs = -np.sum(A*A <= product_border) # i==jかつ条件を満たすペアの個数を先に引き、以降重複を許して数える
    n_pairs += n_zero * N * (product_border >= 0)

    for i in range(N):
        if A[i] > 0:
            n_pairs += np.searchsorted(A, product_border//A[i], side="right")
        elif A[i] < 0:
            n_pairs += N - np.searchsorted(A, -(-product_border//A[i]), side="left")

    n_pairs //= 2
    return n_pairs


lower = -(10**18)
upper = 10**18

while lower <= upper:
    x = (lower + upper) // 2
    if n_pairs_less_than_or_equal_to_border(x) >= K:
        ans = x
        upper = x - 1
    else:
        lower = x + 1

print(ans)

Fixed submission:
# D - Pairs

import numpy as np

N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))), np.int64)

n_zero = len(A[A == 0])
A_posi = A[A > 0]
A_nega = A[A < 0]

def n_pairs_less_than_or_equal_to_border(product_border):
    n_pairs = -np.sum(A*A <= product_border) # i==jかつ条件を満たすペアの個数を先に引き、以降重複を許して数える
    n_pairs += n_zero * N * (product_border >= 0)

    n_pairs += np.searchsorted(A, product_border//A_posi, side="right").sum()
    n_pairs += (N - np.searchsorted(A, -(-product_border//A_nega), side="left")).sum()

    # 上記2行は下記と同様の処理だが下記の実装だとTLE
    # for i in range(N):
    #     if A[i] > 0:
    #         n_pairs += np.searchsorted(A, product_border//A[i], side="right")
    #     elif A[i] < 0:
    #         n_pairs += N - np.searchsorted(A, -(-product_border//A[i]), side="left")

    n_pairs //= 2
    return n_pairs


lower = -(10**18)
upper = 10**18

while lower <= upper:
    x = (lower + upper) // 2
    if n_pairs_less_than_or_equal_to_border(x) >= K:
        ans = x
        upper = x - 1
    else:
        lower = x + 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)], key = lambda x: x[0])

dp = [0] + [float("inf")]*(H + AB[-1][0])
for j in range(H+1):
    for i in range(N):
        if dp[j] < float("inf"):
            try:
                dp[j + AB[i][0]] = min(dp[j + AB[i][0]], dp[j] + AB[i][1])
            except IndexError:
                pass

print(min(dp[H:]))

Fixed submission:
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

dp = [float("inf")]*H + [0]
ans = float("inf")
for j in reversed(range(H+1)):
    for i in range(N):
        if dp[j] < float("inf"):
            if j - AB[i][0] >= 0:
                dp[j - AB[i][0]] = min(dp[j - AB[i][0]], dp[j] + AB[i][1])
            else:
                ans = min(ans, dp[j] + AB[i][1])

print(min(dp[0], ans))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

# ある地点を渡すと、その地点から到達可能かつ距離未計算の地点のリストを返す
def not_investigated_positions(current_position):
    output = []
    for udrl in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
        y = current_position[0]+udrl[0]
        x = current_position[1]+udrl[1]
        if 0 <= y <= H-1 and 0 <= x <= W-1:
            if S[y][x] == "." and distance[y][x] == -1:
                output.append([y, x])
    return output

ans = 0
for sy in range(H):
    for sx in range(W):
        for gy in range(H):
            for gx in range(W):
                if S[sy][sx] == "#" or S[gy][gx] == "#":
                    continue
                distance = [[-1]*W for _ in range(H)]
                current_position = [sy, sx]
                distance[sy][sx] = 0
                next_positions = deque()
                next_positions.append(current_position)

                while next_positions and current_position != [gy, gx]:
                    current_position = next_positions.popleft()
                    for q in not_investigated_positions(current_position):
                        distance[q[0]][q[1]] = distance[current_position[0]][current_position[1]] + 1
                        next_positions.append(q)

                ans = max(ans, distance[gy][gx])

print(ans)


Fixed submission:
# D - Maze Master

from collections import deque

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

# ある地点を渡すと、その地点から到達可能かつ距離未計算の地点のリストを返す
def not_investigated_positions(current_position):
    output = []
    for udrl in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
        y = current_position[0]+udrl[0]
        x = current_position[1]+udrl[1]
        if 0 <= y <= H-1 and 0 <= x <= W-1:
            if S[y][x] == "." and distance[y][x] == -1:
                output.append([y, x])
    return output

ans = 0

for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == "#":
            continue
        distance = [[-1]*W for _ in range(H)]
        current_position = [sy, sx]
        distance[sy][sx] = 0
        next_positions = deque()
        next_positions.append(current_position)

        while next_positions:
            current_position = next_positions.popleft()
            for q in not_investigated_positions(current_position):
                distance[q[0]][q[1]] = distance[current_position[0]][current_position[1]] + 1
                next_positions.append(q)

        ans = max(ans, max(map(max,distance)))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x > A[i]:
            break
        elif (1<<x) & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x > A[i]:
            break
        elif (1<<x) & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x > A[i]:
            break
        elif 2**x & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x > A[i]:
            break
        elif (1<<x) & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

bit = [0] * 61
for i in range(N):
    for x in range(61):
        if 2**x > A[i]:
            break
        elif (1<<x) & A[i] > 0:
            bit[x] += 1

ans = 0
for i in range(61):
    ans += 2**i * (bit[i] * (N-bit[i]))
    ans %= MOD

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

N = int(sys.stdin.buffer.readline())

neighbor = [[] for _ in range(N)]
edge = []
for i in range(N-1):
    a, b = map(int, sys.stdin.buffer.readline().split())
    neighbor[a-1].append(b-1)
    neighbor[b-1].append(a-1)
    edge.append([a-1, b-1, i])

K = len(max(neighbor, key=len))
print(K)
color = [set(range(1, K+1)) for _ in range(N)]

edge = sorted(edge, key = lambda x:x[0])
for idx in range(N-1):
    c = color[edge[idx][0]].pop()
    edge[idx].append(c)
    color[edge[idx][1]].remove(c)

edge = sorted(edge, key=lambda x: x[2])
for x in edge:
    print(x[-1])

Fixed submission:
import sys
sys.setrecursionlimit(500000)

N = int(sys.stdin.buffer.readline())

neighbor = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, sys.stdin.buffer.readline().split())
    neighbor[a-1].append([b-1, i])
    neighbor[b-1].append([a-1, i])

K = max(len(l) for l in neighbor)
print(K)
ans = [-1]*(N-1)

def dfs(current_point, parent_point, parent_edge_color):
    color = 1
    for next_point, idx in neighbor[current_point]:
        if next_point != parent_point:
            if parent_edge_color == color:
                color += 1
            ans[idx] = color
            dfs(next_point, current_point, color)
            color += 1

dfs(0, -1, -1)
print("\n".join(map(str, ans)))
Buggy python submission (verdict: Time Limit Exceeded):
# E - Gluttony

import numpy as np

# np.arrayに変換する場合、sortedを使うよりnp.sortを使う方が速い(使わないとTLE)
N, K = map(int, input().split())
A = np.sort(np.array((list(map(int, input().split())))))
F = np.sort(np.array((list(map(int, input().split())))))[::-1]

lower = 0
upper = A[-1] * F[0]

# 達成可能な最高成績を二分探索する
while lower <= upper:
    result = (lower + upper) // 2
    if sum(np.clip(A - result//F, 0, None)) <= K: # resultを達成可能なAと実際のAの差異の総和が必要な修行コスト
        ans = result
        upper = result - 1
    else:
        lower = result + 1

print(ans)

Fixed submission:
# E - Gluttony

import numpy as np

N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted((list(map(int, input().split()))), reverse = True))

lower = 0
upper = A[-1] * F[0]

# 達成可能な最高成績を二分探索する
while lower <= upper:
    result = (lower + upper) // 2
    # resultを達成可能なAと実際のAの差異の総和が必要な修行コスト
    # np.arrayの和を求める場合、sum(array)よりarray.sum()の方が速い(使わないとTLE)
    if np.clip(A - result//F, 0, None).sum() <= K:
        ans = result
        upper = result - 1
    else:
        lower = result + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# E - Gluttony

import numpy as np

N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted(list(map(int, input().split())), reverse = True))

lower = 0
upper = A[-1] * F[0]

# 達成可能な最高成績を二分探索する
while lower <= upper:
    result = (lower + upper) // 2
    if sum(np.clip(A - result//F, 0, None)) <= K: # resultを達成可能なAと実際のAの差異の総和が必要な修行コスト
        ans = result
        upper = result - 1
    else:
        lower = result + 1

print(ans)

Fixed submission:
# E - Gluttony

import numpy as np

N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted((list(map(int, input().split()))), reverse = True))

lower = 0
upper = A[-1] * F[0]

# 達成可能な最高成績を二分探索する
while lower <= upper:
    result = (lower + upper) // 2
    # resultを達成可能なAと実際のAの差異の総和が必要な修行コスト
    # np.arrayの和を求める場合、sum(array)よりarray.sum()の方が速い(使わないとTLE)
    if np.clip(A - result//F, 0, None).sum() <= K:
        ans = result
        upper = result - 1
    else:
        lower = result + 1

print(ans)
Buggy python submission (verdict: Runtime Error):
H, W, A, B = map(int, input().split())

if H == 1:
    ans = "1"*A + "0"*(W-A)
    print(ans)
    exit()

if W == 1:
    for _ in range(B):
        print(1)
    for _ in range(H-B):
        print(0)
    exit()

ans = [[0]*W for _ in range(H)]

for i in range(A):
    for j in range(B):
        ans[i][j] = 1

for i in range(A, H):
    for j in range(B, W):
        ans[i][j] = 1

for a in ans:
    t = list(map(str, a))
    print("".join(t))

Fixed submission:
H, W, A, B = map(int, input().split())

if H == 1:
    ans = "1"*A + "0"*(W-A)
    print(ans)
    exit()

if W == 1:
    for _ in range(B):
        print(1)
    for _ in range(H-B):
        print(0)
    exit()

ans = [[0]*W for _ in range(H)]

for i in range(B):
    for j in range(A):
        ans[i][j] = 1

for i in range(B, H):
    for j in range(A, W):
        ans[i][j] = 1

for a in ans:
    t = list(map(str, a))
    print("".join(t))
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = map(int,input().split())
A_list = [int(input()) for _ in range (Q)]

result_list = []
for idx in range(N):
    result_list.append("Yes" if K - Q + A_list.count(idx+1) > 0 else "No")

for idx in range(N):
    print(result_list[idx])

Fixed submission:
import collections

N, K, Q = map(int,input().split())
A_list = [int(input())-1 for _ in range (Q)]

A_dic = collections.Counter(A_list)

for idx in range(N):
    print("Yes" if K - Q + A_dic[idx] > 0 else "No")
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A_list = list(map(int, input().split()))

for i in range(M):
    A_list[A_list.index(max(A_list))] //= 2

print(sum(A_list))

Fixed submission:
N, M = map(int, input().split())
A_list = list(map(int, input().split()))

import heapq, math
A_list = [-x for x in A_list]
heapq.heapify(A_list)

for _ in range(M):
    A_min = heapq.heappop(A_list)
    heapq.heappush(A_list, math.ceil(A_min/2))

print(-sum(A_list))
Buggy python submission (verdict: Runtime Error):
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    # sum[0:i+1]
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    # add x to i
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

def inv(X):
    B = Bit(len(X)+1)
    inv = 0
    for i in range(len(X)):
        inv += i - B.sum(X[i])
        B.add(X[i], 1)
    return inv

A_inv = inv(A) % MOD
A2_inv = inv(A*2) % MOD

print((K*A_inv + ((K*(K-1))//2) * (A2_inv - 2*A_inv)) % MOD)

Fixed submission:
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    # sum[0:i+1]
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    # add x to i
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

def inv(X):
    B = Bit(2000+1)
    inv = 0
    for i in range(len(X)):
        inv += i - B.sum(X[i])
        B.add(X[i], 1)
    return inv

A_inv = inv(A) % MOD
A2_inv = inv(A*2) % MOD

print((K*A_inv + ((K*(K-1))//2) * (A2_inv - 2*A_inv)) % MOD)
Buggy python submission (verdict: Runtime Error):
from collections import Counter

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

xy.sort(key=lambda x: x[1])
xy.sort(key=lambda x: x[0])

ans = N
diff = []

for i in range(N-1):
    for j in range(i+1, N):
        diff.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))

c = Counter(diff)
print(N - c.most_common()[0][1])

Fixed submission:
from collections import Counter

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

if N == 1:
    print(1)
    exit()

xy.sort(key=lambda x: x[1])
xy.sort(key=lambda x: x[0])

ans = N
diff = []

for i in range(N-1):
    for j in range(i+1, N):
        diff.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))

c = Counter(diff)
print(N - c.most_common()[0][1])
Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
if len(s) <= 2:
    print(0)
else:
    i = 2
    ans = 0
    while i < len(s):
        if s[i] == "C" and s[i-1] == "B" and s[i-2] == "A":
            s[i] = "A"
            s[i-1] = "C"
            s[i-2] = "B"
            ans += 1
            if i-3 >= 0 and s[i-3] == "A":
                i -= 1
            else:
                i += 2
        else:
            i += 1
    print(ans)

Fixed submission:
s = input()
s = s.replace("BC", "X")

ans = 0
a_count = 0

for i in range(len(s)):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "X":
        ans += a_count
    else:
        a_count = 0

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
if len(s) <= 2:
    print(0)
else:
    i = 2
    ans = 0
    while i < len(s):
        if s[i] == "C" and s[i-1] == "B" and s[i-2] == "A":
            s[i] = "A"
            s[i-1] = "C"
            s[i-2] = "B"
            ans += 1
            i -= 1
        else:
            i += 1
    print(ans)

Fixed submission:
s = input()
s = s.replace("BC", "X")

ans = 0
a_count = 0

for i in range(len(s)):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "X":
        ans += a_count
    else:
        a_count = 0

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
if len(s) <= 2:
    print(0)
else:
    i = 2
    ans = 0
    while i < len(s):
        if s[i] == "C" and s[i-1] == "B" and s[i-2] == "A":
            s[i] = "A"
            s[i-1] = "C"
            s[i-2] = "B"
            ans += 1
            i -= 1
        else:
            i += 1
    print(ans)

Fixed submission:
s = input()
s = s.replace("BC", "X")

ans = 0
a_count = 0

for i in range(len(s)):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "X":
        ans += a_count
    else:
        a_count = 0

print(ans)
Buggy python submission (verdict: Runtime Error):
# E - Product of Arithmetic Progression

import sys
input = sys.stdin.buffer.readline

Q = int(input())
MOD = 10**6 + 3

# a//b (MOD p)
def div_mod(a, b, p = MOD):
    return (a * pow(b, p-2, p)) % p

factorial_memo = [1, 1]
for i in range(2, 10**5 + MOD):
    factorial_memo.append((factorial_memo[-1] * i) % MOD)

for _ in range(Q):
    x, d, n = map(int, input().split())
    if x == 0 and d == 0:
        print(0)
    elif x == 0:
        ans = factorial_memo[n-1]
        ans *= pow(d, n-1, MOD)
        ans %= MOD
        print(ans)
    elif d == 0:
        print(factorial_memo[x])
    else:
        ans = pow(d, n, MOD)
        x_divided_by_d = div_mod(x, d, MOD)
        ans *= factorial_memo[x_divided_by_d + (n-1)]
        ans %= MOD
        ans = div_mod(ans, factorial_memo[x_divided_by_d - 1], MOD)
        print(ans)

Fixed submission:
# E - Product of Arithmetic Progression

import sys
input = sys.stdin.buffer.readline

Q = int(input())
MOD = 10**6 + 3

# a//b (MOD p)
def div_mod(a, b, p=MOD):
    return (a * pow(b, p-2, p)) % p

factorial_memo = [1, 1]
for i in range(2, MOD):
    factorial_memo.append((factorial_memo[-1] * i) % MOD)

def factorial(x, p=MOD):
    if x < p:
        return factorial_memo[x]
    else:
        return 0


for _ in range(Q):
    x, d, n = map(int, input().split())
    if x == 0 and d == 0:
        print(0)
    elif x == 0:
        ans = factorial(n-1)
        ans *= pow(d, n-1, MOD)
        ans %= MOD
        print(ans)
    elif d == 0:
        print(pow(x, n, MOD))
    else:
        ans = pow(d, n, MOD)
        x_divided_by_d = div_mod(x, d, MOD)
        ans *= factorial(x_divided_by_d + (n-1))
        ans %= MOD
        ans = div_mod(ans, factorial(x_divided_by_d - 1), MOD)
        print(ans)
Buggy python submission (verdict: Runtime Error):
# D - Integer Cards

# 小さい数字から順になるべく大きい数字に書き換える

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

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

idx = 0
for i in range(M):
    for _ in range(BC[i][0]):
        if A[idx] < BC[i][1]:
            A[idx] = BC[i][1]
            idx += 1
        else:
            break
    else:
        continue
    break
    
print(sum(A))

Fixed submission:
# D - Integer Cards

# 小さい数字から順になるべく大きい数字に書き換える

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

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

idx = 0
for i in range(M):
    for _ in range(BC[i][0]):
        if idx <= N-1 and A[idx] < BC[i][1]:
            A[idx] = BC[i][1]
            idx += 1
        else:
            break
    else:
        continue
    break
    
print(sum(A))