Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a, b =  map(int,input().split())
c = A + B
if c < A - B:
	c = A - B
if c < A * B:
	c = A * B
print(c)

Fixed submission:
a, b =  map(int,input().split())
c = a + b
if c < a - b:
	c = a - b
if c < a * b:
	c = a * b
print(c)

Buggy python submission (verdict: Runtime Error):
a, b =  map(int,input().split())
c = A + B
if c < A - B:
	c = A - B
if c < A * B:
	c = A * B:
print(c)

Fixed submission:
a, b =  map(int,input().split())
c = a + b
if c < a - b:
	c = a - b
if c < a * b:
	c = a * b
print(c)

Buggy python submission (verdict: Runtime Error):
a, b =  map(int,input().split())
c = A + B
if c < A - B:
  c = A - B
if c < A * B:
  c = A * B:
print(c)

Fixed submission:
a, b =  map(int,input().split())
c = a + b
if c < a - b:
	c = a - b
if c < a * b:
	c = a * b
print(c)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
word = []
ans = 0
temp = 0
tmplist = []
m = 0
for i in range (0,N):
    temp = 0
    linp =  sorted(input())
    inp = ''.join(linp)
    for j in range (0,m):
        if word[j][0] == inp: 
            ans = ans + word[j][1]
            word [j][1] = word[j][1] + 1
            temp = 1
            break
    if temp == 0:
        tmplist = [inp, 1]
        word.append(tmplist) 
        m = m + 1
print(ans)

 


Fixed submission:
n = int(input())
d = {}
ans = 0


for i in range (0,n):
    temp = 0
    linp =  sorted(input())
    inp = ''.join(linp)
    if inp in d:
        ans = ans + d[inp]
        d[inp] = d[inp] + 1
    else:
        d[inp] = 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
word = []
ans = 0
temp = 0
for i in range (0,N):
    temp = 0
    inp = sorted(input())
    for j in range (0,len(word)):
        for k in range (0,10):
            if word[j][k] != inp[k]:
                temp = 0
                break
            temp = 1
        if temp == 1: 
            ans = ans + word[j][10]
            word [j][10] = word[j][10] + 1
            temp = 1
            break
    if temp == 0:
        inp.append(1)
        word.append(inp)
print(ans)



Fixed submission:
n = int(input())
d = {}
ans = 0


for i in range (0,n):
    temp = 0
    linp =  sorted(input())
    inp = ''.join(linp)
    if inp in d:
        ans = ans + d[inp]
        d[inp] = d[inp] + 1
    else:
        d[inp] = 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
word = []
count = 0
for i in range (0,N):
    word.append(sorted(input()))
    for j in range (0, i):
        if word[j] == word[i]:
            count = count + 1
            
print(count)



Fixed submission:
n = int(input())
d = {}
ans = 0


for i in range (0,n):
    temp = 0
    linp =  sorted(input())
    inp = ''.join(linp)
    if inp in d:
        ans = ans + d[inp]
        d[inp] = d[inp] + 1
    else:
        d[inp] = 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if a = b and b = c:
    print('Yes')
else:
    print('No')

Fixed submission:
a,b,c = map(int,input().split())
if a == b and b == c:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
x = int(input())
ans = 0

for i in range(0,a):
    if x < 500 * i:
        break
    if x == 500 * i:
        ans += 1
        break
    for j in range(0,b):
        if x < 500 * i + 100 * j:
            break
        if x == 500 * i + 100 * j:
            ans += 1
            break
        for k in range(0,c)
            if x < 500 * i + 100 * j + 50 * k:
                break
            if x == 500 * i + 100 * j + 50 * k:
                ans += 1
                break

print(ans)


Fixed submission:
a = int(input())
b = int(input())
c = int(input())
x = int(input())
ans = 0

for i in range(0,a+1):
    if x < 500 * i:
        break
    if x == 500 * i:
        ans += 1
        break
    for j in range(0,b+1):
        if x < 500 * i + 100 * j:
            break
        if x == 500 * i + 100 * j:
            ans += 1
            break
        for k in range(0,c+1):
            if x < 500 * i + 100 * j + 50 * k:
                break
            if x == 500 * i + 100 * j + 50 * k:
                ans += 1
                break

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = map(int,input().split())
ans = 1000
tmp = 0
for i in a:
    tmp = 0
    while i % 2 == 0:
        i = i / 2
        tmp += 1
    if max > tmp:
        ans = tmp
print(ans)
    


Fixed submission:
n = int(input())
a = map(int,input().split())
ans = 1000
tmp = 0
for i in a:
    tmp = 0
    while i % 2 == 0:
        i = i / 2
        tmp += 1
    if ans > tmp:
        ans = tmp
print(ans)
Buggy python submission (verdict: Runtime Error):

N = intinput()
tmp = N
divs = 0

while tmp:
    divs += tmp % 10
    tmp = int(tmp/10)

if N % divs == 0:
    print('Yes')
else:
    print('No')
    


Fixed submission:
#from fractions import gcd
#mod = 10 ** 9 + 7
#N = int(input())
#a = list(map(int,input().split()))
#a,b,c = map(int,input().split())
#ans = [0] * N
#math.ceilで切り上げ
import math
import string

def intinput():
    return int(input())

def listintinput():
    return list(map(int,input().split()))

def splitintinput():
    return map(int,input().split())

N = intinput()
tmp = N
divs = 0

while tmp:
    divs += tmp % 10
    tmp = int(tmp/10)

if N % divs == 0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
x, a, b = map(int,input().split())
if b < a:
    print('delicious')
elif b <= a + x:
    print('safe')
else:
    print('dangerous')

Fixed submission:
x=input()
if x[0] =='9' or x[1] == '9':
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
n, m = map(int,inpout().split())
print((n - 1) + (m * 1))

Fixed submission:
n, m = map(int,input().split())
print((n - 1) * (m - 1))
Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n+1):#各行を縦方向に比較
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')

Fixed submission:
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())
flag = False
for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n):#各行を1行ずつ比較
            if b[k] != a[i+k][j:j+n]:
                break
        else:
            flag = True
if flag:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

print(a)
print(b)

for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n+1):#各行を縦方向に比較
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')


Fixed submission:
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())
flag = False
for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n):#各行を1行ずつ比較
            if b[k] != a[i+k][j:j+n]:
                break
        else:
            flag = True
if flag:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

print(a)
print(b)

for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n+1):#各行を縦方向に比較
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')


Fixed submission:
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())
flag = False
for i in range(m-n+1):#比較開始を縦方向に動かす
    for j in range(m-n+1):#比較開始を横方向へ動かす
        for k in range(n):#各行を1行ずつ比較
            if b[k] != a[i+k][j:j+n]:
                break
        else:
            flag = True
if flag:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

for i in range(0, m-n):#比較開始を縦方向に動かす
    for j in range(0, m-n):#比較開始を横方向へ動かす
        for k in range(0,n):
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')

Fixed submission:
s = input()
n = len(s)
ans = 0
for i in range(n):
    if s[i] == 'A':
        ans -= i
##        print('i')
        break

for j in reversed(range(0, n)):
    if s[j] == 'Z':
        ans += j
        break

print(ans + 1)

Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

for i in range(0, m-n):#比較開始を縦方向に動かす
    for j in range(0, m-n):#比較開始を横方向へ動かす
        for k in range(0,n):
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')

Fixed submission:
s = input()
n = len(s)
ans = 0
for i in range(n):
    if s[i] == 'A':
        ans -= i
##        print('i')
        break

for j in reversed(range(0, n)):
    if s[j] == 'Z':
        ans += j
        break

print(ans + 1)

Buggy python submission (verdict: Runtime Error):
m,n = map(int,input().split())
a = []*m
b = []*n
flag = False
for i in range(m):
    a.append(input())
for j in range(n):
    b.append(input())

for i in range(0, m-n):#比較開始を縦方向に動かす
    for j in range(0, m-n):#比較開始を横方向へ動かす
        for k in range(0,n):
            if b[k] != a[i][j:j+n]:
                break
            flag = True

if flag:
    print('Yes')
else:
    print('No')

Fixed submission:
s = input()
n = len(s)
ans = 0
for i in range(n):
    if s[i] == 'A':
        ans -= i
##        print('i')
        break

for j in reversed(range(0, n)):
    if s[j] == 'Z':
        ans += j
        break

print(ans + 1)
Buggy python submission (verdict: Runtime Error):
N, X, Y = map(int,input().split())
X -= 1
Y -= 1

nallpath = N*(N-1)//2
distance = [0] * nallpath

indx = 0
for j in range(N-1):
    for i in range(j+1,N):
        if i <= X:
            distance[indx] = i - j
        elif j >= Y:
            distance[indx] = i - j
        elif j <= X and Y<= i:
            distance[indx] = i - j - (Y-X-1)
        elif  X <= j < i <= Y:
            distance[indx] = min(i-j, (j-X)+(Y-i)+1)
        elif j <= X and X < i <= Y:
            distance[indx] = (X-j) + min(i-X, Y-i+1)
        else:
            distance[indx] = (i-Y) + min(Y-j, j-X+1)
        indx += 1
for i in range(N-1):print(distance.count(i+1))


Fixed submission:
S = input()
if S[2] == S[3] and S[4] == S[5]:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N, X, Y = map(int,input().split())
X -= 1
Y -= 1

nallpath = N*(N-1)//2
distance = [0] * nallpath

indx = 0
for j in range(N-1):
    for i in range(j+1,N):
        if i <= X:
            distance[indx] = i - j
        elif j >= Y:
            distance[indx] = i - j
        elif j <= X and Y<= i:
            distance[indx] = i - j - (Y-X-1)
        elif  X <= j < i <= Y:
            distance[indx] = min(i-j, (j-X)+(Y-i)+1)
        elif j <= X and X < i <= Y:
            distance[indx] = (X-j) + min(i-X, Y-i+1)
        else:
            distance[indx] = (i-Y) + min(Y-j, j-X+1)
        indx += 1
for i in range(N-1):print(distance.count(i+1))


Fixed submission:
N, X, Y = map(int,input().split())
X -= 1
Y -= 1

npath = [0] * (N-1)

indx = 0
for j in range(N-1):
    for i in range(j+1,N):
        if i <= X:
            distance = i - j
        elif j >= Y:
            distance = i - j
        elif j <= X and Y<= i:
            distance = i - j - (Y-X-1)
        elif  X <= j < i <= Y:
            distance = min(i-j, (j-X)+(Y-i)+1)
        elif j <= X and X < i <= Y:
            distance = (X-j) + min(i-X, Y-i+1)
        else:
            distance = (i-Y) + min(Y-j, j-X+1)
        npath[distance-1] +=1
for i in range(N-1):print(npath[i])

Buggy python submission (verdict: Time Limit Exceeded):
N, X, Y = map(int,input().split())
X -= 1
Y -= 1

nallpath = N*(N-1)//2
distance = [0] * nallpath

indx = 0
for j in range(N-1):
    for i in range(j+1,N):
        if i <= X:
            distance[indx] = i - j
        elif j >= Y:
            distance[indx] = i - j
        elif j <= X and Y<= i:
            distance[indx] = i - j - (Y-X-1)
        elif  X <= j < i <= Y:
            distance[indx] = min(i-j, (j-X)+(Y-i)+1)
        elif j <= X and X < i <= Y:
            distance[indx] = (X-j) + min(i-X, Y-i+1)
        else:
            distance[indx] = (i-Y) + min(Y-j, j-X+1)
        indx += 1
for i in range(N-1):print(distance.count(i+1))

Fixed submission:
N, X, Y = map(int,input().split())
X -= 1
Y -= 1

npath = [0] * (N-1)

indx = 0
for j in range(N-1):
    for i in range(j+1,N):
        if i <= X:
            distance = i - j
        elif j >= Y:
            distance = i - j
        elif j <= X and Y<= i:
            distance = i - j - (Y-X-1)
        elif  X <= j < i <= Y:
            distance = min(i-j, (j-X)+(Y-i)+1)
        elif j <= X and X < i <= Y:
            distance = (X-j) + min(i-X, Y-i+1)
        else:
            distance = (i-Y) + min(Y-j, j-X+1)
        npath[distance-1] +=1
for i in range(N-1):print(npath[i])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
Ai = list(map(int,input().split()))
Aisort = sorted(Ai) + [-1]
Aidic = {}
cnt = 1
for i in range(1,N+1):
    if Aisort[i] != Aisort[i-1]:
        Aidic.update([(str(Aisort[i-1]), cnt)])
        cnt = 1
    else:
        cnt = cnt + 1

for A in Ai:
    cnt = 0
    for i, indx in enumerate(Aidic):
        num = int(Aidic[indx])
        if str(A) == indx:
            cnt = cnt + (num-1)*(num-2)//2
        else:
            cnt = cnt + num*(num-1)//2
    print(cnt)
            

Fixed submission:
N = int(input())
Ai = list(map(int,input().split()))
Aisort = sorted(Ai) + [-1]
Aidic = {}
cnt = 1
for i in range(1,N+1):
    if Aisort[i] != Aisort[i-1]:
        Aidic.update([(str(Aisort[i-1]), cnt)])
        cnt = 1
    else:
        cnt = cnt + 1

cnt = 0
for i, indx in enumerate(Aidic):
    num = int(Aidic[indx])
    cnt = cnt + num*(num-1)//2


for A in Ai:
    num = int(Aidic[str(A)])
    num = cnt - (num-1)
    print(num)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()

flag = 1
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S = line[2] + S
      else:
        S = S + line[2]
    else:
      if line[1] == "1":
        S = S + line[2]
      else:
        S = line[2] + S

if flag == 2: S = S[::-1]
print(S)


Fixed submission:
S = input()

flag = 1
S1 = ""
S2 = ""
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S1 = line[2] + S1
      else:
        S2 = S2 + line[2]
    else:
      if line[1] == "1":
        S2 = S2 + line[2]
      else:
        S1 = line[2] + S1

S = S1 + S + S2
if flag == 2: S = S[::-1]
print(S)

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

N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    S = S[::-1]
  else:
    if line[1] == "1":
      S = line[2] + S
    else:
      S = S + line[2]

print(S)


Fixed submission:
S = input()

flag = 1
S1 = ""
S2 = ""
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S1 = line[2] + S1
      else:
        S2 = S2 + line[2]
    else:
      if line[1] == "1":
        S2 = S2 + line[2]
      else:
        S1 = line[2] + S1

S = S1 + S + S2
if flag == 2: S = S[::-1]
print(S)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
pn = list(map(int,input().split()))
num = 0
for i in range(N-K+1): num = max(num,sum(pn[i:i+K]))
print(0.5*K + num*0.5)

Fixed submission:
N, K = map(int,input().split())
pn = list(map(int,input().split()))
num = [0]*(N-K+1)
num[0] = sum(pn[0:K])
for n in range(1,N-K+1):
  num[n] = num[n-1] - pn[n-1] + pn[n+K-1]
print(0.5*(K + max(num)))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
pn = list(map(int,input().split()))
num = 0
for i in range(N-K+1): num = max(num,sum(pn[i:i+K]))
print(0.5*K + num*0.5)

Fixed submission:
N, K = map(int,input().split())
pn = list(map(int,input().split()))
num = [0]*(N-K+1)
num[0] = sum(pn[0:K])
for n in range(1,N-K+1):
  num[n] = num[n-1] - pn[n-1] + pn[n+K-1]
print(0.5*(K + max(num)))
Buggy python submission (verdict: Time Limit Exceeded):
import queue

N, Q = map(int,input().split())
counter = [0]*N
tree = [[] for _ in range(N)]
parent = [-1]*N
for _ in range(N-1):
  a, b = map(int,input().split())
  tree[a-1].append(b-1)
  tree[b-1].append(a-1)

  
for _ in range(Q):
  p, x = map(int,input().split())
  counter[p-1] += x

q = queue.Queue()
q.put(0)

while not q.empty():
  index = q.get()
  pindex = parent[index]
  for i in tree[index]:
    if i == pindex: continue
    counter[i] += counter[index]
    parent[i] = index
    q.put(i)
    
print(*counter)


Fixed submission:
from collections import deque

N, Q = map(int,input().split())
counter = [0]*N
tree = [[] for _ in range(N)]
parent = [-1]*N
for _ in range(N-1):
  a, b = map(int,input().split())
  tree[a-1].append(b-1)
  tree[b-1].append(a-1)

  
for _ in range(Q):
  p, x = map(int,input().split())
  counter[p-1] += x

q  = deque()
q.append(0)

while q:
  index = q.pop()
  pindex = parent[index]
  for i in tree[index]:
    if i == pindex: continue
    counter[i] += counter[index]
    parent[i] = index
    q.append(i)
    
print(*counter)
Buggy python submission (verdict: Runtime Error):
A, B, C = map(int,input())
print(max(0, C-(A-B)))

Fixed submission:
A, B, C = map(int,input().split())
print(max(0, C-(A-B)))
Buggy python submission (verdict: Runtime Error):
S = input()
num = [0]*len(S)
for i in range(len(S)):
  if S[i] == "R":
    for j in range(i+1,len(S)):
      if S[j] =="L":
        Lindex = j
        break
    if (Lindex-i)%2==1:
      num[Lindex-1] += 1
    else:
      num[Lindex] += 1
  else:
    for j in range(i-1,0,-1):
      if S[j] =="R":
        Rindex = j
        break
    if (Rindex-i)%2==1:
      num[Rindex+1] += 1
    else:
      num[Rindex] += 1

print(*num)

Fixed submission:
S = input()

Lindex = S.index("L")
Rindex = 0

num = [0]*len(S)

for i in range(len(S)):
  if S[i] == "R":
    Rindex = i
    if Lindex < i: Lindex = i+ S[i:].index("L")
    if (Lindex-i)%2==1:
      num[Lindex-1] += 1
    else:
      num[Lindex] += 1
  else:
    if (Rindex-i)%2==1:
      num[Rindex+1] += 1
    else:
      num[Rindex] += 1
print(*num)

Buggy python submission (verdict: Runtime Error):
S = input()
num = [0]*len(S)
for i in range(len(S)):
  if S[i] == "R":
    for j in range(i+1,len(S)):
      if S[j] =="L":
        Lindex = j
        break
    if (Lindex-i)%2==1:
      num[Lindex-1] += 1
    else:
      num[Lindex] += 1
  else:
    for j in range(i-1,0,-1):
      if S[j] =="R":
        Rindex = j
        break
    if (Rindex-i)%2==1:
      num[Rindex+1] += 1
    else:
      num[Rindex] += 1

print(*num)

Fixed submission:
S = input()

Lindex = S.index("L")
Rindex = 0

num = [0]*len(S)

for i in range(len(S)):
  if S[i] == "R":
    Rindex = i
    if Lindex < i: Lindex = i+ S[i:].index("L")
    if (Lindex-i)%2==1:
      num[Lindex-1] += 1
    else:
      num[Lindex] += 1
  else:
    if (Rindex-i)%2==1:
      num[Rindex+1] += 1
    else:
      num[Rindex] += 1
print(*num)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
An = list(map(int,input().split()))
for _ in range(M):
  B, C = map(int,input().split())
  An.extend([C]*B)
An.sort(reverse=True)
print(sum(An[0:N]))

Fixed submission:
N, M = map(int,input().split())
An = list(map(lambda x:[int(x),1],input().split()))
D = [[0]*2 for _ in range(M)]
for i in range(M):
  B, C = map(int,input().split())
  D[i] = [C,B]
An.extend(D)
An.sort(reverse=True)
num = 0
cnt = 0
for i in range(len(An)):
  if cnt+An[i][1] >= N:
    num += An[i][0]*(N-cnt)
    print(num)
    break
  else:
    cnt += An[i][1]
    num += An[i][0]*An[i][1]

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
An = sorted(list(map(int,input().split())), reverse=True)

for _ in range(M):
  B, C = map(int,input().split())
  for b in range(N-1,N-1-B,-1):
    if An[b] < C:
      An[b] = C
    else:
      break
  else:
    An.sort(reverse=True)
print(sum(An))

Fixed submission:
N, M = map(int,input().split())
An = list(map(lambda x:[int(x),1],input().split()))
D = [[0]*2 for _ in range(M)]
for i in range(M):
  B, C = map(int,input().split())
  D[i] = [C,B]
An.extend(D)
An.sort(reverse=True)
num = 0
cnt = 0
for i in range(len(An)):
  if cnt+An[i][1] >= N:
    num += An[i][0]*(N-cnt)
    print(num)
    break
  else:
    cnt += An[i][1]
    num += An[i][0]*An[i][1]
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
tree =   [[] for _ in range(N)]
parent = [-1] * N
color = [-1] * N
color[0] = 0
for n in range(N-1):
  u, v, w = map(int,input().split())
  tree[u-1].append([v-1,w])
  tree[v-1].append([u-1,w])

q = deque()
q.append(0)

while q:
  index = q.pop()
  for i in range(len(tree[index])):
    target = tree[index][i][0]
    if parent[target] == index: continue
    parent[target] = index
    if tree[index][i][1]%2 == 0:
      color[target] = color[index]
    else:
      color[target] = (color[index] + 1)%2
    q.append(target)
print(*color, sep="\n")

Fixed submission:
from collections import deque

N = int(input())
tree =   [[] for _ in range(N)]
color = [-1] * N
color[0] = 0
for n in range(N-1):
  u, v, w = map(int,input().split())
  tree[u-1].append([v-1,w])
  tree[v-1].append([u-1,w])

q = deque()
q.append((0,-1))

while q:
  index, parent = q.pop()
  for target, d in tree[index]:
    if target == parent: continue
    if d%2 == 0:
      color[target] = color[index]
    else:
      color[target] = (color[index] + 1)%2
    q.append((target,index))
print(*color, sep="\n")

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

N = int(input())
tree =   [[] for _ in range(N)]
parent = [-1] * N
color = [-1] * N
color[0] = 0
for n in range(N-1):
  u, v, w = map(int,input().split())
  tree[u-1].append([v-1,w])
  tree[v-1].append([u-1,w])

q = deque()
q.append(0)

while q:
  index = q.pop()
  for i in range(len(tree[index])):
    target = tree[index][i][0]
    if parent[target] == index: continue
    parent[target] = index
    if tree[index][i][1]%2 == 0:
      color[target] = color[index]
    else:
      color[target] = (color[index] + 1)%2
    q.append(target)
print(*color, sep="\n")

Fixed submission:
from collections import deque

N = int(input())
tree =   [[] for _ in range(N)]
color = [-1] * N
color[0] = 0
for n in range(N-1):
  u, v, w = map(int,input().split())
  tree[u-1].append([v-1,w])
  tree[v-1].append([u-1,w])

q = deque()
q.append((0,-1))

while q:
  index, parent = q.pop()
  for target, d in tree[index]:
    if target == parent: continue
    if d%2 == 0:
      color[target] = color[index]
    else:
      color[target] = (color[index] + 1)%2
    q.append((target,index))
print(*color, sep="\n")