Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

n = int(input())
lis = list(map(int, input().split()))
def abs(m):
  if m>0:
    return m
  return(-m)
def mysum(data):
  ans = 0
  data = [0] + data+[0]
  for j in range(len(data)-1):
    ans += abs(data[j] - data[j+1])
  return(ans)
for i in range(len(lis)):
  tmp = lis[:i] + lis[i+1:]
  print(mysum(tmp))


### FIXED CODE ###

n = int(input())
lis = [0] + list(map(int, input().split())) + [0]
tmp = []
for j in range(len(lis)-1):
    tmp.append(lis[j+1] - lis[j])
lis = list(map(abs,tmp))
preans = sum(lis)
for i in range(len(tmp)-1):
    print(preans - lis[i] - lis[i+1] + abs(tmp[i] + tmp[i+1]))
### BUGGY CODE ###

if __name__ == '__main__':
    N,Q=list(map(int,input().split()))
    events=[list(map(int,input().split())) for _ in range(N)]
    D=[int(input()) for i in range(Q)]
    ans=["-1"]*Q
    skip=[-1]*Q
    events.sort(key=lambda x:x[2])
    import bisect
    for S,T,X in events:
        right=bisect.bisect_left(D,T-X)
        left=bisect.bisect_left(D,S-X)
        while left<right:
            if skip[left]==-1:
                ans[left]=str(X)
                skip[left]=right
                left+=1
            else:
                left=skip[left]
    print("\n".join(ans))


### FIXED CODE ###

import bisect
import sys
input = sys.stdin.readline


n, q = map(int, input().split())
events = [list(map(int, input().split())) for _ in range(n)]
events.sort(key=lambda x: x[2])

departures = [int(input()) for _ in range(q)]


ans = ['-1'] * q
skip = [-1] * q

for start, end, pos in events:
    left = bisect.bisect_left(departures, start-pos)
    right = bisect.bisect_left(departures, end-pos)

    while left < right:
        if skip[left] == -1:
            ans[left] = str(pos)
            skip[left] = right
            left += 1

        else:
            left = skip[left]

print('\n'.join(ans))
### BUGGY CODE ###

from bisect import bisect_left

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

t = [A[0]]
for a in A[1:]:
    i = bisect_left(t, a) - 1
    if i == -1:
        t = [a] + t
    else:
        t[i] = a
        t.sort()
print(len(t))


### FIXED CODE ###

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

t = [A[0]]
for a in A[1:]:
    if t[-1] >= a:
        t.append(a)
        continue
    ok = len(t) - 1
    ng = -1
    while ok - ng > 1:
        m = (ok + ng) // 2
        if t[m] < a:
            ok = m
        else:
            ng = m
    t[ok] = a
print(len(t))
### BUGGY CODE ###

from collections import defaultdict
from heapq import heappush,heappop,heapify
inf=2**31-1
n,m,*t=map(int,open(0).read().split())
edge=defaultdict(list)
used,d={},{}
l=[]
for a,b,c in zip(t[::3],t[1::3],t[2::3]):
  if b<a:a,b=b,a
  x,y=a+n*c,b+n*c
  if b==n:
    l.append(y)
    d[y]=inf
  edge[a].append((1,x))
  edge[b].append((1,y))
  edge[x].extend([(0,a),(0,y)])
  edge[y].extend([(0,b),(0,x)])
  used[a]=used[b]=used[x]=used[y]=True
used[1]=False
edgelist=edge[1]
heapify(edgelist)
while edgelist:
  m=heappop(edgelist)
  if not used[m[1]]:
    continue
  d[m[1]]=m[0]
  used[m[1]]=False
  for e in edge[m[1]]:
    if used[e[1]]:
      heappush(edgelist,(e[0]+m[0],e[1]))
a=min([d[i]for i in l]or[inf])
print([a,-1][a==inf])

### FIXED CODE ###

def main():
  from heapq import heappush,heappop,heapify
  inf=2**31-1
  n,m,*t=map(int,open(0).read().split())
  z=[]
  for a,b,c in zip(t[::3],t[1::3],t[2::3]):
    z.extend([a,b,a+n*c,b+n*c])
  z={i:v for v,i in enumerate(sorted(set(z)))}
  try:
    z[1]
  except:
    print(-1)
    exit()
  edge=[[]for _ in range(len(z))]
  d={}
  l=[]
  for a,b,c in zip(t[::3],t[1::3],t[2::3]):
    if b<a:a,b=b,a
    i,j,x,y=z[a],z[b],z[a+n*c],z[b+n*c]
    if b==n:
      l.append(y)
    edge[i].append((1,x))
    edge[j].append((1,y))
    edge[x].extend([(0,i),(0,y)])
    edge[y].extend([(0,j),(0,x)])
  used=[False]+[True]*~-len(z)
  d=[0]+[inf]*~-len(z)
  edgelist=edge[0]
  heapify(edgelist)
  while edgelist:
    m=heappop(edgelist)
    if not used[m[1]]:
      continue
    d[m[1]]=m[0]
    used[m[1]]=False
    for e in edge[m[1]]:
      if used[e[1]]:
        heappush(edgelist,(e[0]+m[0],e[1]))
  a=min([d[i]for i in l]or[inf])
  print([a,-1][a==inf])
if __name__=='__main__':
  main()
### BUGGY CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 13 14:27:40 2019

@author: YudNK
"""
import bisect

N, Q = map(int, input().split())
X = []
for i in range(N):
    s, t, x = map(int, input().split())
    X.append([s - x, t - x, x])
D = [int(input()) for i in range(Q)]
keys = [i for i in range(Q)]
X.sort(key = lambda x:x[2])
ans = [-1 for i in range(Q)]
for i in X:
    m = i[0]
    n = i[1]
    xi = i[2]
    it = bisect.bisect_left(D, m)
    while it != len(D):
        if D[it] >= n:
            break
        ans[keys[it]] = xi
        D.pop(it)
        keys.pop(it)
[print(i) for i in ans]

### FIXED CODE ###

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 15 16:58:00 2019

@author: YudNK
"""
import bisect as bs

N, Q = map(int, input().split())
E = []
for i in range(N):
    s, t, x = map(int, input().split())
    a = [s - x, t - x, x] #イベント
    E.append(a)
E.sort(key = lambda x: x[2])
D = []
for i in range(Q):
    D.append(int(input()))

ans = [-1]*Q
check = [-1]*Q

for e in E:
    l = bs.bisect_left(D, e[0])
    r = bs.bisect_left(D, e[1])
    while l < r:
        if check[l] == -1:  
            ans[l] = e[2]
            check[l] = r
            l += 1
        else:
            l = check[l]
        
[print(i) for i in ans]
### BUGGY CODE ###

import math

A,B,N=map(int, input().split())
a=0
b=0
for i in range(max(N-B+1,1),N+1):
    a=i%B
    if b<a:
        b=a
c=math.floor(A*b/B)-A*math.floor(b/B)
print(c)

### FIXED CODE ###

import math

A,B,N=map(int, input().split())

b=0

if N>=B:
  b=B-1
else:
  b=N

c=math.floor(A*b/B)-A*math.floor(b/B)
print(c)
### BUGGY CODE ###

import itertools
n = int(input())
s = list(map(str,input()))
count = 0
unique = len(set(s))
for i in range(1, len(s)+1):
    cand = list(itertools.combinations(s, i))

    if i<= unique:
        for j in range(len(cand)):
            if len(set(cand[j])) == i:
                count +=1
print(count % (10**9 + 7))

### FIXED CODE ###

n = int(input())
s = list(map(str,input()))
count = 1
unique = list(set(s))
for i in unique:
    count *= (s.count(i) + 1)
count -=1
print(count % (10**9 + 7))
### BUGGY CODE ###

def notempty(b):
    return any(map(lambda x: len(x), b))


n = int(input())
a = [list(reversed(list(map(lambda x:int(x) - 1, input().split()))))
     for _ in range(n)]
cnt = 0
while notempty(a):
    flg = 0
    cnt += 1
    li = [True] * n
    for i in range(n):
        if not len(a[i]):
            continue
        tmp = a[i][-1]
        if li[i] and len(a[tmp]) and li[tmp] and a[tmp][-1] == i:
            a[i].pop()
            a[tmp].pop()
            li[i], li[tmp] = False, False
            flg = 1
    if not flg:
        print(-1)
        exit(0)
print(cnt)


### FIXED CODE ###

n = int(input())
g = [[] for _ in range(n ** 2)]
inv = [0] * (n ** 2)
for i in range(n):
    a = list(map(lambda x: int(x)-1, input().split()))
    for j in range(n - 2):
        p, q = min(i, a[j]), max(i, a[j])
        r, s = min(i, a[j + 1]), max(i, a[j + 1])
        g[p * n + q].append(r * n + s)
        inv[r * n + s] += 1
s = []
for i in range(n):
    for j in range(i + 1, n):
        if inv[i * n + j] == 0:
            s.append(i * n + j)
d = [0] * (n ** 2)
while s:
    p = s.pop()
    for node in g[p]:
        d[node] = max(d[node], d[p] + 1)
        inv[node] -= 1
        if inv[node] == 0:
            s.append(node)
for i in range(n):
    for j in range(i + 1, n):
        if inv[i * n + j] > 0:
            print(-1)
            exit(0)
print(max(d) + 1)
### BUGGY CODE ###

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

tmp = [0] * k
ans = float("inf")
for i in range(n-k+1):
  tmp = a[i:i+k]
  if (tmp[0] <= 0 and tmp[-1] <= 0) or (tmp[0] >= 0 and tmp[-1] >= 0):
    cost = max(abs(tmp[0]), abs(tmp[-1])) 
  else:
    cost = min(abs(tmp[0]), abs(tmp[-1]))*2 + max(abs(tmp[0]), abs(tmp[-1]))
    
  if ans > cost:
    ans = cost
    
print(ans)

### FIXED CODE ###

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


ans = float("inf")
for i in range(n-k+1):
  #tmpの左端と右端の符号が同じなら、端から移動していく
  if (a[i] <= 0 and a[i+k-1] <= 0) or (a[i] >= 0 and a[i+k-1] >= 0):
    cost = max(abs(a[i]), abs(a[i+k-1]))
  #選んだろうそくが正負にまたがる場合、左端と右端の近いほうに移動してから  
  else:
    cost = min(abs(a[i]), abs(a[i+k-1]))*2 + max(abs(a[i]), abs(a[i+k-1]))
    
  if ans > cost:
    ans = cost
    
print(ans)
### BUGGY CODE ###

from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

N,M=map(int,input().split())
LRC=[list(map(int,input().split())) for i in range(M)]
stack=[[0,-1]]
idle_max=10**18
visited=[idle_max for i in range(N+1)]
visited2=[0 for i in range(N+1)]
while stack:
    m,a=heappop(stack)
    a=-a
    if a==N:
        print(m)
        break
    if not visited2[a]:
        visited2[a]=1
        for l,r,c in LRC:
            if l<=a<r:
                _m=m+c
                if _m<visited[r]:
                    visited[r]=_m
                    heappush(stack,[_m,-r])
else:
    print(-1)


### FIXED CODE ###

from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))

N,M=map(int,input().split())
LRC=[list(map(int,input().split())) for i in range(M)]
LRC.sort()


stack=[[0,-1,0]]
idle_max=10**18
visited=[idle_max for i in range(N+1)]
visited2=[0 for i in range(N+1)]
visited[1]=0
x=0
while stack:
    #print(stack)
    m,a,p=heappop(stack)
    a=-a
    if a==N:
        print(m)
        break
    if not visited2[a]:
        visited2[a]=1
        for i in range(x,M):
            l,r,c=LRC[i]
            x=i
            if l<=a:
                _m=m+c
                if _m<visited[r]:
                    visited[r]=_m
                    heappush(stack,[_m,-r,a+1])
            else:
                break
else:
    print(-1)
### BUGGY CODE ###

N, A, B, K = map(int, input().split())
mod = 998244353

fac = [1]
inv = [1]
for i in range(1, N + 1) :
    fac.append(fac[-1] * i % mod)
    inv.append(pow(fac[-1], mod - 2, mod))

ret = 0
for na in range(N + 1) :
    if (K - na * A) % B != 0 :
        continue
        
    nb = (K - na * A) // B
    for ng in range(max(0, na + nb - N), min(na, nb) + 1) :
        ret += fac[N] * inv[na-ng] * inv[nb-ng] * inv[ng] * inv[N-na-nb+ng] % mod
        ret %= mod
        
print(ret)


### FIXED CODE ###

N, A, B, K = map(int, input().split())
mod = 998244353

fac = [1]
inv = [1]
for i in range(1, N + 1) :
    fac.append(fac[-1] * i % mod)
    inv.append(pow(fac[-1], mod - 2, mod))

ret = 0
for na in range(N + 1) :
    if (K - na * A) % B != 0 :
        continue
        
    nb = (K - na * A) // B
    if 0 <= na <= N and 0 <= nb <= N :
        ret += fac[N] * inv[N-na] * inv[na] * fac[N] * inv[N-nb] * inv[nb]
        ret %= mod
        
print(ret)
### BUGGY CODE ###

#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
import math
x, n = map(int, input().split())
p = list(map(int, input().split()))

ans = [0]*100

if n == 0:
    print(x)

else:
    for i in range(100):
        ans[i] = i+1

    sort = sorted(p)
    for i in sort:
        ans.remove(i)

    big = 0
    j = -1
    while(big == 0):
        j = j+1
        big = ans.count(x+j)

    big = x+j

    small = 0
    k =-1
    while(small == 0):
        k = k+1
        small = ans.count(x-k)

    small = x-k

    if abs(x-big) < abs(x-small):
        print(big)
    else:
        print(small)


### FIXED CODE ###

#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
import math
x, n = map(int, input().split())
p = list(map(int, input().split()))

if n == 0:
    print(x)

else:
    max = -1
    i = -1
    while(max == -1):
        i = i + 1
        if p.count(x+i) == 0:
            max = x+i

    min = -1
    j = -1
    while(min == -1):
        j = j + 1
        if p.count(x-j) == 0:
            min = x-j

    if (abs(x-max) < abs(x-min)):
        print(max)
    else:
        print(min)
### BUGGY CODE ###

def solve():
  n, m = map(int,input().split())
  plst = list(map(int,input().split()))
  pare_inds = [i for i in range(n)]
  belongs = [[] for i in range(n)]
  
  def get_root(x):
    p = pare_inds[x]
    if p == x:
      return x
    a = get_root(p)
    pare_inds[x] = a
    return a
  
  for i in range(m):
    x, y = map(int,input().split())
    x, y = x - 1, y - 1
    r_x, r_y  = get_root(x), get_root(y)
    if r_x < r_y:
      pare_inds[y] = r_x
      pare_inds[r_y] = r_x
    else:
      pare_inds[x] = r_y
      pare_inds[r_x] = r_y
  
  for i in range(n):
    belongs[get_root(i)].append(i)
  
  ans = 0
  for lst in belongs:
    for i in lst:
      if plst[i] - 1 in lst:
        ans += 1
  
  print(ans)
solve()

### FIXED CODE ###

def solve():
  n, m = map(int,input().split())
  plst = list(map(int,input().split()))
  pare_inds = [i for i in range(n)]
  belongs = [[] for i in range(n)]
  
  def get_root(x):
    p = pare_inds[x]
    if p == x:
      return x
    a = get_root(p)
    pare_inds[x] = a
    return a
  
  for i in range(m):
    x, y = map(int,input().split())
    x, y = x - 1, y - 1
    r_x, r_y  = get_root(x), get_root(y)
    if r_x < r_y:
      pare_inds[y] = r_x
      pare_inds[r_y] = r_x
    else:
      pare_inds[x] = r_y
      pare_inds[r_x] = r_y
  
  for i in range(n):
    belongs[get_root(i)].append(i)
  #print(belongs)
  #print(plst)
  ans = 0
  for lst in belongs:
    lst2 = [plst[i] - 1 for i in lst]
    lst.sort()
    lst2.sort()
    ind1 = 0
    ind2 = 0
    end = len(lst)
    while ind1 < end and ind2 < end:
      if lst[ind1] == lst2[ind2]:
        ans += 1
        ind1 += 1
        ind2 += 1
      elif lst[ind1] < lst2[ind2]:
        ind1 += 1
      else:
        ind2 += 1
  
  print(ans)
solve()
### BUGGY CODE ###

import itertools
N, K = list(map(int, input().split()))
count = 0
for i in range(K, N+1):
    count += N - i
for i in range(K+1, N+1):
    for j in range(1, i):
        if i % j >= K:
            count += 1
print(count)

### FIXED CODE ###

N, K = list(map(int, input().split()))
count = 0
if K == 0:
    print(N*N)
    exit()
for i in range(1, N+1):
    count += int(N/i) * max(i-K, 0) + max(N - int(N/i)*i + 1 - K, 0)
print(count)
### BUGGY CODE ###

s = input()

index = 0
thanS = s[0]
count = 1
a = []
for i in range(1, len(s)):
    if thanS == s[i]:
        count += 1

    else:
        a.append(s[index: index + count])
        thanS = s[i]
        index = index + count
        count = 1

a.append(s[index:])
b = [0]

for i in range(len(a)):
    diff = len(a[i])
    if a[i][0] == "<":
        add = [b[-1] + 1 + j for j in range(diff)]
    else:
        if b[-1] < diff:
            b[-1] = diff
        add = [diff - j for j in range(1, diff + 1)]
    b = b + add

print(sum(b))


### FIXED CODE ###

s = input()

index = 0
thanS = s[0]
count = 1
a = []
for i in range(1, len(s)):
    if thanS == s[i]:
        count += 1

    else:
        a.append(s[index: index + count])
        thanS = s[i]
        index = index + count
        count = 1

a.append(s[index:])
b = 0
add = 0

for i in range(len(a)):
    diff = len(a[i])
    if a[i][0] == "<":
        add += diff * (diff + 1) / 2
        b = diff
    else:
        if b < diff:
            add = add + diff * (diff + 1) / 2 - b
        else:
            add = add + (diff - 1) * diff / 2
        b = 0

print(int(add))
### BUGGY CODE ###

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

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        while b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
            b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
            ans += 1
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)
### BUGGY CODE ###

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


c = 0
a = 0

t = [0]*(n+1)

temp = []


for i in range(m):
    s = input().split()
    if s[0] not in temp:
        if s[1] == 'AC':
            a += 1
            c += t[int(s[0])]
            temp += [s[0]]
        elif s[1] == 'WA':
            t[int(s[0])] += 1
    elif len(temp) == n:
        break

print(a, c)


### FIXED CODE ###

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

a = [0] * (n+1)
b = [0] * (n+1)

c = 0

for i in [0]*(m):

    s = input().split()

    if a[int(s[0])] == 0:
        if s[1] == "AC":
            a[int(s[0])] = 1
            c += b[int(s[0])]
        else:
            b[int(s[0])] += 1

print(sum(a), c)
### BUGGY CODE ###

N, P = map(int, input().split())
S = input()
lenp = len(str(P))
c = 0

if P == 2 or P == 5:
    c = 0
    for i in range(N-1, -1, -1):
        if int(S[i]) % P == 0:
            # それ以降桁を増やしてもOK
            c += i+1
    print(c)
else:
    S = [int(S[i:N]) for i, s in enumerate(tuple(S))]

    dicmodp = {}
    dicmodp[0] = 1
    for s in S:
        r = s % P
        dicmodp.setdefault(r, 0)
        dicmodp[r] += 1
    c = 0
    for k, v in dicmodp.items():
        if v > 1:
            c += v*(v-1)//2
    print(c)


### FIXED CODE ###

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    def solve25(N, P, S):
        c = 0
        for i in range(N-1, -1, -1):
            if int(S[i]) % P == 0:
                # 以降桁を増やしてもOK
                c += i+1
        return c
    print(solve25(N, P, S))
else:
    def solve(N, P, S):
        rcounts = [0] * P
        rcounts[0] = 1 # sそのものが割り切れた場合に引き算する相手がいないから
        ints = 0
        for i, s in enumerate(S[::-1]):
            ints += int(s) * pow(10, i, P)
            r = ints % P
            rcounts[r] += 1
        c = 0
        for v in rcounts:
            c += v*(v-1)//2
        return c
    print(solve(N, P, S))
### BUGGY CODE ###

n,m=map(int,input().split())
A=list(int(input()) for i in range(m))+[-1]
dp=[0]*(n+1)
dp[0]=1
for i in range(1,n+1):
    if i in A:
        dp[i]=0
    else:
        dp[i]=dp[i-1]+dp[i-2]
print(dp[n]%1000000007)

### FIXED CODE ###

n,m=map(int,input().split())
A=list(int(input()) for i in range(m))+[-1]
dp=[0]*(n+1)
dp[0]=1
j=0
for i in range(1,n+1):
    if i==A[j]:
        dp[i]=0
        j +=1
    else:
        dp[i]=(dp[i-1]+dp[i-2])

print(dp[n]%1000000007)
### BUGGY CODE ###

n,m,qu=map(int,input().split())
l=[];r=[]
for i in range(m):
  l0,r0=map(int,input().split())
  l.append(l0);r.append(r0)

for i in range(qu):
  honsu=0
  p,q=map(int,input().split())
  for j in range(m):
    if l[j]>=p and r[j]<=q:
      honsu+=1
  print(honsu)

### FIXED CODE ###

n,m,qu = map(int,input().split())
lr = [[0]*(n+1) for _ in range(n+1)]

for i in range(m):
  l,r = map(int,input().split())
  lr[l][r]+=1

for i in range(1,n+1):
  for j in range(i,n+1):
    lr[i][j]+=lr[i][j-1]
  
for i in range(1,n+1):
  for j in range(i):
    lr[i-j-1][i]+=lr[i-j][i]
    
for i in range(qu):
  p,q = map(int,input().split())
  print(lr[p][q])
### BUGGY CODE ###

import collections
def LI(): return list(map(int,input().split()))
N,Q = LI()
ab = [LI() for _ in range(N-1)]
px = [LI() for _ in range(Q)]
ans = [0]*(N+1) #1_index
graph = {i:collections.deque() for i in range(1,N+1)} #1_index
for a,b in ab:
        graph[a].append(b)
        graph[b].append(a)
for p,x in px:
    ans[p] += x
seen = [-1]*(N+1)  #1_index
stack = []
def dfs():
    seen[1] = 1
    stack.append(1)
    while stack:
        s = stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            g_NO = graph[s].popleft()
            if seen[g_NO]!=-1:
                continue
            seen[g_NO] = 1
            stack.append(g_NO)
            ans[g_NO] += ans[s]
dfs()
print(*ans[1:])


### FIXED CODE ###

import collections,copy,sys
def LI(): return list(map(int,sys.stdin.readline().split()))
N,Q = LI()
ans = [0]*(N+1)
graph = [collections.deque() for _ in range(N+1)] #1_index
for _ in range(N-1):
    a,b = LI()
    graph[a].append(b)
    graph[b].append(a)
for _ in range(Q):
    p,x = LI()
    ans[p] += x
seen = [-1]*(N+1) #1_index
stack = []
def dfs():
    seen[1] = 1
    stack.append(1)
    while stack:
        s = stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            g_NO = graph[s].popleft()
            if seen[g_NO]!=-1:
                continue
            seen[g_NO] = 1
            stack.append(g_NO)
            ans[g_NO] += ans[s]
dfs()
print(*ans[1:])
### BUGGY CODE ###

from queue import LifoQueue

'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

N = int(input())
A =  [[0 for i in range(N)] for j in range(N)]
q = LifoQueue()
b = [-1 for i in range(N)]
c = [0 for i in range(N)]
l = [0 for i in range(N)]

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

q.put(0)
while not q.empty():
	i = q.get()
	c[i] = 1
	for j in range(N):
		if A[i][j] > 0 and c[j] == 0:
			q.put(j)
			b[j] = i
			l[j] = l[i] + A[i][j]

for i in l:
	print('{}'.format(i%2))


### FIXED CODE ###

N = int(input())
q = []
c = [0 for i in range(N)]
l = [0 for i in range(N)]
A = [[] for i in range(N)]
W = [[] for i in range(N)]

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

q.append(0)
while len(q):
	i = q.pop(0)
	c[i] = 1
	k = 0
	for j in A[i]:
		if c[j] == 0:
			q.append(j)
			l[j] = l[i] + W[i][k]
		k = k + 1

for i in l:
	print('{}'.format(i%2))
### BUGGY CODE ###

k=int(input())
import math
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            a=math.gcd(i,j)
            ans+=math.gcd(a,l)
print(ans)

### FIXED CODE ###

k=int(input())
import math
ans=0
for i in range(1,k+1):
    for j in range(1,k+1):
        a=math.gcd(i,j)
        for l in range(1,k+1):
            ans+=math.gcd(a,l)
print(ans)
### BUGGY CODE ###

#https://atcoder.jp/contests/abc162/tasks/abc162_c
# -*- coding: utf-8 -*-
import math
ans = 0
K = int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      ans += math.gcd(math.gcd(i,j),k)
print(ans)

### FIXED CODE ###

#https://atcoder.jp/contests/abc162/tasks/abc162_c
# -*- coding: utf-8 -*-
import math
ans = 0
K = int(input())
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      ans += math.gcd(math.gcd(i,j),k)
print(ans)