Harryxun/stf_run huggingface dataset:
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

x = 0
for i in range(k):
  x = a[x]
print(x+1)

### FIXED CODE ###

n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

visited = [-1 for i in range(n+1)] # 訪れていたら訪れたときの添字を格納する。
points = []
p = 1
while visited[p] == -1:
  visited[p] = len(points)
  points.append(p)
  p = a[p-1] #添字に直す

T = len(points) - visited[p]
s = visited[p]

if k < s:
  print(points[k])
else:
  mod = (k - s) % T
  print(points[s+mod])
N = input()
a = []

for x in range(1, N):
    y = N-x
    if x == y:
        continue
    a.append([x, y])

print(len(a))

### FIXED CODE ###

N = int(input())
a = []
 
for x in range(1, N):
    y = N-x
    if x == y:
        continue
    if x > y:
        break
    a.append([x, y])
 
print(len(a))
N, W = map(int, input().split())
L = sorted( [ [int(i) for i in input().split()] for _ in range(N) ] )

dp = [0] * (W + 1)
cnt = 0

for w, v in L:
    #重さを足し合わせていき合計を求める
    cnt = cnt + w
    #W=重さの制約とこれまでの重さの合計のうち、
    #小さい方から品物iの重さまで、逆順にfor文を回す
    ##3,8-5,...15-7
    for i in range(min(cnt, W), w - 1, -1):
        #同じ重さでより価値が高い組み合わせがあれば価値を更新する
        ## w < Wの制約があり、i - wとその値を参照してから更新しているため、
        ## Wの制約を満たさない値を持つことはない
        ### 方法上、持ち得ない重さに持ち得ない値がつくことがあるが、
        ### それらが解 = 最大値になることはない
        ### (解を求める過程でついでに足されたり足されなかったりするだけなので)
        if dp[i] < dp[i - w] + v:
            dp[i] = dp[i - w] + v

print(max(dp))


### FIXED CODE ###

N = int(input())

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

C = [0] * (N)

C[0] = 0
C[1] = abs(H[1] - H[0])

for i in range(2,N):

    a = abs(H[i] - H[i - 2]) + C[i - 2]
    b = abs(H[i] - H[i - 1]) + C[i - 1]
    C[i] = min(a,b)

print(C[-1])
import math
N = int(input())
result = 0

root = math.ceil(math.sqrt(N))
for i in range(root):
    if N % i  == 0:
        result += (N // i)  - 1

print(result)


### FIXED CODE ###

import math
N = int(input())
result = 0

root = math.ceil(math.sqrt(N))
for i in range(root):
    if N % (i + 1) == 0 and (i + 1) + 1 < N // (i + 1):
        result += N // (i + 1) - 1

print(result)
num = input()
ans = [9,0,900,0,90000]
tmp = 0
if len(num)%2==0:
  tmp = sum(ans[:len(num)])
else:
  tmp += sum(ans[:len(num)-2])
  tmp += int(num[1:])+1

print(tmp)

### FIXED CODE ###

num = input()
ans = [9,0,900,0,90000]
tmp = 0

if len(num)%2==0:
  tmp = sum(ans[:len(num)])
elif len(num)==1:
  tmp = num
else:
  tmp += sum(ans[:len(num)-2])
  tmp += int(num)-10**(len(num)-1)+1

print(tmp)
while 1:
  x,y = map(int, input().split)
  if x == 0 and y == 0:
    break
  min = min(x,y)
  max = max(x,y)
  print "%d %d", (min,max)

### FIXED CODE ###

while 1:
  x, y = map(int, raw_input().split())
  if x == 0 and y == 0:
    break
  elif x > y:
    print "%d %d" % (y,x)
  else:
    print "%d %d" % (x,y)
k=int(input())
s=str(input())
if len(s) <= k:
  print(s)
else:
  print(s in range k + "...")

### FIXED CODE ###

k=int(input())
s=str(input())
if len(s) <= k:
  print(s)
else:
  print(s[0:k] + "...")
from collections import deque

H, W = map(int, input().split())
grid = [input() for i in range(H)]

# 初期が黒('#')の位置からの距離
dist = [[-1]*H for _ in range(W)]

# '#'の位置をキューに追加（＝今回はこれが複数あるので、複数スタートとなる）
black_cells = deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            black_cells.append((h, w))
            dist[h][w] = 0

d = 0
while len(black_cells) > 0:
    # キューとして使うのでpop()ではなくpopleft()
    h, w = black_cells.popleft()  # 黒いセルの座標
    d = dist[h][w]
    for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        new_h = h + dy
        new_w = w + dx
        if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
            continue
        if dist[new_h][new_w] == -1:  # セルが白('.')であるのと同義
            dist[new_h][new_w] = d+1
            black_cells.append((new_h, new_w))

print(d)

### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
grid = [list(input()) for i in range(H)]

# '#'の位置をキューに追加（＝今回はこれが複数あるので、複数スタートとなる）
black_cells = deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            black_cells.append([h, w, 0])

ans = 0
while len(black_cells) > 0:
    one_black_cell = black_cells.popleft()  # キューとして使うのでpop()ではなくpopleft()
    for dy, dx in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nh = one_black_cell[0] + dy
        nw = one_black_cell[1] + dx
        d = one_black_cell[2]
        if nh < 0 or H <= nh or nw < 0 or W <= nw:
            continue
        if grid[nh][nw] == '.':
            grid[nh][nw] = '#'
            black_cells.append([nh, nw, d + 1])
        ans = d

print(ans)
n = input()
m = input()

if n == m:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

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

if N == M:
  print("Yes")
else:
  print("No")
N = int(input())
S = input()
count = 0
for i in range(1, N + 1):
  if S[i - 1] == S[i]:
    count += 1
print(N - count)

### FIXED CODE ###

N = int(input())
S = input()
count = 0
for i in range(1, N):
  if S[i - 1] == S[i]:
    count += 1
print(N - count)
n,a,b = [int(x) for x in input().split()]

s = "b" * a + "r" * b

ans = (n // (a+b)) * a
ama = n % (a+b)

print(ans + s[:ama].count("b"))

### FIXED CODE ###

n,a,b = [int(x) for x in input().split()]

ans = (n // (a+b)) * a
ama = n % (a+b)

if a <= ama:
    print(ans + a)
else:
    print(ans + ama)
import statistics
import math

N = int(input())
A = list(map(int, input().split()))
b = 0
li = []
if max(A) == N:  
  for i in range(N):
    li.append(abs(A[i]-(b+i+1)))
  print(sum(li))
else :
  mode = max(li)
  mode = max(statistics.mode(li))
  b = -mode
  for i in range(N):
    li.append(abs(A[i]-(b+i+1)))
  print(sum(li))


### FIXED CODE ###

import statistics

N = int(input())
A = list(map(int, input().split()))
B = []
li = []
for i in range(N):
  B.append(A[i] - (i+1))
b = int(statistics.median(B))
for i in range(N):
  li.append(abs(A[i] - (b+i+1)))
print(sum(li))
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=[p-1 for p in a]
l=[0]
i=a[0]
p=-1
q=-1
for x in range(n):
    if i in (l):break
    l.append(i)
    i=a[i]
d=-1
for x in range(n):
    if a[x]==i:
        d=x
        break
f=l[:d]
b=l[d:]
if d==0 or d==n-1 or d==-1:
    print(l[k%(n-1)]+1)
    exit()

if k<=d:
    print(l[k]+1)
    exit()

k-=len(f)
print(b[k%len(b)]+1)

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
visited=[False]*(n+1)
visited[1]=True
l=[1]
i=a[0]

for x in range(n):
    if visited[i]:break
    visited[i]=True
    l.append(i)
    i=a[i-1]

d=-1
for x in range(n):
    if l[x]==i:
        d=x
        break

f=l[:d]
b=l[d:]

if len(f)==0:
    print(b[k%len(b)])
    exit()
 
if len(b)==0:
    print(f[k%len(f)])
    exit()

if k<=d:
    print(l[k])
    exit()
 
k-=len(f)
print(b[k%len(b)])
N = int(input())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
ans = min(B) - max(A) + 1

if ans < 1:
    print(0)
else:
    print(ans)

### FIXED CODE ###

N, M = map(int, input().split())
A = []
B = []
for _ in range(M):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
ans = min(B) - max(A) + 1

if ans < 1:
    print(0)
else:
    print(ans)
s = sys.stdin.readlines().replace("\n", "").lower()
for i in 'abcdefghijklmnopqrstuvwxyz':
    print(i, ":", s.count(i))


### FIXED CODE ###

import sys
from collections import Counter
m = Counter(sys.stdin.read().lower())
for i in 'abcdefghijklmnopqrstuvwxyz':
    print(i, ":", m[i])
def main ():
N = int(input())
A = list(map(int,input(). split ()))
if 0 in A:
print(0)
return
prod = 1
for a in A:
prod *= a
if prod > 1000000000000000000:
print(-1)
return
print(prod)
main ()

### FIXED CODE ###

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

for i in range(n):
  ans *= a[i]
  if ans > 10 ** 18:
    ans = -1
    break

print(ans)
start_A, end_B = map(long, raw_input().split())
  
xor_result = start_A
if start_A == end_B:
  xor_result = 0
else:
  for i in range(start_A + 1, end_B + 1):
    xor_result = xor_result ^ i
    print("  %d %s %d %s" % (i, bin(i), xor_result, bin(xor_result)))
print(xor_result)

### FIXED CODE ###

start_A, end_B = map(long, raw_input().split())

result = 0
if start_A == end_B:
  result = start_A
else:
  num_count = end_B - start_A + 1
  if start_A % 2 == 0:
    num_pair = num_count // 2
    if num_count % 2 == 0:
      result = num_pair % 2
    else:
      result = (num_pair % 2) ^ end_B
  else:
    num_pair = (num_count - 1) // 2
    if (num_count - 1) % 2 == 0:
      result = start_A ^ (num_pair % 2)       
    else:
      result = start_A ^ (num_pair % 2) ^ end_B

print result
,B = list(map(float, input().split()))
print(int((A*(B*1000))/1000))

### FIXED CODE ###

from decimal import Decimal

A,B = map(Decimal, input().split())

print(int(Decimal(A*B)))
n,m = map(int,input().split())
res ={}
for _ in range(m):
    x,y = map(int,input().split())
    res[x] = res.get(x,[]) + [y]
memo = {} #nodeを始点としたパスの最長長さ
def dfs(node):
    if node in memo:
        return memo[node]
    a = 0
    for n in res.get(node,[]):
        a = max(a,dfs(n)+1)
    memo[node] = a
    return memo[node]

ans = 0
for i in range(n):
    ans = max(ans,dfs(i))
print(ans)

### FIXED CODE ###

from collections import deque
 
N, M = map(int, input().split())
adjL = [[] for v in range(N)]
indegs = [0] * N
for _ in range(M):
    x, y = map(int, input().split())
    adjL[x-1].append(y-1)
    indegs[y-1] += 1
 
dp = [0] * N
Q = deque([v for v in range(N) if indegs[v] == 0])
while Q:
    vNow = Q.popleft()
    for v2 in adjL[vNow]:
        dp[v2] = max(dp[v2], dp[vNow] + 1)
        indegs[v2] -= 1
        if indegs[v2] == 0:
            Q.append(v2)
 
print(max(dp))
a = input()
cnt = 0
while 1:
    c = input().lower()
    if c = end_of_text:
        break
    cnt = c.split().count(a)
print(cnt)


### FIXED CODE ###

a = input().lower()
cnt = 0
while 1:
    c = input()
    if c == 'END_OF_TEXT':
        break
    cnt += c.lower().split().count(a)
print(cnt)
while 1:
  h, w = map(int, raw_input().stlip())
  if h == 0 and w == 0:
    break
  for i in range(h):
    print "#" * w
 print

### FIXED CODE ###

while 1:
  h, w = map(int, raw_input().split())
  if h == 0 and w == 0:
    break
  for i in range(h):
    print "#" * w
  print""
import sys
from itertools import accumulate
N = int(input())
S = input()
if S == '.'*N or S == '#'*N:
    print(0)
else:
    ind1 = 0
    for i, x in enumerate(S):
        if x == '#':
            ind1 = i
            break
        if i == N-1:
            print(0)
            sys.exit()
    num = 0
    for i, x in enumerate(S[::-1]):
        if x == '.':
            num = i
            break
        if i == N-1:
            print(0)
            sys.exit()
    ind2 = N - num
    if ind1 > ind2:
        print(0)
        sys.exit()
    s = S[ind1:ind2]
    if s[0] == '.' or s[-1] == '#' or len(s) == 0:
        print(0)
        sys.exit()
    else:
        a = s.split('.')
        b = s.split('#')
        aa = []
        bb = []
        for x, y in zip(a, b):
            if len(x) != 0:
                aa.append(len(x))
            if len(y) != 0:
                bb.append(len(y))
        d = [0] + list(accumulate(aa))
        e = [0] + list(accumulate(bb))
        p = d[-1]
        ls = []
        for i in range(len(d)):
            ls.append(e[i]+p-d[i])
        print(min(ls))

### FIXED CODE ###

import sys
from itertools import accumulate
N = int(input())
S = input()

if S == '.'*N or S == '#'*N:
    print(0)
    sys.exit()

ind1 = 0
for i, x in enumerate(S):
    if x == '#':
        ind1 = i
        break
num = 0
for i, x in enumerate(S[::-1]):
    if x == '.':
        num = i
        break
ind2 = N - num
s = S[ind1:ind2]
if len(s) == 0:
    print(0)
    sys.exit()

a = s.split('.')
b = s.split('#')
aa = []
bb = []
for x in a:
    if len(x) != 0:
        aa.append(len(x))
for x in b:
    if len(x) != 0:
        bb.append(len(x))
c = [0] + list(accumulate(aa))
d = [0] + list(accumulate(bb))
ls = []
for i in range(len(c)):
    ls.append(c[i] + d[-1] - d[i])
print(min(ls))
def solve(a):
    dp = [[0]*3 for _ in range(len(a)+1)]
    for i in range(len(a)):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dp[i+1][j] = max(dp[i+1][j], a[i][j] + dp[i][k])
    return max(dp[-1])
  
n = int(input())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
print(solve(a))

### FIXED CODE ###

def solve(a):
    dp = [[0]*3 for _ in range(len(a)+1)]
    for i in range(len(a)):
        for j in range(3):
            for k in range(3):
                if j != k:
                    dp[i+1][j] = max(dp[i+1][j], a[i][j] + dp[i][k])
    return max(dp[-1])
  
n = int(input())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
print(solve(a))
x,n=map(int, input().split())
if n == 0:
  print(x)
  exit()
p=list(map(int, input().split()))
all=set(range(1,101))
s_p=set(p)

min_dis=100
for i in all-s_p:
  dis=abs(i-x)
  if min_dis > dis:
    min_i=i
    min_dis=dis
print(min_dis, min_i)

### FIXED CODE ###

x,n=map(int, input().split())
if n == 0:
  print(x)
  exit()
p=list(map(int, input().split()))
all=set(range(-100,102))
s_p=set(p)
kouho=sorted(all-s_p)

min_dis=201
for i in kouho:
  dis=abs(i-x)
  if min_dis > dis:
    min_i=i
    min_dis=dis
print(min_i)
N = 3
S = [list(map(int, input().split())) for _ in range(N)]
n_guess = int(input())
guesses = set([int(input()) for _ in range(n_guess)])
dic = {}
for r in range(N):
  for c in range(N):
    dic[S[r][c]]=(r,c)

for g in guesses:
  if g in dic:
    (r, c) = dic[g]
    S[r][c] = -1
# print(S)
for i in range(N):
  if sum(S[i]) == -3:
    print('Yes')
    quit()
    
for i in range(N):
  if sum([S[_][i] for _ in range(N)]) == -3:
    print('Yes')
    quit()
    
if all([S[i][N-i-1]==-1 for i in range(N)]) or \
   all([S[i][i]==-1 for i in range(N)]):
   	print('Yes')
else:
	print('No')

### FIXED CODE ###

N = int(input())
print((N+1)//2)
# https://atcoder.jp/contests/abc053/tasks/abc053_b

s = input()
n = len(s)
for i in range(n):
    if s[i] == 'A':
        start = i
        break
for i in range(n - 1, -1):
    if s[i] == 'Z':
        end = i
        break
print(len(s[start:end]))



### FIXED CODE ###

# https://atcoder.jp/contests/abc053/tasks/abc053_b

s = input()
n = len(s)
for i in range(n):
    if s[i] == 'A':
        start = i
        break
for i in range(n - 1, -1, -1):
    if s[i] == 'Z':
        end = i
        break
print(end + 1 - start)
a = input()
b = input()
c = input()
d = input()
e = input()
k = input()
dis = e - a
if dis <= k:
    print('Yay!')
else:
    print(':(')

### FIXED CODE ###

a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
k = int(input())
dis = e - a
if dis <= k:
    print('Yay!')
else:
    print(':(')
import itertools

h, w, k = map(int, input().split())
s = 0
l= []
for _ in range(h):
    a = list(map(int, input().split()))
    s += a.count("#")
    l.append(a)
if (k > s):
    print(0)
else:
    for pair in itertools.combinations([0, 1], h + w):
        hx = pair[:h]
        wx = pair[h + 1 :]
        if (sum(wx) == 0):


### FIXED CODE ###

  
import copy
from itertools import combinations
h, w, k = map(int, input().split())
c =[input() for i in range(h)]
ans = 0
y = []
for i in range(h):
    y.append(i)
x = []
for i in range(w):
    x.append(i)
for i in range(h+1):
    ty = list(combinations(y, i))
    for j in range(w+1):
        tx = list(combinations(x, j))
        for l in ty:
            for m in tx:
                cnt = 0
                for p in l:
                    for q in m:
                        if c[p][q] == '#':
                            cnt += 1
                if cnt == k:
                    ans += 1
print(ans)
A,H= map(int, input().split())
count = 0
while H <= 0:
  H -= A
  count+=1
 print(count)

### FIXED CODE ###

import math
 
H,A = list(map(int, input().split()))
 
print(math.ceil(H/A))
a, b, c = map(int, input().split())
ans = 'Yes' if c - a - b > format(2*(a*b)**0.5, '.1f') else 'No'
print(ans)

### FIXED CODE ###

a, b, c = map(int, input().split())
ans = 'Yes' if (c - a - b)**2 > 4*(a*b) and c - a - b > 0 else 'No'
print(ans)
n = int(input())
resposta = ""

while n > 26:
    resposta += alfabeto[(n%26) - 1]
    n = (n // 26) 


resposta += alfabeto[(n % 26) - 1]
resposta = resposta[::-1]

print(resposta)


### FIXED CODE ###

alfabeto = "abcdefghijklmnopqrstuvwxyz"

n = int(input())
resposta = ""

for i in range(1, 99):
    if n <= 26 ** i:
        n -= 1

        for j in range(i):
            resposta += chr(ord("a") + n % 26)
            n //= 26

        break

    else:
        n -= 26 ** i

resposta = resposta[::-1]

print(resposta)
import math

def is_prime(x):
  if x == 1:
    return False
  elif x == 2:
    return True
  else:
    for i in range(3, math.sqrt(x)//1+1):
      if x%i == 0:
        return False
    return True

print(is_prime(3))

### FIXED CODE ###

import math

def is_prime(x):
  if x <= 0 or x%1 != 0:
    return False
  elif x == 1:
    return False
  elif x == 2:
    return True
  elif x%2 == 0:
    return False
  else:
    for i in range(3, int(math.sqrt(x)//1)+1, 2):
      if x%i == 0:
        return False
    return True

S = [0] * (10**5+1)
for i in range(1, 10**5+1):
  if is_prime(i) and is_prime((i+1)/2):
    S[i] += 1
  S[i] += S[i-1]

Q = int(input())
for _ in range(Q):
  l, r = map(int, input().split())
  print(S[r] - S[l-1])
N = input()
dict = {}

for i in range(1: 10):
    for j in range(i+1: 10):
        dict[i*j] = True

return dict.get(N)

### FIXED CODE ###

N = int(input())
dict = {}

for i in range(1, 10):
    for j in range(i, 10):
        dict[i*j] = True
print('Yes' if dict.get(N) else 'No')
# -*- coding: utf-8 -*-
S = input()

#
can_solve = True
m = [s for s in S if s != 'x']
len_m = len(m)
for i in range(len_m):
    if m[i] != m[len_m - i - 1]:
        can_solve = False
        break

#
if can_solve:
    nums = [0 for _ in range(len_m+1)]
    i, n = 0, 0
    for s in S:
        if s == 'x':
            n += 1
        else:
            nums[i] = n
            i += 1
            n = 0
    nums[i] = n

    len_nums = len_m+1
    ans = 0
    for i in range(len_nums // 2):
        ans += max(nums[i], nums[len_nums - i - 1])
    if len_nums % 2 == 1:
        ans += nums[len_nums // 2 + 1] % 2
    # print(nums)
    print(ans)


else:
    print(-1)





### FIXED CODE ###

# -*- coding: utf-8 -*-
S = input()
ans = 0
left, right = 0, len(S)-1
while left < right:
    if S[left] == S[right]:
        left += 1
        right -= 1
    elif S[left] == 'x':
        left += 1
        ans += 1
    elif S[right] == 'x':
        right -= 1
        ans += 1
    else:
        print(-1)
        exit()

print(ans)
A = int(input())
B = int(input())
result = []
for i in range(A,B+1):
    if (str(i)[0] == str(i)[4]) & (str(i)[1] == str(i)[3]):
        result.append(i)
print(len(result))

### FIXED CODE ###

A,B =map(int,input().split())
result = []
for i in range(A,B+1):
    if (str(i)[0] == str(i)[4]) & (str(i)[1] == str(i)[3]):
        result.append(i)
print(len(result))
def debug(arg):
    if __debug__:
        pass
    else:
        import sys
        print(arg, file=sys.stderr)



n, k = (int(x) for x in input().split())

c = {} 
t = 0
for x in range(k, 0, -1):
    c[x] = (k // x) ** n
    c[x] = c[x] - sum(c[x * y] for y in range(2, k // x + 1))
    s = c[x] * x
    t += s
    t = t % 1000000007

print(t)

### FIXED CODE ###

import itertools
import math

n = int(input())
c1 = range(1, n + 1)
c2 = itertools.combinations(range(1, n + 1), 2)
c3 = itertools.combinations(range(1, n + 1), 3)

t1 = sum(list(c1))
t2 = sum(list(map(lambda x: math.gcd(x[0], x[1]), c2))) * 6
t3 = sum(list(map(lambda x: math.gcd(x[0], math.gcd(x[1], x[2])), c3))) * 6

print(sum([t1, t2, t3]))
import math
X, K, D = map(int, input().split())
if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= X//D
    ans = X - (D * (X//D)) * (X // abs(X))
    if K % 2 == 0 and abs(ans) < D:
        ans = abs(ans)
    else:
        ans = min(abs(ans+D), abs(ans-D))
    # print((D * X//D) * (X // abs(X)))
print(ans)
# print(X // D, math.ceil(X / D))


### FIXED CODE ###

import sys
import math
X, K, D = map(int, input().split())
if X == 0 and K % 2 == 0:
    print(X)
    sys.exit()
elif X == 0 and K % 2 == 1:
    print(D)
    sys.exit()

if abs(X // D) >= K:
    ans = X - (D * K) * (X // abs(X))
    ans = abs(ans)
else:
    K -= abs(X//D)
    ans = X - (D * abs(X//D)) * (X // abs(X))
    if K % 2 == 0 and K == 0:
        ans = abs(ans)
    elif K % 2 == 0 and K >= 2:
        ans = min([abs(ans), abs(ans-D*2), abs(ans+D*2)])
    else:
        ans = min(abs(ans+D), abs(ans-D))
print(ans)
a,b,c=map(list,input().split())
print("".join("A",b[0],"C"))

### FIXED CODE ###

print("A"+input()[8]+"C")
import sys
from collections import defaultdict

sys.setrecursionlimit(4000)


class Node:
  def __init__(self, tpl):
    self.id = tpl[0] + 1
    self.c = int(tpl[1])
    self.relations = {}

  def add_relation(self, node):
    self.relations[node.id] = node

  def print_node_with_relations(self, nest = 0, exclude_id = None):
    print("  " * nest, self.get_print_str())
    for node in self.relations.values():
      if node.id == exclude_id: continue
      node.print_node_with_relations(nest + 1, self.id)

  def print_node(self):
    print(self.get_print_str())

  def get_print_str(self):
    return f"{self.id}({self.c})"


def prepare_nodes(n):
  node_dict = {
    node.id: node
    for node in map(Node, enumerate(input().split()))
  }

  for _ in range(n - 1):
    a, b = sorted(map(int, input().split()))
    node_dict[a].add_relation(node_dict[b])
    node_dict[b].add_relation(node_dict[a])

  return node_dict


def cal(k, node, prev_id = None):
  other_color_nodes = list(filter(
    lambda node: node.c != k and node.id != prev_id,
    node.relations.values()
  ))
  num = len(other_color_nodes)
  for next_node in other_color_nodes:
    num += cal(k, next_node, node.id)
  return num


def main():
  n = int(input())
  node_dict = prepare_nodes(n)

  for k in range(1, n + 1):
    other_color_nodes = tuple(filter(
      lambda node: node.c != k, node_dict.values()
    ))
    num = len(other_color_nodes) * 2
    if num == n * 2:
      print(0)
      continue

    for node in other_color_nodes:
      num += cal(k, node)

    print(int(n * (n - 1) / 2) + n - int(num / 2))


main()


### FIXED CODE ###

import sys
import itertools

sys.setrecursionlimit(3 * 10**5)


class Node:
  def __init__(self, color):
    self.color = int(color) - 1
    self.children = []

  def add_child(self, node):
    self.children.append(node)

  @staticmethod
  def prepare_nodes(n):
    nodes = tuple(map(Node, input().split()))
    for _ in range(n - 1):
      a, b = map(lambda x: int(x) - 1, input().split())
      nodes[a].add_child(nodes[b])
      nodes[b].add_child(nodes[a])

    return nodes


def main():
  n = int(input())
  nodes = Node.prepare_nodes(n)

  dp = [0 for _ in range(n)]
  ans_list = [n*(n + 1) // 2 for _ in range(n)]

  def update_ans(s, color):
    ans_list[color] -= (s + dp[color]) * (s + dp[color] + 1) // 2

  def dfs(s, node, parent):
    color = node.color
    pre_s = s + dp[color]
    for child in node.children:
      if child == parent: continue

      dp[color] = -s
      s = dfs(s, child, node)
      update_ans(s, color)

    s += 1
    dp[color] = pre_s - s

    return s

  s = dfs(0, nodes[0], Node(-1))

  for i in range(n):
    update_ans(s, i)
    print(ans_list[i])


main()