Harryxun/stf_run huggingface dataset:
N=int(input())
A=[int(i) for i in input().split()]
G=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
Visited=[False]*N
B=[0]*N
def B_map(x):
  C=[]
  S=-1
  Visited[x]=True
  for i in G[x]:
    if not Visited[i]:
      res=B_map(i)
      if S<0:
        S=res
      else:
        S+=res
      C.append(res)
  if S<0:
    B[x]=A[x]
  else:
    M=max(max(C),sum(C)//2)
    T=2*A[x]-S
    if 0<=(S-T)//2<=M:
      B[x]=2*A[x]-S
    else:
      print("NO")
      exit()
    if x==0 and T!=0:
      print("NO")
      exit()
  return B[x]
B_map(0)
for i in range(N):
  if B[i]<0:
    print("NO")
    exit()
print("YES")

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
N=int(input())
A=[int(i) for i in input().split()]
G=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
Visited=[False]*N
B=[0]*N
def B_map(x):
  C=[]
  S=0
  Visited[x]=True
  for i in G[x]:
    if not Visited[i]:
      res=B_map(i)
      S+=res
      C.append(res)
  if len(C)==0:
    B[x]=A[x]
  elif len(C)==1:
    if A[x]==S:
      B[x]=S
    else:
      print("NO")
      exit()
  else:
    if 0<=S-A[x]<=min(S-max(C),S//2) and 2*A[x]-S>=0:
      B[x]=2*A[x]-S
    else:
      print("NO")
      exit()
  return B[x]
B_map(0)
if len(G[0])==1:
  if B[0]!=A[0]:
    print("NO")
    exit()
else:
  if B[0]!=0:
    print("NO")
    exit()
    
print("YES")
A, B, C = map(int,input().split())
print(sum(A,B,C)-max(A,B,C))

### FIXED CODE ###

A, B, C = map(int,input().split())
print(sum([A,B,C])-max(A,B,C))
N=input()
S=input()
K=int(input())
count=0
for s in S:
    if s==S[k]:
        pass
    else:
        S=S[:count] + '*' + S[count+1:]
    count+=1
print(S)

### FIXED CODE ###

N=int(input())
S=input()
K=int(input())
count=0
for s in S:
    if s==S[K-1]:
        pass
    else:
        S=S[:count] + '*' + S[count+1:]
    count+=1
print(S)
import sys
S=sys.stdin.readline().strip()
A=["0","0"]
if int(S)<=10**9:
    A.extend([S,"0","0","1"])
    print(" ".join(A))
else:
    for i in range(2,10**9):
        for j in range(2,10**9):
            T=S-i*j
            if T==0:
               A.extend([str(i),"0","0",str(j)])
               print(" ".join(A))
               exit()

### FIXED CODE ###

s=int(input());l=10**9;n=0--s//l;print(0,0,1,l,n,n*l-s)
from itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

##累積和のリストが得られる
a_cum = list(accumulate(a))
b_cum = list(accumulate(b))

ans=[0]
for i in range(n+1):    #0~n+1
    c = bisect_right(b,k-a[i])-1 ##Aをi冊読んだ場合の残り時間 : k-a[i]  bisect_rightでbの累積時間の最大のインデックス(本の冊数)が求まる
                                 ##bisectは位置が求まるので、-1で冊数を求めている
    if c!=-1: #0から始まるので、冊数が0の場合は-1となってしまう
        ans.append(c+i)
print(max(ans))

### FIXED CODE ###

from itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

##累積和のリストが得られる
a_cum = [0] + list(accumulate(a))
b_cum = [0] + list(accumulate(b))

ans = [0]
for i in range(n + 1):    #0~n+1
    c = bisect_right(b_cum, k-a_cum[i]) - 1 ##Aをi冊読んだ場合の残り時間 : k-a[i]  bisect_rightでbの累積時間の最大のインデックス(本の冊数)が求まる
                                 ##bisectは位置が求まるので、-1で冊数を求めている
    if c != -1: #0から始まるので、冊数が0の場合は-1となってしまう
        ans.append(c+i)

print(max(ans))
a,b=int(split(input()))
x=a+b
y=2*a+2*b
print(x,y)

### FIXED CODE ###

a,b=input().split()
a=int(a)
b=int(b)
print(a*b,2*a+2*b)
import sys
sys.setrecursionlimit(100001)
def input():
    return sys.stdin.readline()[:-1]

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse = True)
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(N+1)]
for i in range(2, N+1):
    for a in A:
        if i >= x[a] and dp[i-x[a]] + 1 > dp[i]:
            dp[i] = dp[i-x[a]] + 1
K = N
ans = ''
while K > 0:
    for a in A:
        if dp[K] == dp[K-x[a]] + 1:
            if dp[K] == 1 and K != x[a]:
                continue
            ans += str(a)
            K -= x[a]
            break
print(ans)

### FIXED CODE ###

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse = True)
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(N+1)]
INF = float('inf')
for i in range(1, N+1):
    t = -INF
    for a in A:
        if i >= x[a]:
            t = max(dp[i-x[a]] + 1, t)
    dp[i] = t

K = N
k = dp[N]
ans = ""
while k > 0:
    for a in A:
        xa = x[a]
        if K >= xa and k == dp[K-xa] + 1:
            ans += str(a)
            K -= xa
            k -= 1
            break
print(ans)
s = input()
def check_and_cut(string):
    tmp = '123'
    if len(string) <= 4:
        pass
    else:
        if len(string) >4:
            if string[-5:] == 'dream':
                tmp = string[:-5]
            elif string[-5:] == 'erase':
                tmp = string[:-5]
        if len(string) >5:
            if string[-6:] == 'eraser':
                tmp = string[:-6]
        if len(string) >6:
            if string[-7:] == 'dreamer':
                tmp = string[:-7]
    #print(string,len(string),string[-6:],tmp,string[-6:] == 'eraser')
    if tmp == '':
        print('YES')
    else:
        if tmp == '123':
            print('NO')
        else:
            check_and_cut(tmp)
check_and_cut(s)

### FIXED CODE ###

import sys #追加
sys.setrecursionlimit(500*500)

s = input()
check_flag = True
def check_and_cut(string):
    tmp = '123'
    flag = True
    if len(string) <= 4:
        pass
    else:
        if len(string) >4:
            if string[-5:] == 'dream':
                tmp = string[:-5]
                flag = False
            elif string[-5:] == 'erase':
                tmp = string[:-5]
                flag = False
        if len(string) >5 and flag:
            if string[-6:] == 'eraser':
                tmp = string[:-6]
                flag = False
        if len(string) >6 and flag:
            if string[-7:] == 'dreamer':
                tmp = string[:-7]
                flag = False
    #print(string,len(string),string[-6:],tmp,string[-6:] == 'eraser')
    return tmp
while check_flag:
    t = check_and_cut(s)
    if t == '123':
        print('NO')
        exit()
    if len(t) == 0:
        print('YES')
        exit()
    s = t
import sympy

N = int(input())

a = sympy.factorint(N)
key = []
f = []
serch_mini = []

for i in a:
    key.append(i)

for i in key:
    b = a[i]
    for j in range(b):
        f.append(i)

for i in range(len(f)):
    b = f[0:i+1]
    c = f[i+1:]
    bb = 1
    cc = 1
    for j in range(len(b)):
        bb *= b[j]
    for k in range(len(c)):
        cc *= c[k]
    serch_mini.append(bb+cc)

print(min(serch_mini)-2)


### FIXED CODE ###

N = int(input())
n = int((N ** 0.5)//1 + 1)

ans = 1000000
ans_list = []

for i in range(1, n):
    if N % i == 0:
        ans = N / i
        ans_list.append(i + ans)

print(int(min(ans_list)-2))
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))

sumA = sum(A)
lmt = sum(A) /(4*m)
#print(sumA, lmt)
A.sort(reverse=True)
if A[m] > lmt:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

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

lmt = sum(A) /(4*m)
A.sort(reverse=True)
if A[m-1] >= lmt:
  print('Yes')
else:
  print('No')
N = int(input())
A = list(map(int, input().split()))

x = sum(A)
y = sum([A[i] ** 2 for i in range(N)])

print(((x ** 2 - y) // 2) % (10 ** 9 + 7))

### FIXED CODE ###

D, T, S = map(int, input().split())

if T * S >= D:
	print("Yes")
else:
	print("No")
a = list(map(int, input().split()))
b = a[0]
while b % a[1] != 0:
  b += a[0]
print(b  

### FIXED CODE ###

a = list(map(int, input().split()))
b = a[0]
while b % a[1] != 0:
  b += a[0]
print(b)
H, W  = map(int, input().split())
X = [list(input().split()) for i in range(H)]
for i in range(2*N):
  print(*X[int(i/2)])

### FIXED CODE ###

H, W  = map(int, input().split())
X = [list(input().split()) for i in range(H)]
for i in range(2*H):
  print(*X[int(i/2)])
from operator import itemgetter
from collections import OrderedDict
n = int(input())
data = OrderedDict({i:0 for i in range(n,0,-1)})
for _ in range(n):
    a, v = [int(el) for el in input().split(' ')]
    data[a] += v
print(*max(data.items(), key=itemgetter(1)))

### FIXED CODE ###

n = int(input())
data = [0] * n
for _ in range(n):
    a, v = [int(el) for el in input().split()]
    data[a-1] += v
result = max(data)
print(data.index(result)+1, result)
import collections
import math
n,k = map(int, raw_input().split(' '))
ais = map(int, raw_input().split(' '))
r = 0
m = 42
x = 0
for ii in range(m, -1, -1):
	cc = collections.Counter([(ai >> ii) & 1 for ai in ais])
	if (1 << ii) <= k:
		if cc[1] > cc[0]:
			x = x
		elif cc[1] == cc[0]:
			a,b = 0,0
			for ai in ais:
				if (ai >> ii) & 1 == 0:
					a += ai		
				else:
					b += ai
			if a <= b:
				x |= (1 << i)
		else:
			x |= (1 << ii)
	  
for ai in ais:
	r += ai ^ x
print r

### FIXED CODE ###

import collections
import math
n,k = map(int, raw_input().split(' '))
ais = map(int, raw_input().split(' '))
r = 0
m = 42
x = 0
for ii in range(m, -1, -1):
	cc = collections.Counter([(ai >> ii) & 1 for ai in ais])
	if (x + (1 << ii) <= k):
		if cc[0] > cc[1]: x += (1 << ii)
		
	  
for ai in ais:
	r += ai ^ x
print r
import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
answer,flag = 1,np.sign(a[1]-a[0])
for i in range(1,n-1):
	num = np.sign(a[i+1]-a[i])
	if flag==0:
		flag = num
	elif num==-flag:
		answer += 1
		flag = 0
print(answer)

### FIXED CODE ###

import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
answer = 1
if n!=1: flag = np.sign(a[1]-a[0])
for i in range(1,n-1):
	num = np.sign(a[i+1]-a[i])
	if flag==0:
		flag = num
	elif num==-flag:
		answer += 1
		flag = 0
print(answer)
if __name__ == '__main__':
    w,h,x,y = map(int,input().split())
    if x + x == w and y + y == h::
        a = 1
    else:
        a = 0

    print("%f %d" %(w*h/2, a))

### FIXED CODE ###

if __name__ == '__main__':
    w,h,x,y = map(int,input().split())
    if x + x == w and y + y == h:
        a = 1
    else:
        a = 0

    print("%f %d" %(w*h/2, a))
import sys
sys.setrecursionlimit(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[map(int,input().split()) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)



### FIXED CODE ###

import sys
sys.setrecursionlimit(10000)
def fill(y,x):
    global w,h,l
    if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
        l[y][x+1]=0
        fill(y,x+1)
    if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
        l[y][x-1]=0
        fill(y,x-1)
    if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
        l[y+1][x]=0
        fill(y+1,x)
    if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
        l[y-1][x]=0
        fill(y-1,x)
    
    if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
        l[y+1][x+1]=0
        fill(y+1,x+1)
    if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
        l[y-1][x+1]=0
        fill(y-1,x+1)
    if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
        l[y+1][x-1]=0
        fill(y+1,x-1)
    if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
        l[y-1][x-1]=0
        fill(y-1,x-1)
    
while 1:
    w,h=map(int,input().split())
    if not w and not h:break
    l=[list(map(int,input().split())) for _ in range(h)]

    cnt=0
    while 1:
        cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
        if not cds:
            break
        else:
            y,x=cds[0]
        l[y][x]=0
        fill(y,x)
        cnt+=1
    print(cnt)
def kaibun(s):
    for _ in range(n//2):
        if s[_]==s[n-_-1]:
            continue
        else:
            return False
    return True

s=input()
n=len(s)
if kaibun(s) and kaibun(s[:(n-1)//2])and kaibun(s[(n+3)//2-1:]):
    print("Yes")
else:
    print("No")

### FIXED CODE ###

s=input()
n=len(s)
s1=s[:(n-1)//2]
flag=0
for i in range(len(s1)//2):
    if(s1[i]!=s1[-1-i]):
        flag=1
        break

s1=s[(n+3)//2-1:]
for i in range(len(s1)//2):
    if(s1[i]!=s1[-1-i]):
        flag=1
        break

for i in range(n//2):
    if s[i]!=s[-1-i]:
        flag=1
        break

if flag==1:
    print("No")
else:
    print("Yes")
k,s=map(int,input().split())
cnt=0
for i in range(k):
  for j in range(k):
    for l in range(k):
      if i+j+l=s:
        cnt+=1
print(cnt)

### FIXED CODE ###

k,s=map(int,input().split())
cnt=0
#print(k,s)
for x in range(k+1):
  for y in range(k+1):
    z=s-x-y
    if z>=0 and z<=k:
      cnt+=1
print(cnt)
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
C = [[] for i in range(M)]
for i in range(N):
    l, r, c = MAP()
    c -= 1
    C[c].append((l, r))

# 同チャンネルで連続する区間をまとめる
LR = []
for i in range(M):
    if C[i]:
        C[i].sort()
        LR.append(C[i][0])
        for j in range(1, len(C[i])):
            l1, r1 = C[i][j-1]
            l2, r2 = C[i][j]
            if r1 == l2:
                LR.pop()
                LR.append((l1, r2))
                # 3連続以上を考慮して左端を更新
                C[i][j][0] = l1
            else:
                LR.append((l2, r2))

# いもす法
MAX = 10 ** 5 + 7
imos = [0] * MAX
for l, r in LR:
    imos[l-1] += 1
    imos[r] -= 1
imos = list(accumulate(imos))
print(max(imos))


### FIXED CODE ###

# -*- coding: utf-8 -*-

N, C = map(int, input().split())
T = 10**5+2

c_ls = [[0] * T for _ in range(C)]
# いもす
for i in range(N):
    s, t, c = map(int, input().split())
    c_ls[c-1][s] += 1
    c_ls[c-1][t] -= 1
# 各チャンネル毎の累積和
for i in range(C):
    for j in range(1, T):
        c_ls[i][j] += c_ls[i][j-1]
# 他チャンネルとの重複を見るために開始を1早める
for i in range(C):
    for j in range(1, T):
        if c_ls[i][j] > c_ls[i][j-1]:
            c_ls[i][j-1] += 1
# 全チャンネルを合計
sm = [0] * T
for i in range(C):
    for j in range(T):
        sm[j] += c_ls[i][j]
# 重複が一番多くなる箇所が答え
print(max(sm))
import re
a, b = [int(x) for x in input().split()]
s = input()
print("Yes" if re.search(f"\\d{{{a}}}-\\d{{{b}}}", s) is not None else "No")

### FIXED CODE ###

import re
a, b = [int(x) for x in input().split()]
s = input()
print("Yes" if re.search("\\d{{{}}}-\\d{{{}}}".format(a, b), s) is not None else "No")
l,r,d=map(int(input()).split())
print(r//d-(l-1)//d)

### FIXED CODE ###

l,r,d=map(int,input().split())
print(r//d-(l-1)//d)
n = input()
d = {"S":[none]*13, "H":[none]*13, "C":[none]*13, "D":[none]*13}

for i in range(n):
	x, y = raw_input().split()
	d[x][int(y)-1] = True

for k in ["S","H","C","D"]:
	for i in range(13):
		if d[k][i]:
			continue
		print ("%s %d"%(k, i+1))

### FIXED CODE ###

n = int(raw_input())
lst = []
#store all the cards
for i in ["S","H","C","D"]:
	for j in xrange(1,14):
		lst.append("{0} {1}".format(i,j))
#discard the given cards
for i in range(n):
	a = raw_input()
	lst.remove(a)
#print the result
for i in range(len(lst)):
	print lst[i]
n=int(input())
a=[int(j) for j in input().split()]

def f(l,m):
    if l==[]:
        return -10**100
    if m==1:
        return max(l)
    if len(l)<=2:
        return -10**100
    return max(f(l[1:],m),l[0]+f(l[2:],m-1))

print(f(a,n//2))

### FIXED CODE ###

n=int(input())
a=[int(j) for j in input().split()]
p=[0]*n
d=[0]*n
for i in range(n):
    p[i]=a[i]+p[i-2]
    if (i&1):
        d[i]=max(p[i-1],a[i]+d[i-2])
    else:
        d[i]=max(d[i-1],a[i]+d[i-2])
print(d[-1])
import math
A,B,C,D=(int(x) for x in input().split(" "))
def nb_divisers(n,m):
    return n//m
def nb_d_btw(f,g,n):
    if f%n==0:
        print(nb_divisers(g,n)-nb_divisers(f,n)+1)
        return nb_divisers(g,n)-nb_divisers(f,n)+1
    else:
        print(nb_divisers(g,n)-nb_divisers(f,n))
        return nb_divisers(g,n)-nb_divisers(f,n)
def nb_notdivisers_f(a,b,c,d):
    e=int(c*d/math.gcd(c,d))
    return b-a+1-(nb_d_btw(a,b,c)+nb_d_btw(a,b,d)-nb_d_btw(a,b,e))
print(nb_notdivisers_f(A,B,C,D))

### FIXED CODE ###

A, B, C, D=(int(x) for x in input().split(" "))
import fractions

def eks(x,a):
    return a//x

def wai(x):
    return eks(x,B)-eks(x,A-1)

def lcm(a,b):
    return a*b//fractions.gcd(a,b)

zet=B-A+1-(wai(C)+wai(D)-wai(lcm(C,D)))
print (zet)
N, M = map(int,input().split())
results = [0 for _ in range(N+1)]
wa_cnt = 0
ac_cnt = 0
for _ in range(M):
  problem, result = input().split()
  if results[problem] == 0 and result == 'AC':
    ac_cnt += 1
    results[problem] = 1
  elif results[problem] == 0 and result == 'WA':
    wa_cnt += 1
print(ac_cnt, wa_cnt)

### FIXED CODE ###

N, M = map(int,input().split())
results = [0 for _ in range(N+1)]
wa_list = [0 for __ in range(N+1)]
ac_cnt = 0
wa_cnt = 0
for _ in range(M):
  problem, result = input().split()
  if results[int(problem)] == 0 and result == 'AC':
    ac_cnt += 1
    results[int(problem)] = 1
  elif results[int(problem)] == 0 and result == 'WA':
    wa_list[int(problem)] += 1
for i in range(1, N+1):
  if results[i] == 1:
    wa_cnt += wa_list[i]
print(ac_cnt, wa_cnt)
a=input() #len input
b=input() #string input
if len(b)>int(a): # if exceed the len given
    print(b[:int(a)],"...",sep = "") #print up to a
else:
    print(c)

### FIXED CODE ###

#K input for the range of S
K = int(input())
#input of lowercase english letters
S = input()
if len(S) > K:
    print(S[:K] + "...")
else:
    print(S)
while True:
    n,w=map(int,raw_input().split())
    if n==w==0:break
    L=[0]*(w+1)
    for i in range(n):
        v=input()
        L[v/w]+=1
    maxh=max(L)
    r=n-1
    while r>0 and L[r]==0:
        r-=1
    ink=0.01
    for i in range(r+1):
        p=float(r-i)/r
        ink+=float(r-i)/r*float(L[i])/maxh
    print ink

### FIXED CODE ###

while True:
    n,w=map(int,raw_input().split())
    if n==w==0:break
    L=[0]*(w+1)
    for i in range(n):
        v=input()
        L[v/w]+=1
    maxh=max(L)
    r=w
    while r>0 and L[r]==0:
        r-=1
    ink=0.01
    for i in range(r+1):
        if r<>0:
            ink+=float(r-i)/r*float(L[i])/maxh
        else:
            ink+=L[i]
    print ink
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

i = 1
pas = [i]
k = K
while k > 0:
    if A[i] in pas:
        break
    pas.append(A[i])
    i = A[i]
    k -= 1

roopindx = pas.index(A[i])

roopi = K%(len(pas)-roopindx)

print(pas[roopi])

### FIXED CODE ###

import numpy as np

def main():
    N,K = map(int,input().split())
    A = [0]
    A += list(map(int,input().split()))
    A = np.array(A)
    
    i = 1
    pas = np.zeros(N)
    pas[0] = i
    k = 1
    pass_set = set(pas)
    while k <= K:
        if A[i] in pass_set:
            rps = np.where(pas==(A[i]))[0]
            ans = A[i]
            break
        pas[k] = A[i]
        pass_set.add(A[i])
        ans = A[i]
        i = A[i]
        k += 1

    if k >= K:
        print(ans)
    else:
        rpnum = (K-rps[0])%(k-rps)
        print(int(pas[rps[0]+rpnum]))

main()
N = int(input())
A = list(map(int, input().split()))

for i in range(1, N+1):
  print(A.count(i))

### FIXED CODE ###

import math

R = int(input())
output = 2* R * math.pi

print(output)
n=int(input())
a=list(map(int,input().split()))
min=0
ans=0
for i z in enumerate:
  if i==0:continue
  if min>=z:
    ans+=1
  min=min(min,z)
print(ans)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
m=a[0]
ans=1
for i,z in enumerate(a):
  if i==0:continue
  if z<=m:
    ans+=1
  m=min(m,z)
print(ans)
n=int(input())
l=[0]*n
l[0]=2
l[1]=1
for i in range(n):
  if i==0 or i==1:
    continue
  else:
    l[i]=l[i-1]+l[i-2]
print(l[n])

### FIXED CODE ###

n=int(input())
l=[0]*(n+1)
l[0]=2
l[1]=1
for i in range(n+1):
  if i==0 or i==1:
    continue
  else:
    l[i]=l[i-1]+l[i-2]
print(l[n])
n,m=map(int,input().split())

def fac(n):
  if n == 0:
    return 1
  else:
    return n * fac(n-1)

def binom(n,k):
  if n==0:
    return 0
  else:
    return fac(n) // (fac(n-k) * fac(k))

print(binom(n,2) + binom(m,2))


### FIXED CODE ###

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

def fac(n):
  if n == 0:
    return 1
  else:
    return n * fac(n-1)

def binom(n,k):
  if n==0 or n==1:
    return 0
  else:
    return fac(n) // (fac(n-k) * fac(k))

print(binom(n,2) + binom(m,2))
a,b,k=map(int,input().split())
l=[]
if k==1:
	print("1")
	break
for i in range(1,min(a,b)):
	if a%i==0 and b%i==0:
		l.append(i)

print(l[k-1])

### FIXED CODE ###

a,b,k=map(int,input().split())
l=[]

for i in range(1,(min(a,b))+1):
	if a%i==0 and b%i==0:
		l.append(i)
l.sort(reverse=True)
if a==1 or b==1:
    l=[1,1]
print(l[k-1])
a,b = map(int, raw_input().split())
print str(a*b) + " " + str(2(a+b))

### FIXED CODE ###

a,b = map(int, raw_input().split())
print str(a * b) + " " + str(2 * (a+b))
N = int(input())
l = list()

for n in range(N):
    a = input()
    l.append(a)

l_uniq = list(set(l))

if len(l) == len(l_uniq):
    for i in range(0, N-1):
        if a[i+1][0] != a[i][-1]:
            print("No")
            break
    else:
        print("Yes")
else:
    print("No")
    


### FIXED CODE ###

N = int(input())
l = list()

for n in range(N):
    a = input()
    l.append(a)

l_uniq = list(set(l))

if len(l) == len(l_uniq):
    for i in range(0, N-1):
        if l[i+1][0] != l[i][-1]:
            print("No")
            break
    else:
        print("Yes")
else:
    print("No")
S=input()
h=S/3600
m=(S%3600)/60
s=(S%3600)%60
print("%d:%d:%d"%(h,m,s))

### FIXED CODE ###

t=int(input())
h=t//3600
m=(t%3600)//60
s=t-h*3600-m*60
print('{}:{}:{}'.format(h,m,s))
import math
A, B, C, D = map(int, input().split())
count = 0
BCcount = math.floor(B / C)
BDcount = math.floor(B / D)
ACcount = math.floor((A - 1) / C)
ADcount = math.floor((A - 1) / D)
l = C * D / gcd(C, D)
BCDcount = math.floor(B / l)
ACDcount = math.floor((A - 1) / l)
Bcount = B - (BCcount + BDcount - BCDcount) 
Acount = (A - 1) - (ACcount + ADcount - ACDcount)
print(Bcount - Acount)

### FIXED CODE ###

from decimal import Decimal
def gcd(a,b):
  if b == 0:
    return a
  else:
    return gcd(b, a%b)

A, B, C, D = map(int, input().split())
count = 0
BCcount = B // C
BDcount = B // D
ACcount = (A - 1) // C
ADcount = (A - 1) // D
l = C * D // gcd(C, D)
BCDcount = B // l
ACDcount = (A - 1) // l
Bcount = Decimal(B - (BCcount + BDcount - BCDcount))
Acount = Decimal((A - 1) - (ACcount + ADcount - ACDcount))
print(Decimal(Bcount - Acount))