Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
dir=True
ans=s
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      ans=c+ans
    else:
      ans=ans+c
def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(ans)
else:
  print(reverse(ans))


Fixed submission:
s=input()
q=int(input())
dir=True
ans=s
right=""
left=""
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      left=c+left
    else:
      right=right+c

def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(left+ans+right)
else:
  t=reverse(right)+reverse(ans)+reverse(left)
  print(t)

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
dir=True
ans=s
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      ans=c+ans
    else:
      ans=ans+c
      
if dir:
  print(ans)
else:
  print(ans[::-1])


Fixed submission:
s=input()
q=int(input())
dir=True
ans=s
right=""
left=""
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      left=c+left
    else:
      right=right+c

def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(left+ans+right)
else:
  t=reverse(right)+reverse(ans)+reverse(left)
  print(t)

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
q=int(input())
dir=True
ans=s
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()
    ff = False
    if dir:
      if f == "1":
        ans=c+ans
      else:
        ans=ans+c
    else:
      if f == "1":
        ans=ans+c
      else:
        ans=c+ans

if dir:
  print(ans)
else:
  print(ans[::-1])

Fixed submission:
s=input()
q=int(input())
dir=True
ans=s
right=""
left=""
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      left=c+left
    else:
      right=right+c

def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(left+ans+right)
else:
  t=reverse(right)+reverse(ans)+reverse(left)
  print(t)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = sys.maxsize * (-1)
r = sys.maxsize

while l+1 < r:
  x = (l+r)//2
  def isOk():
    tot = 0
    for i in range(n):
      if a[i] < 0:
        l = -1
        r = n
        while (l+1<r):
          c = (l+r)//2;
          if (a[c] * a[i] < x):
            r = c
          else:
            l = c
        tot += n-r
      else:
        l = -1
        r = n
        while (l+1<r):
          c = (l+r)//2
          if (a[c] * a[i] < x):
            l = c
          else:
            r = c
        tot += r
      if (a[i]*a[i] < x):
        tot -= 1
    tot /= 2
    return tot < k
  if (isOk()):
    l = x
  else:
    r = x;
print(l)

Fixed submission:
import sys
n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l = sys.maxsize * (-1)
r = sys.maxsize

while l+1 < r:
  x = (l+r)//2
  def isOk():
    tot = 0
    for i in range(n):
      if a[i] < 0:
        l = -1
        r = n
        while (l+1<r):
          c = (l+r)//2;
          if (a[c] * a[i] < x):
            r = c
          else:
            l = c
        tot += n-r
      else:
        l = -1
        r = n
        while (l+1<r):
          c = (l+r)//2
          if (a[c] * a[i] < x):
            l = c
          else:
            r = c
        tot += r
      if (a[i]*a[i] < x):
        tot -= 1
    tot /= 2
    return tot < k
  if (isOk()):
    l = x
  else:
    r = x;
print(l)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))

def kitai(n):
  return (1 + n) / 2

ans = 0
for i in range(n - k + 1):
  temp = 0
  for j in range(k):
    temp += kitai(p[i + j])
  ans = max(ans, temp)
  
print(ans)

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))

s = [0]

def kitai(n):
  return (1 + n) / 2

for i in range(n):
  s.append(kitai(p[i]) + s[-1])

ans = 0
for i in range(n - k + 1):
  ans = max(ans, s[i + k] - s[i])
print(ans)
Buggy python submission (verdict: Runtime Error):
n = input()
k = int(input())
ln = len(n) + 1

dp=[[[0]* (ln) for _ in range(2)] for i in range(ln)]
dp[0][0][0] = 1
for i in range(len(n)):
  for smaller in range(2):
    x = int(n[i])
    t = 9 if smaller else x
    for j in range(t+1):
      for knum in range(ln-1):
        if j == 0:
          dp[i+1][int(smaller or j < x)][knum] += dp[i][smaller][knum]
        else:
          dp[i+1][int(smaller or j < x)][knum+1] += dp[i][smaller][knum]
print(dp[-1][0][k]+dp[-1][1][k])

Fixed submission:
n = input()
k = int(input())
ln = len(n) + 1

dp=[[[0]* (ln) for _ in range(2)] for i in range(ln)]
dp[0][0][0] = 1
for i in range(len(n)):
  for smaller in range(2):
    x = int(n[i])
    t = 9 if smaller else x
    for j in range(t+1):
      for knum in range(ln-1):
        if j == 0:
          dp[i+1][int(smaller or j < x)][knum] += dp[i][smaller][knum]
        else:
          dp[i+1][int(smaller or j < x)][knum+1] += dp[i][smaller][knum]
try:
  print(dp[-1][0][k] + dp[-1][1][k])
except:
  print(0)
Buggy python submission (verdict: Time Limit Exceeded):
h, ｎ = map(int, input().split())
dp = [-1] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  if dp[i] == -1: continue
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    if dp[ht] != -1:
      dp[ht] = min(dp[ht], dp[i] + j[1])
    else:
      dp[ht] = dp[i] + j[1]

print(dp[h])


Fixed submission:
h, ｎ = map(int, input().split())
dp = [-1] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  if dp[i] == -1: continue
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    if dp[ht] != -1:
      dp[ht] = min(dp[ht], dp[i] + j[1])
    else:
      dp[ht] = dp[i] + j[1]

print(dp[h])

Buggy python submission (verdict: Time Limit Exceeded):
h, ｎ = map(int, input().split())
dp = [10**10] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  t = 10 ** 10
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    dp[ht] = min(dp[ht], dp[i] + j[1]) 

print(dp[h])


Fixed submission:
h, ｎ = map(int, input().split())
dp = [-1] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  if dp[i] == -1: continue
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    if dp[ht] != -1:
      dp[ht] = min(dp[ht], dp[i] + j[1])
    else:
      dp[ht] = dp[i] + j[1]

print(dp[h])
Buggy python submission (verdict: Runtime Error):
from functools import reduce
import math
def lcm_base(x, y):
  return (x * y) // math.gcd(x, y)

def lcm(*numbers):
  return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
  return reduce(lcm_base, numbers, 1)

n,m = map(int, input().split())
a = list(map(int, input().split()))
a = [i//2 for i in list(set(a))]

cnt_common = -1
for i in a:
  cnt = 0
  while i % 2 == 0:
    i = i//2
    cnt += 1
  if cnt_common == -1:
    cnt_common = cnt
    continue
  if cnt_common != cnt:
    nothing = True
    break

b=lcm_list(a)
if nothing:
  print(0)
else:
  ans = (m-b)//(2*b) + 1
  print(max(ans,0))

Fixed submission:
from functools import reduce
import math
def lcm_base(x, y):
  return (x * y) // math.gcd(x, y)

def lcm(*numbers):
  return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
  return reduce(lcm_base, numbers, 1)

n,m = map(int, input().split())
a = list(map(int, input().split()))
a = [i//2 for i in list(set(a))]
nothing = False
cnt_common = -1
for i in a:
  cnt = 0
  while i % 2 == 0:
    i = i//2
    cnt += 1
  if cnt_common == -1:
    cnt_common = cnt
    continue
  if cnt_common != cnt:
    nothing = True
    break

b=lcm_list(a)
if nothing:
  print(0)
else:
  ans = (m-b)//(2*b) + 1
  print(max(ans,0))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))

pos=0
for x in range(1,n+1):
  if a:
    t = a.index(x) if x in a else -1
    if t == -1:
      if pos==0:
        print(-1)
      else:
        print(n-pos)
      break
    else:
      pos+=1
      a=a[t+1:]
  else:
    continue
else:
  print(n-pos)

Fixed submission:
n=int(input())
a=map(int,input().split())

temp=1
res=[]
for x in a:
  if x == temp:
    temp+=1

if temp==1:
  print(-1)
else:
  print(n+1-temp)
Buggy python submission (verdict: Time Limit Exceeded):
n=input()
s=input()

cnt=0
for i in range(0,1000):
  pos=0
  t="{0:03d}".format(i)
  for j in range(int(n)):
    if s[j] == t[pos]:
      pos+=1
    if pos == 3:
      cnt+=1
      break

print(cnt)

Fixed submission:
n=input()
s=input()

cnt=0
for i in range(0,1000):
  pos=0
  t="{0:03d}".format(i)
  for j in range(int(n)):
    if s[j] == t[pos]:
      pos+=1
    if pos == 3:
      cnt+=1
      break

print(cnt)

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

cnt=0
for i in range(0,1000):
  pos=-1
  t="{0:03d}".format(i)
  for j in t:
    pos+=1
    while pos < len(s) and j != s[pos]:
      pos+=1
    
  if pos < len(s):
    cnt+=1

print(cnt)


Fixed submission:
n=input()
s=input()

cnt=0
for i in range(0,1000):
  pos=0
  t="{0:03d}".format(i)
  for j in range(int(n)):
    if s[j] == t[pos]:
      pos+=1
    if pos == 3:
      cnt+=1
      break

print(cnt)

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

import re

cnt=0
for i in range(0,1000):
  t="{0:03d}".format(i)
  reg=r'.*'+re.escape(t[0])+r'.*'+re.escape(t[1])+r'.*'+re.escape(t[2])
  if re.search(reg,s):
    cnt+=1

print(cnt)
  

Fixed submission:
n=input()
s=input()

cnt=0
for i in range(0,1000):
  pos=0
  t="{0:03d}".format(i)
  for j in range(int(n)):
    if s[j] == t[pos]:
      pos+=1
    if pos == 3:
      cnt+=1
      break

print(cnt)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input())
 
if n <= 9 and m <= 9:
  print(n * m)
else:
  print(-1)

Fixed submission:
n, m = map(int, input().split())
 
if n <= 9 and m <= 9:
  print(n * m)
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input())

if n <= 9 and m <- 9:
  print(n * m)


Fixed submission:
n, m = map(int, input().split())
 
if n <= 9 and m <= 9:
  print(n * m)
else:
  print(-1)
Buggy python submission (verdict: Runtime Error):
import sys

def wf(d):
  for i in range(n):
    for j in range(n):#start
      for k in range(n):#end
        d[j][k]=min(d[j][k], d[j][i] + d[i][k])
  return d

n,m,l = map(int, input().split())

d=[[sys.maxsize]*(n) for _ in range(n)]
e=[[sys.maxsize]*(n) for _ in range(n)]
for i in range(n):
  d[i][i] = 0
  e[i][i] = 0

for _ in range(m):
  a,b,c = map(int, input().split())
  d[a-1][b-1]=c
  d[b-1][a-1]=c

d=wf(d)
for i in range(n):
  for j in range(n):
    if d[i][j] <= l:
      e[i][j] = 1

e=wf(e)

q=int(input())
for _ in range(q):
  s,t = map(int, input().split())
  if e[s-1][t-1] == sys.maxsize:
    print(-1)
  else:
    print(e[s-1][t-1]-1)


Fixed submission:
n,m  = map(int, input().split())

if 2 * m <= n:
  print(n - 2 * m)
else:
  print(0)

Buggy python submission (verdict: Runtime Error):
n  = int(input())

if n % 2 == 0:
  print((n // 2) / n)
else:
  print((int(n / 2) + 1)  / n)

Fixed submission:
n,m  = map(int, input().split())

if 2 * m <= n:
  print(n - 2 * m)
else:
  print(0)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

ans=""
for i in range(1, n+1):
  ans = ans + str(a.index(i)+1) + " "
  
print(ans.strip())


Fixed submission:
n = int(input())
ans=[0] * (n + 1)

a=list(map(int, input().split()))

for i in range(1, n+1):
  chakujunn = a[i-1]
  ans[chakujunn] = i
  
print(" ".join(map(str,ans[1:])))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

ans=[]
for i in range(1, n+1):
  ans.append(str(a.index(i)+1))
  
print(" ".join(ans))


Fixed submission:
n = int(input())
ans=[0] * (n + 1)

a=list(map(int, input().split()))

for i in range(1, n+1):
  chakujunn = a[i-1]
  ans[chakujunn] = i
  
print(" ".join(map(str,ans[1:])))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
d = defaultdict(int)

n,m = map(int, input().split())
d["0"*n] = 0
for i in range(m):
  a,b = map(int, input().split())
  c = list(map(int, input().split()))
  temp=[0]*n
  for i in c:
    temp[i-1] = 1
  t = "".join(str(tt) for tt in temp)
  ks = list(d.keys())
  for key in ks:
    if key != "0"*n and d[key] == 0:
      continue
    t2=""
    for b1, b2 in zip(key, t):
      if b1 == "1" or b2 == "1":
        t2 += "1"
      else:
        t2 += "0"
    if d[t2] == 0:
      d[t2] = d[key] + a
    else:
      d[t2] = min(d[key] + a, d[t2])

ans=d["1"*n]
if ans == 0:
  print(-1)
else:
  print(ans)

Fixed submission:
n,m = map(int, input().split())
dp = [0]*(2**n + 1)
for i in range(m):
  a,b = map(int, input().split())
  c = list(map(int, input().split()))
  temp=0
  for i in c:
    temp += 2 ** (i-1)
  for i in range(2**n):
    if i != 0 and dp[i] == 0:
      continue
    t = i | temp
    if dp[t] == 0:
      dp[t] = dp[i] + a
    else:
      dp[t] = min(dp[i] + a, dp[t])

ans = dp[2**n - 1]
if ans == 0:
  print(-1)
else:
  print(ans)
Buggy python submission (verdict: Runtime Error):
n,q = map(int, input().split())
graph1 = [[] for _ in range(n+1)]
graph2 = [[] for _ in range(n+1)]
ans=[0]*(n+1)
reached=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph1[a].append(b)
    graph2[b].append(a)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for g in graph1[i], graph2[i]:
    for j in g:
      if reached[j] == 1:
        continue
      ans[j] += ans[i]
      dfs(j)

if __name__ == '__main__':
  main()

Fixed submission:
import sys
sys.setrecursionlimit(200000000)
n,q = map(int, input().split())
graph1 = [[] for _ in range(n+1)]
graph2 = [[] for _ in range(n+1)]
ans=[0]*(n+1)
reached=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph1[a].append(b)
    graph2[b].append(a)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for g in graph1[i], graph2[i]:
    for j in g:
      if reached[j] == 1:
        continue
      ans[j] += ans[i]
      dfs(j)

if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
n,q = map(int, input().split())
graph = [[] for _ in range(n+1)]
ans=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph[a].append(b)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  for j in graph[i]:
    ans[j] += ans[i]
    dfs(j)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(200000000)
n,q = map(int, input().split())
graph1 = [[] for _ in range(n+1)]
graph2 = [[] for _ in range(n+1)]
ans=[0]*(n+1)
reached=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph1[a].append(b)
    graph2[b].append(a)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for g in graph1[i], graph2[i]:
    for j in g:
      if reached[j] == 1:
        continue
      ans[j] += ans[i]
      dfs(j)

if __name__ == '__main__':
  main()
Buggy python submission (verdict: Time Limit Exceeded):
import string
import collections

n = int(input())
ans=[]
for i in range(n):
  s=input()
  c = collections.Counter(s)
  ans.append("".join(map(str,[c[i] for i in string.ascii_lowercase])))

c2 = collections.Counter(ans)
cnt=0
for k, v in c2.items():
  cnt+=(v-1)*v//2

print(cnt)

Fixed submission:
import string
import collections

n = int(input())
ans=[]
for i in range(n):
  s=input()
  c = collections.Counter(s)
  ans.append("".join(map(str,[c[i] for i in string.ascii_lowercase])))

c2 = collections.Counter(ans)
cnt=0
for k, v in c2.items():
  cnt+=(v-1)*v//2

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [0]+list(map(int, input().split()))

box=[]
for i in range(n,0,-1):
  t=0
  for j in box:
    if j%i == 0:
      t+=1
  if t%2 != a[i]:
    box.append(i)
else:
  print(len(box))
  box=[str(i) for i in box]
  print(" ".join(box))
#print(-1)

Fixed submission:
n = int(input())
a = [0]+list(map(int, input().split()))
boxi=[0]*(n+1)
box=[]
for i in range(n,0,-1):
  t=0
  for j in range(2,n//i+1):
    if boxi[j*i] == 1:
      t+=1
  if t%2 != a[i]:
    box.append(i)
    boxi[i]=1
else:
  print(len(box))
  box=[str(i) for i in box]
  print(" ".join(box))
#print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [0]+list(map(int, input().split()))

box=[]
for i in range(n,0,-1):
  t=0
  for j in box:
    if j%i == 0:
      t+=1
  if t%2 != a[i]:
    box.append(i)
else:
  print(len(box))
  box=[str(i) for i in box]
  print(" ".join(box))
#print(-1)

Fixed submission:
n = int(input())
a = [0]+list(map(int, input().split()))
boxi=[0]*(n+1)
box=[]
for i in range(n,0,-1):
  t=0
  for j in range(2,n//i+1):
    if boxi[j*i] == 1:
      t+=1
  if t%2 != a[i]:
    box.append(i)
    boxi[i]=1
else:
  print(len(box))
  box=[str(i) for i in box]
  print(" ".join(box))
#print(-1)
Buggy python submission (verdict: Runtime Error):
n, x = map(int, input().split())
l = list(map(int, input().split()))

d=[0]
for i in range(1, n+1):
  d.append(d[-1]+l[i-1])
  
print(len([i for i in d if i <= x]))

Fixed submission:
x, a = map(int, input().split())

if x >= a:
  print(10)
else:
  print(0)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

ans=set()
l, r = map(int, input().split())
for j in range(l,r+1):
    ans.add(j)

for i in range(m-1):
  l, r = map(int, input().split())
  temp=set()
  lm=min(list(ans))
  rm=min(list(ans))
  for j in range(min(l,lm), min(r,rm)+1):
    temp.add(j)
  ans=temp & ans

print(len(ans))

Fixed submission:
n, m = map(int, input().split())

ans=set()
lm=0
rm=n
for i in range(m):
  l, r = map(int, input().split())
  lm=max(l, lm)
  rm=min(r, rm)
if rm - lm < 0:
  print(0)
else:
  print(rm - lm + 1)

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

ans=set()
l, r = map(int, input().split())
for j in range(l,r+1):
    ans.add(j)

for i in range(m-1):
  l, r = map(int, input().split())
  temp=set()
  for j in range(l,r+1):
    temp.add(j)
  ans=temp & ans

print(len(ans))

Fixed submission:
n, m = map(int, input().split())

ans=set()
lm=0
rm=n
for i in range(m):
  l, r = map(int, input().split())
  lm=max(l, lm)
  rm=min(r, rm)
if rm - lm < 0:
  print(0)
else:
  print(rm - lm + 1)
Buggy python submission (verdict: Runtime Error):
def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for dic in uv[i]:
    to = dic["to"]
    w = dic["weight"]
    if w % 2 == 0:
      color[to] = color[i]
    else:
      color[to] = int(not color[i])
    dfs(to)

n = int(input())
color = [0]*(n+1)
reached = [0]*(n+1)
uv = [[] * (n+1) for i in range(n+1)]
for i in range(n-1):
  u,v,w = map(int,input().split())
  uv[u].append({"to":v, "weight":w})
  uv[v].append({"to":u, "weight":w})
dfs(1)
for i in color[1:]:
  print(i)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for dic in uv[i]:
    to = dic["to"]
    w = dic["weight"]
    if w % 2 == 0:
      color[to] = color[i]
    else:
      color[to] = int(not color[i])
    dfs(to)

n = int(input())
color = [0]*(n+1)
reached = [0]*(n+1)
uv = [[] * (n+1) for i in range(n+1)]
for i in range(n-1):
  u,v,w = map(int,input().split())
  uv[u].append({"to":v, "weight":w})
  uv[v].append({"to":u, "weight":w})
dfs(1)
for i in color[1:]:
  print(i)
Buggy python submission (verdict: Runtime Error):
from collections import deque
n,m = map(int, input().split())

rel = [[] for _ in range(n)]
for i in range(m):
  x,y,z = map(int, input().split())
  rel[x-1].append(y-1)
  rel[y-1].append(x-1)

ans = [-1]*n
def hoge(parent, pos):
  for i in rel[pos]:
    if ans[i] >= 0:
      continue
    ans[i] = pos
    hoge(pos, i)
  return

cnt = 0
for i in range(n):
  if ans[i] < 0:
    hoge(0, i)
    cnt += 1

print(cnt)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
n,m = map(int, input().split())

rel = [[] for _ in range(n)]
for i in range(m):
  x,y,z = map(int, input().split())
  rel[x-1].append(y-1)
  rel[y-1].append(x-1)

ans = [-1]*n
def hoge(parent, pos):
  for i in rel[pos]:
    if ans[i] >= 0:
      continue
    ans[i] = pos
    hoge(pos, i)
  return

cnt = 0
for i in range(n):
  if ans[i] < 0:
    hoge(0, i)
    cnt += 1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
gl=[0]
gr=[0]
for i in range(n):
  gl.append(gcd(a[i], gl[-1]))
  gr.insert(0,gcd(a[n-i-1], gr[0]))
ans = 0
for i in range(n):
  ans = max(ans, gcd(gl[i],gr[i+1]))
  
print(ans)

Fixed submission:
from fractions import gcd
from functools import reduce

def gcd_list(numbers):
  return reduce(gcd, numbers)

n = int(input())
a = list(map(int, input().split()))
gl=[0]
gr=[0]
for i in range(n):
  gl.append(gcd(a[i], gl[-1]))
  gr.append(gcd(a[n-i-1], gr[-1]))
gr=gr[::-1]
ans = 0
for i in range(n):
  ans = max(ans, gcd(gl[i],gr[i+1]))
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)

n = int(input())
a = list(map(int, input().split()))

ans = 0
for i in range(len(a)):
  ans = max(ans, gcd_list(a[0:i]+a[i + 1:]))
  
print(ans)

Fixed submission:
from fractions import gcd
from functools import reduce

def gcd_list(numbers):
  return reduce(gcd, numbers)

n = int(input())
a = list(map(int, input().split()))
gl=[0]
gr=[0]
for i in range(n):
  gl.append(gcd(a[i], gl[-1]))
  gr.append(gcd(a[n-i-1], gr[-1]))
gr=gr[::-1]
ans = 0
for i in range(n):
  ans = max(ans, gcd(gl[i],gr[i+1]))
  
print(ans)