Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02707 (python)

Fixed submission:
n=int(input())
a=list(map(int,input().split(" ")))
t=[0 for i in range(n)]
for i in range(n-1):
  b=a[i]
  t[b-1]=t[b-1]+1
for j in range(n):
  print(t[j])

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
t=[0 for i in range(n)]
for i in a:
  b=a[i]
  t[b-1]=t[b-1]+1
for j in range(n):
  print(t[j])

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
t=[0 for i in range(n)]
for i in a:
  b=a[i]
  t[b-1]=t[b-1]+1
for j in range(n):
  print(t[j])

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split(" "))
list=[0 for i in range(n)]
for i in a:
  b=a[i]
  list[b]=list[b]+1
for j in list:
  print(list[j])
Problem p02753 (python)

Fixed submission:
s=list(input())
if ("A" not in s) or ("B" not in s):
  print("No")
else:
  print("Yes")

Buggy submission (verdict: Runtime Error):
a,b=list(map(float,input().split()))
import sys
for i in range(100000):
  if int(i*8/100)==a and int(i*10/100)==b:
    print(i)
    sys.exit()
print(-1)

Buggy submission (verdict: Runtime Error):
s=list(input())
if (A not in s) or (B not in s):
  print("No")
else:
  print("Yse")
Problem p02771 (python)

Fixed submission:
k=list(map(int,input().split()))
from collections import Counter
l=Counter(k)
if len(l)==2:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
k=list(map(int,input().split())))
from collections import Counter
l=Counter(k)
if len(l)==2:
  print("Yes")
else:
  print("No")
Problem p02777 (python)

Fixed submission:
s,t=list(input().split())
a,b=list(map(int,input().split()))
u=input()
 
if u==s:
  print(str(a-1)+" "+str(b))
else:
  print(str(a)+" "+str(b-1))

Buggy submission (verdict: Runtime Error):
s,t=list(input())
a,b=list(map(int,input().split()))
u=input()

if u==s:
  print(a-1)
else:
  print(b-1)
Problem p02859 (python)

Fixed submission:
a=int(input())
print(a*a)

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
if n%2!=0:
  print("No")
else:
  if s[:int(n/2)]==s[int(n/2):]:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
if n%2!=0:
  print("No")
else:
  if s[:int(n//2)]==s[int(n//2):]:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
n=int(input())
s=input()
if n%2!=0:
  print("No")
else:
  if s[:n//2]==s[n//2:]:
    print("Yes")
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
import math
def distance(x1,x2,y1,y2):
  d=math.sqrt((x1-x2)**2+(y1-y2)**2)
  return d
count=int(input())
zahyou=[]
for i in range(count):
  a=input().split(" ")
  zahyou.append(a)

lenth=0
for i in zahyou:
  for j in zahyou:
    lenth=lenth+distance(int(i[0]),int(j[0]),int(i[1]),int(j[1]))
print(lenth/count)
Problem p02860 (python)

Fixed submission:
a=int(input())
b=input()
if a%2 == 0:
  if b[:a//2]==b[a//2:]:
    print("Yes")
  else:
    print("No")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
n=input()
if n%2!=0:
  print("No")
else:
  if n[:n//2]==n[n//2:]:
    print("Yes")
  else:
    print("No")
Problem p02879 (python)

Fixed submission:
a = input().split(" ")
if int(a[0]) <= 9 and int(a[1]) <= 9:
  print(int(a[0])*int(a[1]))
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
import math
a=int(input())
list=[]
for i in range(1,int(math.sqrt(a))+1):
  if a%i==0:
    list.append(i+int(a/i)-2)
b=min(list)
print(b)

Buggy submission (verdict: Runtime Error):
kuku=[]
for i in range(1,10):
  for j in range(1,10):
    kuku.append(i*j)
  
a=int(input())
if a in kuku:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
a=int(input())
print(a*a)
Problem p02954 (python)

Fixed submission:
s=list(input())
s=s+["R"]
n=len(s)-1
ans_list=[0 for i in range(n)]

flg_1=0
flg_2=0
start=0
for i in range(1,n+1):
  if s[i-1]=="R" and s[i]=="L":
    flg_1=i
  elif s[i-1]=="L" and s[i]=="R":
    flg_2=i
    if (flg_2-start)%2==0:
      if (flg_1-start)%2!=0:
        ans_list[flg_1-1]=(flg_2-start)//2
        ans_list[flg_1]=(flg_2-start)//2
      else:
        ans_list[flg_1-1]=(flg_2-start)//2
        ans_list[flg_1]=(flg_2-start)//2
    else:
      if (flg_1-start)%2!=0:
        ans_list[flg_1-1]=(flg_2-start)//2+1
        ans_list[flg_1]=(flg_2-start)//2
      else:
        ans_list[flg_1-1]=(flg_2-start)//2
        ans_list[flg_1]=(flg_2-start)//2+1
    start=i

for i in ans_list:
  print(str(i)+str(" "),end="")

Buggy submission (verdict: Runtime Error):
l,r=list(map(int,input().split()))
s=[]
if r-l>2050:
  print(0)
else:
  for i in range(l,r+1):
    s.append(i%2019)
  s.sort()
  print(s[0]*s[1])
Problem p03013 (python)

Fixed submission:
n,m=list(map(int,input().split(" ")))
a=[]
dp=[float("inf") for i in range(n+1)]
for i in range(m):
  a=int(input())
  dp[a]=0
dp[0]=1
if dp[1] != 0:
  dp[1]=1
for i in range(2,n+1):
  if dp[i]!=0:
    dp[i]=dp[i-1]+dp[i-2]
print(dp[n]%1000000007)

Buggy submission (verdict: Runtime Error):
n,m=list(map(int,input().split(" ")))
a=[]
for i in range(m):
  a.append(int(input()))
dp=[float("inf") for i in range(n)]
dp[0]=0
if 1 in a:
  dp[1]=0
else:
  dp[1]=1
for i in range(2,n):
  if (i-1 not in a)and(i-2 not in a):
    dp[i]=dp[i-1]+dp[i-2]
  elif (i-1 not in a)and(i-2 in a):
    dp[i]=dp[i-1]
  elif (i-1 in a)and(i-2 not in a):
    dp[i]=dp[i-2]
  else:
    dp[i]=0
print(n)
Problem p03042 (python)

Fixed submission:
s=list(input())
s=list(map(int,s))

a=s[0]*10+s[1]
b=s[2]*10+s[3]

if (a==0 and b==0) or (a>=13 and b>=13) or (a==0 and b>=13) or (a>=13 and b==0):
  print("NA")
elif 0<a<=12 and 0<b<=12:
  print("AMBIGUOUS")
elif 0<a<=12:
  print("MMYY")
else:
  print("YYMM")

Buggy submission (verdict: Runtime Error):
if (a==0 and b==0) or (a>=13 and b>=13):
  print("NA")
elif 0<a<=12 and 0<a<=12:
  print("AMBIGUOUS")
elif 0<a<12:
  print("MMYY")
else:
  print("YYMM")
Problem p02987 (python)

Fixed submission:
s = set(input())
print('Yes' if len(s) == 2 else 'No')

Buggy submission (verdict: Runtime Error):
s = list(input())
s.sort()
if len(len(s))!=4:
  print("No")
elif s[0]==s[1] and s[2]==s[3]:
  	print("Yes")
else:
  	print("No")
Problem p02987 (python)

Fixed submission:
def f_f(str):
    for s in str:
        if str.count(s) != 2:
            return False
    return True
 
S = input()
if f_f(S):
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
from collections import*
n, m, *t = map(int, open(0).read().split())       
*w, s, t = [a-1for a in t]         
e = [[]for _ in range(n)]        
for u, v in zip(w[::2], w[1::2]):      
    e[u].append(v)

INF = 10**10
f = [[INF]*3for _ in range(n)]        
f[s][0] = 0        
q = deque([(s, 0)])           
while q:
    vi, j = q.popleft()         
    if vi == t and j == 0:
        print(f[vi][0]//3)
        break
    for ui in e[vi]:           
        if f[ui][(j+1)%3] < INF: continue           
        f[ui][(j+1)%3] = f[vi][j] + 1
        q.append((ui, (j+1)%3))
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
s = int(input())
list = [int(n) for n in input().split()]

s_l = sorted(list)
print(s_l[s//2] - s_l[s//2 - 1])

Buggy submission (verdict: Runtime Error):
s = int(input())
list = [int(n) for n in input().split()]

sum = 0

for i in range(1, s - 1):
    if (list[i] - list[i - 1]) * (list[i + 1] - list[i]) > 0:
        sum += 1

print(sum)
Problem p03315 (python)

Fixed submission:
calc = input()
result = 0
for letter in calc:
    if letter == '+':
        result = result+1
    else:
        result = result-1
print(result)

Buggy submission (verdict: Runtime Error):
info = list(map(int, input().split(' ')))
array = list(map(int, input().split(' ')))

print((info[0]-1)//(info[1]-1))
Problem p03041 (python)

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

tmp = S[K-1].lower()
S[K-1] = tmp
ans = "".join(S)

print(ans)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split(" "))
S = input()

tmp = S[K-1].lower()
S[K-1] = tmp
ans = "".join(S)

print(ans)
Problem p03017 (python)

Fixed submission:
N,A,B,Q,D = map(int,input().split())
S = input()
C = []
for i in range(len(S)):
    C.append(S[i])

if Q<D:
    error = 0
    for i in range(A,D-1):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
else:
    error = 0
    for i in range(B,D-1):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
    flag = 0
    for i in range(B-1,D):
        if C[i-1]=="." and C[i+1]=="." and C[i]==".":
            flag = 1
    if flag==0:
        error = 1

if error==0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,A,B,Q,D = map(int,input().split())
S = input()
C = []
for i in range(len(S)):
    C.append(S[i])

if Q<D:
    error = 0
    for i in range(B,C):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
else:
    error = 0
    for i in range(B,D):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
    flag = 0
    for i in range(B-1,D):
        if C[i-1]=="." and C[i+1]=="." and C[i]==".":
            flag = 1
    if flag==0:
        error = 1

if error==0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,A,B,Q,D = map(int,input().split())
S = input()
C = []
for i in range(len(S)):
    C.append(S[i])

if Q<D:
    error = 0
    for i in range(len(C)-1):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
else:
    error = 0
    for i in range(len(C)-2):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
    flag = 0
    for i in range(max(A,B),min(Q,D)+1):
        if C[i]=="." and C[i+1]=="." and C[i+2]==".":
            flag = 1
    if flag==0:
        error = 1

if error==0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N,A,B,Q,D = map(int,input().split())
S = input()
C = []
for i in range(len(S)):
    C.append(S[i])

if Q<D:
    error = 0
    for i in range(len(C)-1):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
else:
    error = 0
    for i in range(len(C)-2):
        if C[i]=="#" and C[i+1]=="#":
            error = 1
            break
    flag = 0
    for i in range(max(A,B),min(Q,D)):
        if C[i]=="." and C[i+1]=="." and C[i+2]==".":
            flag = 1
    if flag==0:
        error = 1

if error==0:
    print("Yes")
else:
    print("No")
Problem p02987 (python)

Fixed submission:
S = input()
S = sorted(S,key=str)
if S.count(S[0]) == 2 and S[2] == S[3]:
    print("Yes")
    exit()
print("No")

Buggy submission (verdict: Runtime Error):
N = int(input())
d = list(map(int,input().split()))
dunder = 0
count = 0
for i in range(max(d)):
    dunder = sum (x<i for x in d)
    if 2*dunder == len(d):
        count += 1
    elif 2*dunder > len(d):
        break
print(count)
Problem p02989 (python)

Fixed submission:
N = int(input())
d = list(map(int,input().split()))
d.sort()
m = N//2
if d[m] == d[m-1]:
    print(0)
else:
    print(d[m]-d[m-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
d = list(map(int,input().split()))
dunder = 0
count = 0
for i in range():
    dunder = sum (x<i for x in d)
    if 2*dunder == len(d):
        count += 1
    elif 2*dunder > len(d):
        break
print(count)
Problem p03250 (python)

Fixed submission:
n = sorted([int(num) for num in input().split()])
print(n[2]*10+n[1]+n[0])

Buggy submission (verdict: Runtime Error):
n = sorted(list(map(int,input().split())),revese=True)
print(10*n[0]+n[1]+n[2])
Problem p03834 (python)

Fixed submission:
print(input().replace(","," "))

Buggy submission (verdict: Runtime Error):
print(input().replace(/,/g," "))
Problem p02987 (python)

Fixed submission:
#４文字の文字列Sが二種類で2回ずつ現れる判定
S = input()
S_list = set(list(S))
#print(S_list)




if len(S_list) == 2:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
n = int(input())
P_list = list(map(int,(input().split(" "))))
#print(P_list)
count = 0
i = 0
for i in range(n-2):
    if P_list[i] > P_list[i+1] > P_list[i+2] or P_list[i] < P_list[i+1] < P_list[i+2]:
        count = count + 1

#print(n)
#print(P_list)
print(count)
Problem p03207 (python)

Fixed submission:
n = int(input())
p = [int(input()) for _ in range(n)]

p.sort()
print(sum(p[:-1]) + p[-1] // 2)

Buggy submission (verdict: Runtime Error):
n = int(input())
p = [int(input()) for _ in range(n)]

p.sort()
print(sum(p[:-1] + p[-1] // 2))
Problem p03291 (python)

Fixed submission:
S = input()
n = len(S)
mod = (10**9 + 7)

dp = [[0] * 4 for _ in range(n + 1)]

dp[n] = [0, 0, 0, 1]
for i in range(n-1, -1, -1):
    if S[i] == '?':
        dp[i][3] =  dp[i+1][3] * 3 % mod
        dp[i][2] = (dp[i+1][2] * 3 + dp[i+1][3]) % mod
        dp[i][1] = (dp[i+1][1] * 3 + dp[i+1][2]) % mod
        dp[i][0] = (dp[i+1][0] * 3 + dp[i+1][1]) % mod
    elif S[i] == 'C':
        dp[i][3] =  dp[i+1][3] % mod
        dp[i][2] = (dp[i+1][2] + dp[i+1][3]) % mod
        dp[i][1] =  dp[i+1][1] % mod
        dp[i][0] =  dp[i+1][0] % mod
    elif S[i] == 'B':
        dp[i][3] =  dp[i+1][3] % mod
        dp[i][2] =  dp[i+1][2] % mod
        dp[i][1] = (dp[i+1][1] + dp[i+1][2]) % mod
        dp[i][0] =  dp[i+1][0] % mod
    elif S[i] == 'A':
        dp[i][3] =  dp[i+1][3]
        dp[i][2] =  dp[i+1][2]
        dp[i][1] =  dp[i+1][1]
        dp[i][0] = (dp[i+1][0] + dp[i+1][1]) % mod
print(dp[0][0])

Buggy submission (verdict: Runtime Error):
init_input()

S = input()
n = len(S)
mod = (10**9 + 7)

A = [0] * n
C = [0] * n
Q = [0] * n
Bi = []
Qi = []

for i, s in enumerate(S):
    if s == 'A':
        A[i] = 1
    elif s == 'B':
        Bi.append(i)
    elif s == 'C':
        C[i] = 1
    else:
        Q[i] = 1
        Qi.append(i)

for i in range(1, n):
    A[i] += A[i - 1]
    C[i] += C[i - 1]
    Q[i] += Q[i - 1]

def mod_pow(x, n, mod):
    if n == 0: return 1
    d, m = n >> 1, n & 1
    p = mod_pow(x, d, mod)
    return (p * p * (x if m else 1))

from itertools import chain
ans = 0
for b in chain(Bi, Qi):
    if b == 0 or n - 1 <= b: continue
    ai = A[b - 1]
    qa = Q[b - 1]
    a = (ai * mod_pow(3, qa, mod))
    if qa > 0:
        a += (mod_pow(3, qa - 1, mod) * qa)

    lc = C[n - 1] - C[b]
    lqc = Q[n - 1] - Q[b]
    c = (lc * mod_pow(3, lqc, mod))
    if lqc > 0:
        c += (mod_pow(3, lqc - 1, mod) * lqc)
    
    ans += (a * c) % mod

print(ans % mod)
Problem p02984 (python)

Fixed submission:
n=int(input())
arr=list(map(int,input().split()))
arr2=[0]*n
for i in range(0,2*n,2):
  arr2[i//2]=arr[i%n]
ans=[0]*n
s=sum(arr)//2
cum=[0]*n
start=sum(arr2[:n//2])
cum[0]=start
l=n//2
for i in range(n):
  start=start+arr2[(i+l)%n]-arr2[i]
  cum[(i+1)%n]=start
for i in range(n):
  ans[(n-1+i*2)%n]=(s-cum[i])*2
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
N, K = map(int, input().split())
mod = 10**9 + 7
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
def p(n, k):
    x = 1
    for i in range(k):
        x *= n - i
        x %= mod
    return x
def f(x, par):
    L = len(G[x])
    if x == par:
        if L <= K-1:
            ans = K*p(K-1,L) % mod
        else:
            return 0
    elif x != par:
        if L-1 <= K-2:
            ans = p(K-2,L-1)
        else:
            return 0
    for i in G[x]:
        if i != par:
            ans *= f(i, x)
            ans %= mod
    return ans
print(f(0, 0))
Problem p03307 (python)

Fixed submission:
N = input()
N = int(N)
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

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

print(lcm(2, N))

Buggy submission (verdict: Runtime Error):
N = input()
aryA = input().split(" ")

N = int(N)
aryA = [int(s) for s in aryA]
n = len(aryA)

tot = []
totlist = []
b = N

for m in range(b):
    for x in range(n):
        tot.append(abs(aryA[x] - (m + x + 1)))
    totlist.append(sum(tot))

print(min(totlist))

Buggy submission (verdict: Runtime Error):
import math
N = input()
N = int(N)
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(lcm(2, N))
Problem p02990 (python)

Fixed submission:
from math import factorial
N, K = map(int, input().split())
mod = 10**9 + 7
def combination(n, r):
	if n >= r:
		return int(factorial(n) // (factorial(r) * factorial(n - r)))
	else:
		return 0

for i in range(1, K+1):
	print(combination(N-K+1, i) * combination(K-1, i-1)%mod)

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1)%(10**9+7))

Buggy submission (verdict: Runtime Error):
import math

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

for i in range(1,K+1):
	print(math.factorial(N-K+1)//math.factorial(N-K+1-i)//math.factorial(i)*math.factorial(K-1)//math.factorial(K-i)//math.factorial(i-1)%1000000007)

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1)%1000000007)

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//math.factorial(n-r)//math.factorial(r)

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1)%1000000007)

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//math.factorial(n-r)//math.factorial(r)%1000000007

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1)%1000000007)

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//math.factorial(n-r)//math.factorial(r)%1000000007

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1))

Buggy submission (verdict: Runtime Error):
import math

def comb(n,r):
	return math.factorial(n)//math.factorial(n-r)//math.factorial(r)%1000000007

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

for i in range(1,K+1):
	print(comb(N-K+1,i)*comb(K-1,i-1))
Problem p03041 (python)

Fixed submission:
N, K = map(int, input().split())
S = str(input())

if K == 1:
	S = S[K-1].lower() + S[K:N]
elif K == N:
	S = S[0:N-1] + S[N-1].lower()
else:
	S = S[0:K-1] + S[K-1].lower() + S[K:N]

print(S)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = str(input())

if K == 1:
	S = S[K-1].lower() + S[K:N]
elif K == N:
	S = S[0:N-1] + S[N].lower()
else:
	S = S[0:K-1] + S[K-1].lower() + S[K:N]

print(S)
Problem p02989 (python)

Fixed submission:
N = int(input())
l = list(map(int, input().split()))
l.sort()
print(l[len(l)//2] - l[len(l)//2-1])

Buggy submission (verdict: Runtime Error):
N = int(input())
l = list(map(int, input().split()))
l.sort()
print(l[N/2-1]-l[N/2])
Problem p02989 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a.sort()
answer = a[int(n/2)] - a[int(n/2 - 1)]
print(answer)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[int(n/2 + 1)] == a[int(n/2)]:
    answer = 0
else:
    answer = a[int(n/2)] - a[int(n/2 - 1)]
print(answer)
Problem p03038 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
import heapq
import bisect
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    ret = sum(A)
    cs = Counter(A)
    for c in C:
        if c not in cs:
            cs[c] = 0
    vs = sorted(cs.keys())

    i = 0
    while cs[vs[i]] == 0:
        i += 1

    for b, c in zip(B, C):
        while 0 < b:
            if c <= vs[i]:
                break
            bb = min(b, cs[vs[i]])
            b -= bb
            cs[vs[i]] -= bb
            cs[c] += bb
            ret += bb * (c - vs[i])
            if cs[vs[i]] == 0:
                i += 1
    return ret


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(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]" 
    C = [int()] * (M)  # type: "List[int]" 
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    result = solve(N, M, A, B, C)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import heapq
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, M: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    heapq.heapify(A)

    for b, c in zip(B, C):
        for i in range(b):
            v = heapq.heappop(A)
            if v < c:
                heapq.heappush(A, c)
            else:
                heapq.heappush(A, v)
                break
    return sum(A)


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(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    result = solve(N, M, A, B, C)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()