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

from sys import stdin
import numpy as np
from scipy.ndimage.filters import minimum_filter, maximum_filter

def init_maze(maze, H, W, start, goal):
    cost = np.zeros((H, W), dtype=int) + 999
    done = np.zeros((H, W), dtype=bool)
    barrier = np.zeros((H, W), dtype=bool)
    for i in range(H):
        for j in range(W):
            if (i, j) == start:
                # start = (i, j)
                cost[i, j] = 0
                done[i, j] = True
            if (i, j) == goal:
                pass
                # goal = (i, j)
            if maze[i][j] == '#':
                barrier[i,j] = True
    return cost, done, barrier

def bnf(maze, cost, done, barrier, goal, g):
    for i in range(1, 999):
        done_next = maximum_filter(done, footprint=g) * ~done

        cost_next = minimum_filter(cost, footprint=g) * done_next
        cost_next[done_next] += 1

        cost[done_next] = cost_next[done_next]
        cost[barrier] = 999

        done[done_next] = done_next[done_next]
        done[barrier] = False

        if done[goal[0], goal[1]] == True:
            return max(cost_next[done_next])
            break
    return 0


if __name__ == '__main__':
    H, W = [ int(x) for x in stdin.readline().rstrip().split() ]
    maze = []
    for i in range(H):
        maze.append(stdin.readline().rstrip())

    g = np.array([
        [0,1,0],
        [1,1,1],
        [0,1,0]
    ])

    start = (2, 0)
    goal = (0, 4)
    costs = []
    for i in range(H):
        for j in range(W):
            start = (i, j)
            for i_g in range(i, H):
                for j_g in range(j, W):
                    goal = (i_g, j_g)
                    cost, done, barrier = init_maze(maze, H, W, start, goal)
                    cost, done, barrier = init_maze(maze, H, W, start, goal)
                    costs.append(bnf(maze, cost, done, barrier, goal, g))
    print(max(costs))


### FIXED CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

from collections import deque

H, W = input_nums()
H += 2
W += 2
grid = '#' * W
for i in range(H - 2):
    grid += '#' + input() + '#'
grid += '#' * W

def bfs(start):
    INF = 10**9
    dist = [INF] * (H * W)
    dist[start] = 0
    q = deque([start])
    while q:
        v = q.popleft()
        dv = dist[v]
        for move in (1, -1, W, -W):
            next_v = v + move
            if grid[next_v] == '#':
                continue
            d_next_v = dv + 1
            if d_next_v >= dist[next_v]:
                continue
            dist[next_v] = d_next_v
            q.append(next_v)
    return max([x for x in dist if x < INF])

ans = max([bfs(x) for x in range(H * W) if grid[x] != '#'])
print(ans)
### BUGGY CODE ###

n = int(input())
btn = []
checked = []
flag = True
j = 1
count = 0
for i in range(n):
  btn.append(int(input()))

while flag:
  if btn[j - 1] == 2:
    count += 1
    print(count)
    flag = False
  elif j - 1 in checked:
    print(-1)
    flag = False
  else:
    checked.append(j - 1)
    j = btn[j - 1]
    count += 1


### FIXED CODE ###

n = int(input())
btn = []
checked = set()

for i in range(n):
  btn.append(int(input()))

def find(n):
  global btn, checked
  j = 1
  for i in range(n):
    if btn[j - 1] == 2:
      # count += 1
      return len(checked) + 1
    elif j - 1 in checked:
      return -1
    else:
      checked.add(j - 1)
      j = btn[j - 1]
  return -1

print(find(n))
### BUGGY CODE ###

n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
     for j in range(i + 1,n-1):
          flag = True
          for k in range(j + 1,n):
               if l[i] + l[j] > l[k]  :
                    ans += 1
               else:
                    break
print(ans)

### FIXED CODE ###

import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
     for j in range(i + 1,n-1):
          ans += bisect.bisect_left(l,l[i] + l[j]) - j - 1
print(ans)
### BUGGY CODE ###

def make_divisors(n):
    i = 1
    while i*i <= n:
        if n % i == 0:
            yield i
            if i != n // i:
                yield n//i
        i += 1


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

exist = set()
dup = set()
for item in a:
    if item in exist:
        dup.add(item)
    else:
        exist.add(item)

ret = 0

for val in a:
    is_ok = True
    if val in dup:
        continue
    for item in make_divisors(val):
        if item == val:
            continue
        elif item in cand:
            is_ok = False
            break
    if is_ok:
        ret += 1

print(ret)


### FIXED CODE ###

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

cand = set(a)

exist = set()
dup = set()
for item in a:
    if item in exist:
        dup.add(item)
    else:
        exist.add(item)

dame_set = set()

a.sort()
ret = 0

for val in a:
    if val in dup:
        if val in dame_set:
            continue
        else:
            i = 1
            cnt = 10 ** 6 // val
            while i <= cnt:
                tmp = i * val
                if tmp in cand:
                    dame_set.add(tmp)
                i += 1
    else:
        if val in dame_set:
            continue
        else:
            ret += 1
            i = 1
            cnt = 10 ** 6 // val
            while i <= cnt:
                tmp = i * val
                if tmp in cand:
                    dame_set.add(tmp)
                i += 1

print(ret)
### BUGGY CODE ###

maxa=-(10**10)
a,b,n=map(int,input().split())
for i in range(1,min(n+1,b+1)):
  maxa=max(((a*i)//b)-a*(i//b),maxa)
print(maxa)

### FIXED CODE ###

maxa=-(10**10)
a,b,n=map(int,input().split())
i=min(n,b-1)
maxa=max(((a*i)//b)-a*(i//b),maxa)
print(maxa)
### BUGGY CODE ###

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

if min(A)==1:
  print(1)
  exit()

while len(A)>1:
  monster_min = A.pop(A.index(min(A)))
  for i in range(len(A)):
    A[i] %= monster_min
  A.sort()
  for i in A[:]:  
    if i == 0: A.remove(i)
    else: continue
  A.append(monster_min)

print(A[0])

### FIXED CODE ###

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

if min(A)==1:
  print(1)
  exit()

while len(A)>1:
  monster_min = A.pop(A.index(min(A)))
  for i in range(len(A)):
    A[i] %= monster_min
  A.sort()
  for i in A[:]:  
    if i == 0: A.remove(i)
    else: continue
  A.append(monster_min)

print(A[0])
### BUGGY CODE ###

import sys
from itertools import combinations, permutations, product, combinations_with_replacement, accumulate
from heapq import heapify, heappop, heappush, heappushpop
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from math import sqrt, log, floor, ceil, factorial, cos, sin, pi #, gcd
from fractions import gcd
from operator import mul
from functools import reduce
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
INF = float('inf')
LINF = 2**63-1
NIL = -LINF
MOD = 10**9+7
MGN = 4
def II(): return int(input())
def IF(): return float(input())
def IS(): return input().replace('\n', '')
def ILCI(n: int): return [II() for _ in range(n)]
def ILCF(n: int): return [IF() for _ in range(n)]
def ILI(): return list(map(int, input().split()))
def ILLI(n: int): return [[int(j) for j in input().split()] for i in range(n)]
def ILF(): return list(map(float, input().split()))
def ILLF(n: int): return [[float(j) for j in input().split()] for i in range(n)]
def LTOS(lst: list, sep: str = ' '): return sep.join(map(str, lst))
def DEC(lst: list): return list(map(lambda x: x-1, lst))
def INC(lst: list): return list(map(lambda x: x+1, lst))


def main():
    A,B,C = ILI()

    ans = "No"
    if C - A - B < 0:
      for i in range(10**8):
        pass
    
    # A + 2sqrt(AB) + B < C
    if C - A - B  > 0 and  4 * A * B < (C- A - B) ** 2:
        ans = "Yes"

    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from itertools import combinations, permutations, product, combinations_with_replacement, accumulate
from heapq import heapify, heappop, heappush, heappushpop
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from math import sqrt, log, floor, ceil, factorial, cos, sin, pi #, gcd
from fractions import gcd
from operator import mul
from functools import reduce
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
INF = float('inf')
LINF = 2**63-1
NIL = -LINF
MOD = 10**9+7
MGN = 4
def II(): return int(input())
def IF(): return float(input())
def IS(): return input().replace('\n', '')
def ILCI(n: int): return [II() for _ in range(n)]
def ILCF(n: int): return [IF() for _ in range(n)]
def ILI(): return list(map(int, input().split()))
def ILLI(n: int): return [[int(j) for j in input().split()] for i in range(n)]
def ILF(): return list(map(float, input().split()))
def ILLF(n: int): return [[float(j) for j in input().split()] for i in range(n)]
def LTOS(lst: list, sep: str = ' '): return sep.join(map(str, lst))
def DEC(lst: list): return list(map(lambda x: x-1, lst))
def INC(lst: list): return list(map(lambda x: x+1, lst))


def main():
    A,B,C = ILI()

    ans = "No"
    # A + 2sqrt(AB) + B < C
    if C - A - B  > 0 and  4 * A * B < (C- A - B) ** 2:
        ans = "Yes"

    print(ans)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

N=int(input())
p=input()
p=p.split()
A=[]
for i in range(len(p)):
  A.append(int(p[i]))

X=[]
for i in range(N):
  for j in range(i+1,N):
    X.append(A[i]*A[j])
x=0
for i in range(len(X)):
  x+=X[i]

MOD=1000000007
print(x%MOD)

  




### FIXED CODE ###

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

S=sum(A)
a=0
for x in A:
  S-=x
  a+=S*x
    
MOD=10**9+7
print(int(a%MOD))
### BUGGY CODE ###

for i in range(3000):
  for j in range(3000):
    print(i + j)

### FIXED CODE ###

N, T = map(int, input().split())
food = [0] * N
for i in range(N):
  food[i] = list(map(int, input().split()))

food = sorted(food)  
#i番目までの商品でj分いないに完食できる美味しさの最大値  
dp = [[0] * (T + 1) for i in range(N + 1)]  

for i in range(N):
  for j in range(1, T + 1):
    #i番目の商品を使わない場合
    now = max(dp[i + 1][j - 1], dp[i][j])
    #使う場合
    if (j - food[i][0]) >= 0:
      now2 = dp[i][j - food[i][0]] + food[i][1]
    else:
      now2 = 0
    dp[i + 1][j] = max(now, now2)
    
#print(dp)
#T - 1分に i + 1番目の商品を注文
ans = 0
for i in range(N):
  now3 = dp[i][T - 1] + food[i][1]
  #print(dp[i][T - 1], food[i][1])
  ans = max(ans, now3)
  
print(ans)
### BUGGY CODE ###

from collections import deque

n = int(input())
graph = [[0 for _ in range(n)] for _ in range(n)]
for _ in range(n - 1):
    a, b, c = map(int, input().split())
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c
q, k = map(int, input().split())

distance = [float('inf') for _ in range(n)]
distance[k - 1] = 0

d = deque()
d.append(k - 1)

visited = set({})
total = set([i for i in range(n)])
while len(total - visited) > 0:
    point = d.popleft()
    prev = float('inf')
    for index, value in enumerate(graph[point]):
        if value > 0:
            distance[index] = min(distance[point] + graph[point][index], distance[index])
        if (value <= prev) & (value > 0):
            if index not in visited:
                d.append(index)
    visited.add(point)
    
for _ in range(q):
    x, y = map(int, input().split())
    print(distance[x - 1] + distance[y - 1])

### FIXED CODE ###

from collections import deque

n = int(input())
graph = [[] for _ in range(n)]

for _ in range(n - 1):
    a, b, c = map(int, input().split())
    graph[a - 1].append((b - 1, c))
    graph[b - 1].append((a - 1, c))
q, k = map(int, input().split())

distance = [float('inf') for _ in range(n)]
distance[k - 1] = 0

d = deque()
d.append(k - 1)
visited = set({})
while d:
    point = d.popleft()
    for index, value in graph[point]:
        distance[index] = min(distance[point] + value, distance[index])
        if index not in visited:
            d.append(index)
    visited.add(point)
    
for _ in range(q):
    x, y = map(int, input().split())
    print(distance[x - 1] + distance[y - 1])
### BUGGY CODE ###

import sys
N,A,B=map(int,input().split())
S=[]
for i in range(N):
  S+=[int(input())] 
count=0
S.sort()
C=A-B
h=[S[0]]
for i in range(N-1):
  if S[i+1]>=S[0]*(B/A):
    h+=[S[i+1]]
  else:
    break

N=len(h)

while True:
  h.sort(reverse=1)
  if h[0]<=0:
    print(count)
    sys.exit()
  if (h[0]/A)%1==0:
    t=int(h[0]/A)
  else:
    t=int(h[0]/A)+1
  for i in range(N):
    h[i]-=B*t
  for j in range(t):
    h.sort(reverse=1)
    h[0]-=C
  count+=t

### FIXED CODE ###

import math
N,A,B=map(int,input().split())
S=[]
for i in range(N):
  S+=[int(input())] 
S.sort(reverse=1)
count=0

def Check(T):
  count=0
  for i in range(N):
    a=math.ceil((S[i]-B*T)/(A-B))
    if a>0:
      count+=a
    else:
      break
  if count<=T:
    return 1
  else:
    return 0

L=1
while True:
  if S[0]/B >=2**L:
    L+=1
  else:
    break

T=2**L#checkしたいナンバー。
for l in range(L,0,-1):
  if Check(T)==1:
    T-=2**(l-1)
  elif Check(T)==0:
    T+=2**(l-1)
if Check(T)==0:
  print(T+1)
else:
  print(T)
### BUGGY CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
d = []
for _ in range(m):
  b, c = map(int, input().split())
  d.extend([c for _ in range(b)])
a.extend(d)
desc = sorted(a, reverse=True)
ans = sum(desc[:n])
print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
a = list(map(int, input().split()))
d = []
for _ in range(m):
  b, c = map(int, input().split())
  d.append((b, c))
d.sort(key=lambda x: x[1], reverse=True)
l = 0
for bc in d:
  a.extend([bc[1] for _ in range(bc[0])])
  l += bc[0]
  if l > n: break
desc = sorted(a, reverse=True)
ans = sum(desc[:n])
print(ans)
### BUGGY CODE ###

#122 C

N,Q= map(int,input().split())
S=input()
ans=[0]*len(S)
for i in range(len(S)):
    ans[i]=S[0:i+1].count('AC')
    
for i in range(Q):
    l,r=map(int,input().split())
    print(ans[r-1]-ans[l-1])

### FIXED CODE ###

#122 C
N,Q= map(int,input().split())
S=input()
ans=[0]*(N+1)

for i in range(2,N+1):
    ans[i]=ans[i-1]
    if S[i-2:i]=='AC':
        ans[i]+=1
    
for i in range(Q):
    l,r=map(int,input().split())
    print(ans[r]-ans[l])
### BUGGY CODE ###

n = int(input())
#a, b = map(int, input().split())
#x, y, z = map(int, input().split())
#a, b, h, m = map(int, input().split())
arr = list(map(int, input().split()))

p = 1
for i in arr:
	p *= i
		
if p > (10**18):
	print(-1)
else:
	print(p)


### FIXED CODE ###

n = int(input())
#a, b = map(int, input().split())
#x, y, z = map(int, input().split())
#a, b, h, m = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
p = 1
if 0 in arr:
	print(0)
else:
	for i in reversed(arr):
		p *= i
		if p > (10**18):
			print(-1)
			exit()
	print(p)
### BUGGY CODE ###

N = int(input())
S = [input() for i in range(N)]

import itertools
word_list=[]
for word in S:
  word=''.join(sorted(word))
  word_list.append(word)
word_set=set(word_list)

num_list=[]
for word in word_set:
  num = len(list(itertools.combinations(range(word_list.count(word)),2)))
  num_list.append(num)
print(sum(num_list))

### FIXED CODE ###

N = int(input())
S = [input() for i in range(N)]

list = {}
for i in S:
  i=''.join(sorted(i))
  if i in list:
    list[i] = list[i] + 1
  else:
    list[i] = 1

count = 0
for i in list.values():
  n = i - 1
  count = count + int(((n * (n + 1)) / 2))
  
print(count)
### BUGGY CODE ###

import math
import bisect
import heapq
from collections import deque

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

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

d = [float('inf')] * n
d[0] = 0
d[1] = abs(h[0] - h[1])


for i in range(2,n):

    p = [100000] * k

    for j in range(0, min(k,i)):
        # print(i-j)
        p[j] = d[i-j-1] + abs(h[i-j-1]-h[i])

    d[i] = min(p)

print(d[n-1])


### FIXED CODE ###

# import math
# import bisect
# import heapq
# from collections import deque

def main():
    n, k = map(int, input().split())

    h = list(map(int, input().split()))
    inf = 10**7
    d = [inf] * n
    d[0] = 0

    for j in range(1, n):
        p = [d[i] + abs(h[j] - h[i]) for i in range(max(0, j - k), j)]

        d[j] = min(p)

    print(d[-1])

if __name__ == "__main__":
    main()
### BUGGY CODE ###

from functools import reduce
import math
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
def modinv(a, m):
    g, x, y = xgcd(a, m)
    return x % m
def modmul(a,b):
    return a * b % 998244353
def comb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(modmul, range(n, n - r, -1))
    under = reduce(modmul, range(1,r + 1))
    return over * modinv(under, 998244353)
def mempow(a,b):
    temp = 1
    yield temp
    for i in range(b):
        temp = temp * a % 998244353
        yield temp

N,M,K = (int(x) for x in input().split())
ans = 0
if M == 1:
    if K + 1 < N:
        print('0')
    else:
        print('1')
else:
    for i in range(K+1):
        ans = (ans + (comb(N-1,i)*M*pow(M-1,N-i-1,998244353))) % 998244353
    print(ans)

### FIXED CODE ###

from functools import reduce
import math
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
def modinv(a, m):
    g, x, y = xgcd(a, m)
    return x % m
def modmul(a,b):
    return a * b % 998244353
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)
def mempow(a,b):
    temp = 1
    yield temp
    for i in range(b):
        temp = temp * a % 998244353
        yield temp
def memfact(a):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % 998244353
        yield temp

N,M,K = (int(x) for x in input().split())
memp = []
memf = []
mpappend = memp.append
mfappend = memf.append
for x in mempow(M-1,N-1):
    mpappend(x)
for x in memfact(N-1):
    mfappend(x)
ans = 0
if M == 1:
    if K + 1 < N:
        print('0')
    else:
        print('1')
else:
    i = 0
    for i in range(K+1):
        ans = (ans + (comb(N-1,i,998244353)*M*memp[N-i-1])) % 998244353
    print(ans)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=[]
for _ in range(n):
    aa.append(int(input()))
a = aa[0]
b = max(aa[1:])
print(b)
for i in range(1,n):
    if a >= b:
        print(a)
        b = a
        a = aa[i]
    else:
        if aa[i] != b:
            print(b)
            a = aa[i]
        else:
            a = aa[i]
            b = max(aa[:i]+aa[i+1:])
            print(b)

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=[]
for i in range(n):
    aa.append(int(input()))
ma = max(aa)
if aa.count(ma) > 1:
    for _ in range(n):
        print(ma)
else:
    for i in range(n):
        if aa[i] != ma:
            print(ma)
        else:
            print(max(aa[:i]+aa[i+1:]))
### BUGGY CODE ###

n = int(input())
v = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
for i in range(1 << n):
    value = 0
    cost = 0
    for j in range(n):
        if i >> j & 1:
            value += v[j]
            cost += c[j]
    ans = max(ans, value - cost)

print(ans)

### FIXED CODE ###

n = int(input())
v = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
for i in range(n):
    if v[i] > c[i]:
        ans += v[i] - c[i]

print(ans)
### BUGGY CODE ###

x = int(input())
a = [1]
s = 1
for i in range(2, x+1):
    s += i
    a.append(s)

if x in a:
    print(a.index(x) + 1)
    exit()

for i in a:
    if x < i:
        print(a.index(i)+1)
        break


### FIXED CODE ###

import bisect
from math import sqrt
x = int(input())
a = [1]
s = 1
for i in range(2, 2*int(sqrt(x))):
    s += i
    a.append(s)

if x in a:
    print(a.index(x) + 1)
    exit()

print(bisect.bisect_right(a, x)+1)
### BUGGY CODE ###

n, m = map(int,input().split())
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
for i in range(m):
    l.sort()
    for j in range(s[i][0]):
        if l[j] < s[i][1]:
            l[j] = s[i][1]
        else:
            break
print(sum(l))


### FIXED CODE ###

n, m = map(int,input().split())
l = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(m)]
# n,m = 3,2
# l = [5,1,4]
# s = [[2,3],[1,5]]
# l = [1, 8, 5, 7, 100, 4, 52, 33, 13, 5]
l.sort()
s.sort(key=lambda x: x[1], reverse=True)
idx = 0
for i in range(m):
    for j in range(idx, min(n, idx + s[i][0])):
        if l[j] < s[i][1]:
            l[j] = s[i][1]
    idx += s[i][0]
        # else:
        #     idx += s[i][0]
        #     break
print(sum(l))
### BUGGY CODE ###

n, m = map(int, input().split())
p = []
s = []
ac_list = []
wa_list = [0] * n
wa_count = 0
for i in range(m):
    input_p, input_s = input().split()
    if input_p not in ac_list:
        if input_s == 'AC':
            ac_list.append(input_p)
            wa_count += wa_list[int(input_p)-1]
        else:
            wa_list[int(input_p)-1] += 1

        
print(len(ac_list), wa_count)

### FIXED CODE ###

n, m = map(int, input().split())
ps = [[0, 0] for i in range(n)]  
wa_count = 0
ac_count = 0
for i in range(m):
    input_p, input_s = input().split()
    if ps[int(input_p)-1][0] <= 0:
        if input_s == 'AC':
            ps[int(input_p)-1][0] += 1
        else:
            ps[int(input_p)-1][1] += 1
for i in ps:
    if i[0] == 1:
        ac_count += 1
        wa_count += i[1]

print(ac_count, wa_count)
### BUGGY CODE ###

N, K = map(int, input().split())
dst = list(map(int, input().split()))
log = [1]
current = 1
for i in range(1, K+1):
    current = dst[current-1]
    if current in log:
        loop_start_ind = log.index(current)
        loop_len = i-loop_start_ind
        current = log[loop_start_ind+(K-i) % loop_len]
        break
    else:
        log.append(current)
print(current)

### FIXED CODE ###

N, K = map(int, input().split())
dst = list(map(int, input().split()))
log = [1]
log_set=set(log)
current = 1
for i in range(1, K+1):
    current = dst[current-1]
    if current in log_set:
        loop_start_ind = log.index(current)
        loop_len = i-loop_start_ind
        current = log[loop_start_ind+(K-i) % loop_len]
        break
    else:
        log.append(current)
        log_set.add(current)
print(current)
### BUGGY CODE ###

import itertools
import fractions

K = int(input())
K = [i for i in range(1, K+1)]
p = itertools.combinations_with_replacement(K, 3)
p = list(p)
g = []

for i in p:
    a = fractions.gcd(i[0], i[1])
    b = fractions.gcd(a, i[2])
    if i[0] != i[1] != i[2]:
        g.append(b)
        g.append(b)
        g.append(b)
        g.append(b)
        g.append(b)
        g.append(b)
    elif i[0] == i[1] == i[2]:
        g.append(b)
    else:
        g.append(b)
        g.append(b)
        g.append(b)


print(sum(g))


### FIXED CODE ###

import math

K = int(input())
g = 0
for i in range(1, K+1):
    for j in range(1, i+1):
        for k in range(1, j+1):
            a = math.gcd(i, j)
            b = math.gcd(a, k)
            if i != j != k:
                g += b*6
            elif i == j == k:
                g += b
            else:
                g += b*3

print(g)
### BUGGY CODE ###

n = int(input())

count = 0
for i in range(n-1):
    count += i+1

print(count)

### FIXED CODE ###

n = int(input())

print(int((n-1)*n//2))
### BUGGY CODE ###

q1=list(map(int,input().split(" ")))
a1=list(map(int,input().split(" ")))

sumsum=0
for i in range(q1[0]-q1[1]+1):
    sum1=sum(a1[i:i+q1[1]])
    if(sumsum<sum1):
        sumsum=sum1
sumsum+=q1[1]
print(sumsum/2)


### FIXED CODE ###

q1=list(map(int,input().split(" ")))
a1=list(map(int,input().split(" ")))


sum1=sum(a1[:q1[1]])
sumsum=sum1
for i in range(q1[0]-q1[1]):
    sum1=sum1+a1[i+q1[1]]-a1[i]
    if(sumsum<sum1):
        sumsum=sum1
sumsum+=q1[1]
print(sumsum/2)