Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
turn = 0
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    turn += 1
  elif s[0] == "2": # 足す
    if (s[1] == "1" and turn % 2 == 0) or (s[1] == "2" and turn % 2 == 1):
      S = s[2] + S
    elif (s[1] == "2" and turn % 2 == 0) or (s[1] == "1" and turn % 2 == 1):
      S = S + s[2]
if turn % 2 == 0:
  print(S)
else:
  print(S[::-1])

Fixed submission:
S = input()
Q = int(input())
turn = 0
head = ""
tail = ""
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    turn += 1
  elif s[0] == "2": # 足す
    if (s[1] == "1" and turn % 2 == 0) or (s[1] == "2" and turn % 2 == 1):
      head = s[2] + head
    elif (s[1] == "2" and turn % 2 == 0) or (s[1] == "1" and turn % 2 == 1):
      tail = tail + s[2]
if turn % 2 == 0:
  print(head + S + tail)
else:
  print((head + S + tail)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    S = S[::-1]
  elif s[0] == "2":
    if s[1] == "1": # 先頭
      S = s[2] + S
    elif s[1] == "2": # 末尾
      S = S + s[2]
print(S)

Fixed submission:
S = input()
Q = int(input())
turn = 0
head = ""
tail = ""
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    turn += 1
  elif s[0] == "2": # 足す
    if (s[1] == "1" and turn % 2 == 0) or (s[1] == "2" and turn % 2 == 1):
      head = s[2] + head
    elif (s[1] == "2" and turn % 2 == 0) or (s[1] == "1" and turn % 2 == 1):
      tail = tail + s[2]
if turn % 2 == 0:
  print(head + S + tail)
else:
  print((head + S + tail)[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
cnt = 0
for i in range(1,N+1):
  for j in range(1,N+1):
    if str(i)[0:1:] == str(j)[-1::] and str(i)[-1::] == str(j)[0:1:]:
      cnt += 1
print(cnt)

Fixed submission:
N = int(input())
cnt = [[0]*10 for _ in range(10)]
for i in range(1, N+1):
  head = int(str(i)[0])
  tail = int(str(i)[-1])
  cnt[head][tail] += 1
ans = 0
for i in range(1, 10):
  for j in range(1, 10):
    ans += cnt[i][j]*cnt[j][i]
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
S, T = [0]*N, [0]*N
for i in range(N):
  S[i], T[i] = input().split()
X = input()

print(sum(T[S.index(X)+1:]))

Fixed submission:
N = int(input())
s, t = [0]*N, [0]*N
for i in range(N):
  s[i], t[i] = input().split()
X = input()

ans = 0
flag = False
for i in range(N):
  if flag:
    ans += int(t[i])
  if s[i] == X:
    flag = True

print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
s, t = [0]*N, [0]*N
for i in range(N):
  s[i], t[i] = map(int, input().split())
X = input()

ans = 0
flag = False
for i in range(N):
  if flag:
    ans += t[i]
  if s[i] == X:
    flag = True

print(ans)

Fixed submission:
N = int(input())
s, t = [0]*N, [0]*N
for i in range(N):
  s[i], t[i] = input().split()
X = input()

ans = 0
flag = False
for i in range(N):
  if flag:
    ans += int(t[i])
  if s[i] == X:
    flag = True

print(ans)

Buggy python submission (verdict: Runtime Error):
N = input()
s, t = [0]*N, [0]*N
for i in range(N):
  s[i], t[i] = map(int, input().split())
X = input()

ans = 0
flag = False
for i in range(N):
  if flag:
    ans += t[i]
  if s[i] == X:
    flag = True

print(ans)

Fixed submission:
N = int(input())
s, t = [0]*N, [0]*N
for i in range(N):
  s[i], t[i] = input().split()
X = input()

ans = 0
flag = False
for i in range(N):
  if flag:
    ans += int(t[i])
  if s[i] == X:
    flag = True

print(ans)
Buggy python submission (verdict: Runtime Error):
K, X = map(int, input().split())
if 500*K >= X:
  print("Yes")
elif:
  print("No")

Fixed submission:
K, X = map(int, input().split())
if 500*K >= X:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))

i = 1
while i in a:
  a[0:a.index(i)+1] = []
  i += 1

ans = N - i + 1
if i == 1:
  ans = -1
print(ans)

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

ans, nxt = 0, 1

for i in range(N):
  if a[i] == nxt:
    nxt += 1
  else:
    ans += 1

if ans == N:
  ans = -1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(sum(A)/2 - S[i+1]) < diff:
    diff = abs(sum(A)/2 - S[i+1])
    idx = i+1
lsum = S[idx]
rsum = S[-1]-S[idx]
print(abs(lsum-rsum))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
c = sum(A)/2

S = [0] * (N+1)
S[0] = 0
for i in range(N):
  S[i+1] = A[i] + S[i]

m = float("INF")
j = -1
for i in range(1, N+1):
  if abs(c - S[i]) < m:
    m = abs(c - S[i])
    j = i

print(abs((S[N] - S[j]) - S[j]))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
S = [0]*(N+1)
S[0] = 0
diff = float("INF")
idx = -1
for i in range(N):
  S[i+1] = S[i] + A[i]
  if abs(sum(A)/2 - S[i+1]) < diff:
    diff = sum(A)/2 - S[i+1]
    idx = i+1
lsum = S[idx]
rsum = S[-1]-S[idx]
print(abs(lsum-rsum))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
c = sum(A)/2

S = [0] * (N+1)
S[0] = 0
for i in range(N):
  S[i+1] = A[i] + S[i]

m = float("INF")
j = -1
for i in range(1, N+1):
  if abs(c - S[i]) < m:
    m = abs(c - S[i])
    j = i

print(abs((S[N] - S[j]) - S[j]))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
LS = [0]*N
RS = [0]*N
ans = float("INF")
for i in range(N):
  LS[i] = sum(A[:i+1])
  RS[i] = sum(A[i+1:])
  ans = min(ans, abs(RS[i] - LS[i]))
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
c = sum(A)/2

S = [0] * (N+1)
S[0] = 0
for i in range(N):
  S[i+1] = A[i] + S[i]

m = float("INF")
j = -1
for i in range(1, N+1):
  if abs(c - S[i]) < m:
    m = abs(c - S[i])
    j = i

print(abs((S[N] - S[j]) - S[j]))
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())
mod = pow(10, 9) + 7

m = (2*Y - X)/3
n = (2*X - Y)/3

if m.is_integer() == False or n.is_integer() == False:
  print(0)
  exit()
if m < 0 or n < 0:
  print(0)
  exit()

def comb(n, k):
  if k > n - k:
    comb(n, n - k)
    return
  mul, div = 1, 1
  for i in range(k):
    mul = mul * (n - i) % mod
    div = div * (k - i) % mod
  return mul * pow(div, mod-2, mod)

ans = comb(int(n+m), int(n)) % mod
print(ans)

Fixed submission:
X, Y = map(int, input().split())
mod = pow(10, 9) + 7

m = (2*Y - X)/3
n = (2*X - Y)/3

if m.is_integer() == False or n.is_integer() == False:
  print(0)
  exit()
if m < 0 or n < 0:
  print(0)
  exit()

def comb(n, k):
  if k > n - k:
    return comb(n, n - k)
  mul, div = 1, 1
  for i in range(k):
    mul = mul * (n - i) % mod
    div = div * (k - i) % mod
  return mul * pow(div, mod-2, mod) % mod

ans = comb(int(n+m), int(n))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
  print(0)
  exit()
ans = 1
A = []
for i in range(max(D)+1):
  A.append(D.count(i))
for i in range(max(D)+1):
  if A[i] == 0:
    print(0)
    exit()
  if i > 0:
    ans *= pow(A[i-1], A[i], mod) % mod
print(ans % mod)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
A = [0]*(max(D)+1)
for i in range(N):
  A[D[i]] += 1
if D[0] != 0 or not all(D[1:]) or not all(A):
  print(0)
  exit()
if D[0] != 0:
  print(0)
  exit()
for i in range(1, max(D)+1):
#  if A[i] == 0:
#    print(0)
#    exit()
  ans = ans * pow(A[i-1], A[i], mod) % mod
print(ans % mod)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
  print(0)
  exit()
ans = 1
A = []
for i in range(max(D)+1):
  A.append(D.count(i))
for i in range(max(D)+1):
  if A[i] == 0:
    print(0)
    exit()
  if i > 0:
    ans *= pow(A[i-1], A[i], mod)
print(ans % mod)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
A = [0]*(max(D)+1)
for i in range(N):
  A[D[i]] += 1
if D[0] != 0 or not all(D[1:]) or not all(A):
  print(0)
  exit()
if D[0] != 0:
  print(0)
  exit()
for i in range(1, max(D)+1):
#  if A[i] == 0:
#    print(0)
#    exit()
  ans = ans * pow(A[i-1], A[i], mod) % mod
print(ans % mod)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
  print(0)
  exit()
ans = 1
for i in range(max(D)+1):
  if D.count(i) == 0:
    print(0)
    exit()
  if i > 0:
    ans *= pow(D.count(i-1), D.count(i), mod)
print(ans % mod)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
A = [0]*(max(D)+1)
for i in range(N):
  A[D[i]] += 1
if D[0] != 0 or not all(D[1:]) or not all(A):
  print(0)
  exit()
if D[0] != 0:
  print(0)
  exit()
for i in range(1, max(D)+1):
#  if A[i] == 0:
#    print(0)
#    exit()
  ans = ans * pow(A[i-1], A[i], mod) % mod
print(ans % mod)
Buggy python submission (verdict: Runtime Error):
from math import *
a, b, x = map(int, input().split())
if a*a*b >= 2*x:
  theta = degrees(pi/2 - atan(2*x/(a*pow(b,2))))
else:
  theta = degrees(pi/2 - atan(pow(a,3)/(2*(pow(a,2)*b - x))))    
print(theta)

Fixed submission:
from math import *
a, b, x = map(int, input().split())
if a*a*b == x:
    theta = 0
elif a*a*b > 2*x:
    theta = degrees(pi/2 - atan(2*x/(a*pow(b,2))))
else:
    theta = degrees(pi/2 - atan(pow(a,3)/(2*(pow(a,2)*b - x))))
print(theta)
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = list(map(int, input().split()))

points = []
for i in range(N):
  points.append(K)

for i in range(Q):
  correct = int(input())
  for j in range(N):
    if j != correct - 1:
      points[j] -= 1

for i in range(N):
  if points[i] <= 0:
    print('No')
  else:
    print('Yes')

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

points = [K-Q] * N

for i in range(Q):
  A = int(input())
  points[A-1] += 1
  
for i in range(N):
  if points[i] > 0:
    print('Yes')
  else:
    print('No')
Buggy python submission (verdict: Runtime Error):
A, B = map(int,input().split())

if B - A <= 0:
  print(0)
else:
  cnt = 0
  B -= 1
  while B - (A - 1) >= 0:
    B = B - (A - 1)
    cnt += 1
print(cnt)

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

socket = 1
cnt = 0
while True:
  if B == 1:
    print(0)
    exit()
  else:
    socket = socket + A - 1
    cnt += 1
    if socket >= B:
      print(cnt)
      exit()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
H = list(map(int,input().split()))

def lower(i, H):
  cnt = 0
  while H[i] >= H[i+1]:
    cnt += 1; i += 1;
    if i+1 > N-1:
      break
  return cnt

ans = 0
for i in range(N-1):
  tmp = lower(i, H)
  ans = max(ans, tmp)
  i += tmp

print(ans)

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

def lower(i, H):
  cnt = 0
  while H[i] >= H[i+1]:
    cnt += 1; i += 1;
    if i+1 > N-1:
      break
  return cnt

ans, i = 0, 0
while i < N - 1:
  tmp = lower(i, H)
  ans = max(ans, tmp)
  i += tmp+1

print(ans)

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

def lower(i, H):
  cnt = 0
  while H[i] >= H[i+1]:
    cnt += 1; i += 1;
    if i+1 > N-1:
      break
  return cnt

ans = 0
for i in range(N-1):
  ans = max(ans, lower(i, H))

print(ans)

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

def lower(i, H):
  cnt = 0
  while H[i] >= H[i+1]:
    cnt += 1; i += 1;
    if i+1 > N-1:
      break
  return cnt

ans, i = 0, 0
while i < N - 1:
  tmp = lower(i, H)
  ans = max(ans, tmp)
  i += tmp+1

print(ans)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left
N, M, X = map(int, input().split())
A = sorted(list(map(int, input().split())))
index = bisect_left(A, X)
print(min(len(A[:index]), len(A[index:])))

Fixed submission:
print(3*int(input())**2)
Buggy python submission (verdict: Time Limit Exceeded):
L, R = map(int,input().split())
mod = 2019

ans = pow(10,9)+7
for i in range(L,min(R,L+mod+1)):
  for j in range(i+1,min(R+1,i+1+mod+1)):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)

print(ans)

Fixed submission:
L, R = map(int,input().split())
mod = 2019

ans = pow(10,9)+7
for i in range(L,R):
  for j in range(i+1,R+1):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)
    if ans == 0:
      break
  if ans == 0:
    break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
L, R = map(int,input().split())
mod = 2019

ans = 2*pow(10,9)+7
for i in range(L,R):
  for j in range(i+1,R+1):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)
    if ans == 0:
      break

print(ans)

Fixed submission:
L, R = map(int,input().split())
mod = 2019

ans = pow(10,9)+7
for i in range(L,R):
  for j in range(i+1,R+1):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)
    if ans == 0:
      break
  if ans == 0:
    break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
L, R = map(int,input().split())
mod = 2019

ans = pow(10,9)+7
for i in range(L,R):
  for j in range(i+1,R+1):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)
    if ans == 0:
      break

print(ans)

Fixed submission:
L, R = map(int,input().split())
mod = 2019

ans = pow(10,9)+7
for i in range(L,R):
  for j in range(i+1,R+1):
    val = ((i%mod) * (j%mod))%mod
    ans = min(ans, val)
    if ans == 0:
      break
  if ans == 0:
    break

print(ans)
Buggy python submission (verdict: Runtime Error):
S = int(input())
for i in range(1, 4):
  if A[i] == A[i-1]:
    print("Bad")
    exit()
print("Good")

Fixed submission:
S = input()
for i in range(1, 4):
  if S[i] == S[i-1]:
    print("Bad")
    exit()
print("Good")
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]

if any([abs(A[i+1] - A[i] == 1) for i in range(len(A)-1)]):
  print(0)
  exit()

mod = pow(10, 9) + 7
dp = [0] * (N+1+2)
dp[0] = 1
if 1 not in A:
  dp[1] = 1
  
for i in range(2, N+1):
  if i not in A:
    if i-1 not in A:
      dp[i] = (dp[i] + dp[i-1]) % mod
    if i-2 not in A:
      dp[i] = (dp[i] + dp[i-2]) % mod
print(dp[N])

Fixed submission:
N, M = map(int,input().split())
a = [int(input()) for _ in range(M)]
mod = pow(10,9) + 7

dp = [0]*(N+1)
dp[0] = 1

ok = [True]*(N+1)
for i in a:
  ok[i] = False

for i in range(N):
  for j in range(i+1,min(N+1,i+3)):
    if ok[j]:
      dp[j] += dp[i]
      dp[j] %= mod
      
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
a = [int(input()) for _ in range(M)]
mod = pow(10,9) + 7

dp = [0]*(N+1)
dp[0] = 1

for i in range(N):
  for j in range(i+1,min(N+1,i+3)):
    if j not in a:
      dp[j] += dp[i]
      dp[j] %= mod
      
print(dp[N])

Fixed submission:
N, M = map(int,input().split())
a = [int(input()) for _ in range(M)]
mod = pow(10,9) + 7

dp = [0]*(N+1)
dp[0] = 1

ok = [True]*(N+1)
for i in a:
  ok[i] = False

for i in range(N):
  for j in range(i+1,min(N+1,i+3)):
    if ok[j]:
      dp[j] += dp[i]
      dp[j] %= mod
      
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
a = [int(input()) for _ in range(M)]
mod = pow(10,9) + 7

dp = [0]*(N+1)
dp[0] = 1

for i in range(N):
  for j in range(i+1,min(N+1,i+3)):
    if j not in a:
      dp[j] += dp[i]
      dp[j] %= mod
      
print(dp[N])

Fixed submission:
N, M = map(int,input().split())
a = [int(input()) for _ in range(M)]
mod = pow(10,9) + 7

dp = [0]*(N+1)
dp[0] = 1

ok = [True]*(N+1)
for i in a:
  ok[i] = False

for i in range(N):
  for j in range(i+1,min(N+1,i+3)):
    if ok[j]:
      dp[j] += dp[i]
      dp[j] %= mod
      
print(dp[N])
Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  if cnt >= N:
    break
  for _ in range(b):
    li.append(c)
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
  if cnt >= N:
    break
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
s = ""
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  s += str(c) * b
  cnt += b
li = list(s)
for i in range(min(N, cnt)):
  if A[i] < int(li[i]):
    A[i] = int(li[i])
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
  if cnt >= N:
    break
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
  if cnt >= N:
    break
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = sorted(list(map(int, input().split())))
BC = [list(map(int, input().split())) for _ in range(M)]
li = []
cnt = 0
for b, c in sorted(BC, key=lambda x:x[1], reverse=True):
  cnt += b
  for _ in range(b):
    li.append(c)
  if cnt >= N:
    break
for i in range(min(N,cnt)):
  if A[i] < li[i]:
    A[i] = li[i]
print(sum(A))
Buggy python submission (verdict: Runtime Error):
N = int(input())
G = [[] for _ in range(N)]

def dfs(v, dist):
  for nv, e_dist in G[v]:
    if ans[nv] == -1:
      ans[nv] = (dist + e_dist) % 2
      dfs(nv, dist + e_dist)

for i in range(N-1):
  u, v, w = map(int, input().split())
  G[u-1].append([v-1, w])
  G[v-1].append([u-1, w])

ans = [-1] * N
dfs(0, 0)

print(*ans, sep="\n")

Fixed submission:
import sys
sys.setrecursionlimit(pow(10, 9))

N = int(input())
G = [[] for _ in range(N)]

def dfs(v, dist):
  for nv, e_dist in G[v]:
    if ans[nv] == -1:
      ans[nv] = (dist + e_dist) % 2
      dfs(nv, dist + e_dist)

for i in range(N-1):
  u, v, w = map(int, input().split())
  G[u-1].append([v-1, w])
  G[v-1].append([u-1, w])

ans = [-1] * N
dfs(0, 0)

print(*ans, sep="\n")
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
for r in range(N+1):
  for g in range(N+1):
    if (N - r*R - g*G) % B == 0 and (N - r*R - g*G) >= 0:
      ans += 1
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
for r in range(N//R+1):
  for g in range((N-r*R)//G+1):
    if (N - r*R - g*G) % B == 0:
      ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
K = int(input())
t = S[K+1]
ans = ""
for s in S:
  if s == t:
    ans += s
  else:
    ans += "*"
print(ans)

Fixed submission:
N = int(input())
S = input()
K = int(input())
t = S[K-1]
ans = ""
for s in S:
  if s == t:
    ans += s
  else:
    ans += "*"
print(ans)
Buggy python submission (verdict: Runtime Error):
a = [int(input()) for _ in range(5)]
k = int(input())
for i in range(4):
  if a[i+1] - a[i] > k
    print(":(")
    exit()
print("Yay!")

Fixed submission:
a = [int(input()) for _ in range(5)]
k = int(input())
for i in range(4):
  if a[i+1] - a[0] > k:
    print(":(")
    exit()
print("Yay!")
Buggy python submission (verdict: Runtime Error):
N = int(input())
T = [int(input()) for _ in range(5)]
print(5 + ceil(N // min(T)))

Fixed submission:
from math import ceil
N = int(input())
T = [int(input()) for _ in range(5)]
print(4 + ceil(N / min(T)))
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
  
s = [0]*N
for i in range(N):
  s[i] = S[0:i+1:].count("AC")
    
for _ in range(Q):
  l, r = map(int, input().split())
  print(s[r-1] - s[l-1])

Fixed submission:
N, Q = map(int, input().split())
S = input()
  
ac_sum = [0]*N
for i in range(N):
  if S[max(i-1,0):i+1] == "AC":
    ac_sum[i] = ac_sum[max(i-1,0)] + 1
  else:
    ac_sum[i] = ac_sum[max(i-1,0)]

for _ in range(Q):
  l, r = map(int, input().split())
  print(ac_sum[r-1] - ac_sum[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()

for i in range(Q):
  l, r = map(int, input().split())
  print(S[l-1:r:].count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()
  
ac_sum = [0]*N
for i in range(N):
  if S[max(i-1,0):i+1] == "AC":
    ac_sum[i] = ac_sum[max(i-1,0)] + 1
  else:
    ac_sum[i] = ac_sum[max(i-1,0)]

for _ in range(Q):
  l, r = map(int, input().split())
  print(ac_sum[r-1] - ac_sum[l-1])
Buggy python submission (verdict: Runtime Error):
y, m, d = input().split("/")
if y * 365 + m * 12 + d > 2019 * 365 + 4 * 12 + 30:
  print("TBD")
else:
  print("Heisei")

Fixed submission:
y, m, d = map(int, input().split("/"))
if y * 365 + m * 30 + d > 2019 * 365 + 4 * 30 + 30:
  print("TBD")
else:
  print("Heisei")
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int,input().split()))

dp = [float("INF")] * N
dp[0] = 0
K = min(N-1, K)
for i in range(1, K+1):
  dp[i] = abs(h[i] - h[0])

for i in range(K, N):
  c = [float("INF")] * (K+1)
  for j in range(1, K+1):
    c[j] = dp[i-j] + abs(h[i] - h[i-j])
  dp[i] = min(c)
  
print(dp[N-1])

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

dp = [10**9]*N
dp[0] = 0

for i in range(1,N):
  for j in range(K+1):
    if i-j >= 0:
      dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
  
print(dp[N-1])

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

dp = [10**9]*N
dp[0] = 0

for i in range(1,N):
  for j in range(1,K+1):
    if i-j >= 0:
      dp[i] = min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))

print(dp[N-1])

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

dp = [10**9]*N
dp[0] = 0

for i in range(1,N):
  for j in range(K+1):
    if i-j >= 0:
      dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
  
print(dp[N-1])

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

dp = [10**9]*N
dp[0] = 0

for i in range(1,N):
  for j in range(K+1):
    if i-j >= 0:
      dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
  
print(dp[N-1])

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

dp = [10**9]*N
dp[0] = 0

for i in range(1,N):
  for j in range(K+1):
    if i-j >= 0:
      dp[i] = min(dp[i], dp[i-j]+abs(h[i]-h[i-j]))
  
print(dp[N-1])