CodeEff/ECCO huggingface dataset:
# Original solution
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,k = list(map(int,ipt().split()))
#    n = 5000
#    k = 1
    p = [int(i)-1 for i in ipt().split()]
#    p = [(i-1)%n for i in range(n)]
#    c = [10**9 for i in range(n)]
    c = [int(i) for i in ipt().split()]
    ma = -10**18
    al = [False]*n
    for i in range(n):
        if al[i]:
            continue
        pts = []
        nap = i
        for j in range(n):
            nap = p[nap]
            pts.append(c[nap])
            al[nap] = True
            if nap == i:
                break
        ln = len(pts)
        jt = (k-1)%ln
        jt += 1+ln
        sma = 0
        for jp in pts:
            sma += jp
        if sma > 0:
            nm = ((k-1)//ln-1)*sma
        else:
            nm = 0
        pts *= 2
        ma1 = -10**18
#        print(pts,al)
        for ji in range(ln):
            sm = 0
            for jj in range(jt):
                sm += pts[ji-jj]
#                print(i,j,j-jj,sm)
                if ma1 < sm:
                    ma1 = sm
        ma1 += nm
        if ma < ma1:
            ma = ma1
#        print(ma)

    print(ma)




    return None

if __name__ == '__main__':
    main()


# Optimized solution
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,k = list(map(int,ipt().split()))
#    n = 5000
#    k = 1
    p = [int(i)-1 for i in ipt().split()]
#    p = [(i-1)%n for i in range(n)]
#    c = [10**9 for i in range(n)]
    c = [int(i) for i in ipt().split()]
    ma = -10**18
    al = [False]*n
    for i in range(n):
        if al[i]:
            continue
        pts = []
        nap = i
        for j in range(n):
            nap = p[nap]
            pts.append(c[nap])
            al[nap] = True
            if nap == i:
                break
        ln = len(pts)
        jt = k%ln+ln
        sma = 0
        for jp in pts:
            sma += jp
        if sma > 0:
            nm = (k//ln-1)*sma
        else:
            nm = 0
        pts *= 2
        ma1 = -10**18
#        print(pts,al)
        for ji in range(ln):
            sm = 0
            for jj in range(jt):
                sm += pts[ji-jj]
#                print(i,j,j-jj,sm)
                if ma1 < sm:
                    ma1 = sm
        ma1 += nm
        if ma < ma1:
            ma = ma1
#        print(ma)

    print(ma)




    return None

if __name__ == '__main__':
    main()
# Original solution
import sys
input = sys.stdin.readline
#from itertools import accumulate
#from itertools import combinations
N,K = list(map(int,input().split()))
P = list(map(int,input().split()))
C = list(map(int,input().split()))

#N,K = 4,4
#P = [2,3,4,1]
#C = [-9,9,1,-1]

visited = [False]*(N+1)
loops = []
for v in range(1,N+1):
    if visited[v]:
        continue
    l = [v]
    while True:
        v = l[-1]
        visited[v] = True
        nv = P[v-1]
        if visited[nv]:
            break
        l.append(nv)
    loops.append(l)

cand = []
for loop in loops:
    temp = 0
    vals = [C[i-1] for i in loop]
    l = len(vals)
    vals2 = vals+vals[:-1]
    vals3 = vals+vals+vals[:-1]
    cumsum = [0]
    for v in vals3:
        cumsum.append(cumsum[-1] + v)
    #cumsum = [0] + list(accumulate(vals2))
    gh = []
    #print(vals)
    if K <= l:
        temp = 0
        for left in range(0,2*l-K):
            for right in range(left+1,left+K+1):
                gh.append(cumsum[right] - cumsum[left])
        if gh:
            temp = max(gh)
    else:
        temp = 0
        s = sum(vals)
        q, r = divmod(K,l)
        q -= 1
        r += l
        for left in range(0,3*l-r):
            for right in range(left+1,left+r+1):
                #print(left,right)
                gh.append(cumsum[right] - cumsum[left])
        if gh:
            temp = max(gh)
        if s > 0:
            temp += q * s
    cand.append(temp)
print((max(cand)))






# Optimized solution
import sys
input = sys.stdin.readline
#from itertools import accumulate
#from itertools import combinations
N,K = list(map(int,input().split()))
P = list(map(int,input().split()))
C = list(map(int,input().split()))

#N,K = 4,4
#P = [2,3,4,1]
#C = [-9,9,1,-1]
#N = 5000
#K = 10**8
#P = [i+1 for i in range(1,5000)] + [1]
#C = [10**9] * N

visited = [False]*(N+1)
loops = []
for v in range(1,N+1):
    if visited[v]:
        continue
    l = [v]
    while True:
        v = l[-1]
        visited[v] = True
        nv = P[v-1]
        if visited[nv]:
            break
        l.append(nv)
    loops.append(l)

cand = []
for loop in loops:
    temp = 0
    vals = [C[i-1] for i in loop]
    l = len(vals)
    vals3 = vals+vals+vals[:-1]
    cumsum = [0]
    for v in vals3:
        cumsum.append(cumsum[-1] + v)
    gh = []
    #print(vals)
    if K <= l:
        temp = 0
        ma = min(vals)
        for left in range(0,2*l-K):
            for right in range(left+1,left+K+1):
                ma = max(ma,(cumsum[right] - cumsum[left]))
        temp += ma
    else:
        temp = 0
        s = sum(vals)
        q, r = divmod(K,l)
        q -= 1
        r += l
        ma = min(vals)
        for left in range(0,3*l-r):
            for right in range(left+1,left+r+1):
                ma = max(ma,(cumsum[right] - cumsum[left]))
        temp += ma
        if s > 0:
            temp += q * s
    cand.append(temp)
print((max(cand)))
# Original solution
from collections import defaultdict
INF = float('inf')
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = -INF
for init in range(1, N + 1):   # 初めの場所をiとする
    score = defaultdict(int)  # int/bool/list....
    visited = [-1] * (N + 1)
    visited[init] = 0  # 何回移動後に着いたか？
    i = init
    # max_init = -INF
    for k in range(1, K + 1):
        prev = i
        i = P[i]    # k回移動後に着くところ

        if visited[i] < 0:  # まだ訪れてなかった
            visited[i] = k
            score[i] = score[prev] + C[i]
            # max_init = max(max_init, score[i])
            max_score = max(max_score, score[i])
            continue

        loop_score = (score[prev] + C[i]) - score[i]
        loop_len = k - visited[i]

        score[i] = score[prev] + C[i]
        # max_init = max(max_init, score[i])
        max_score = max(max_score, score[i])

        if k == K:
            break # これ以上移動できない

        start = i   # ループを開始するノード
        loop_num = (K - k) // loop_len # ループをあと何回回れるか
        loop_rem = (K - k) % loop_len

        if loop_rem == 0:
            loop_num -= 1
            loop_rem = loop_len

        jj = start
        temp = 0
        temp_max = 0
        for l in range(loop_rem):
            jj = P[jj]
            temp += C[jj]
            temp_max = max(temp_max, temp)

        loop_max = temp_max + loop_num * loop_score
        # max_init = max(max_init, score[start] + loop_max)
        max_score = max(max_score, score[start] + loop_max)
        break

    # max_score = max(max_score, max_init)

if max_score == -INF:
    max_score = max(C[1:])

print(max_score)

# Optimized solution
from collections import defaultdict
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])

for init in range(1, N + 1):   # 初めの場所をiとする
    score = defaultdict(int)  # int/bool/list....
    visited = [-1] * (N + 1)
    visited[init] = 0  # 何回移動後に着いたか？
    i = init
    for k in range(1, K + 1):
        prev = i
        i = P[i]    # k回移動後に着くところ

        if visited[i] < 0:  # まだ訪れてなかった
            visited[i] = k
            score[i] = score[prev] + C[i]
            max_score = max(max_score, score[i])
            continue

        loop_score = (score[prev] + C[i]) - score[i]
        loop_len = k - visited[i]

        score[i] = score[prev] + C[i]
        # max_score = max(max_score, score[i])

        # if k == K:
        #     break # これ以上移動できない

        start = i   # ループを開始するノード
        loop_num = (K - k) // loop_len # ループをあと何回回れるか
        loop_rem = (K - k) % loop_len

        if loop_num > 0 and loop_rem == 0:
            loop_num -= 1
            loop_rem = loop_len

        jj = start
        temp = 0
        temp_max = 0
        for l in range(loop_rem):
            jj = P[jj]
            temp += C[jj]
            temp_max = max(temp_max, temp)

        loop_max = temp_max + loop_num * loop_score
        max_score = max(max_score, score[start] + loop_max)
        break

print(max_score)
# Original solution
from collections import defaultdict
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])

for init in range(1, N + 1):   # 初めの場所をiとする
    score = defaultdict(int)  # int/bool/list....
    visited = [-1] * (N + 1)
    visited[init] = 0  # 何回移動後に着いたか？
    i = init
    for k in range(1, K + 1):
        prev = i
        i = P[i]    # k回移動後に着くところ

        if visited[i] < 0:  # まだ訪れてなかった
            visited[i] = k
            score[i] = score[prev] + C[i]
            max_score = max(max_score, score[i])
            continue

        loop_score = (score[prev] + C[i]) - score[i]
        loop_len = k - visited[i]

        score[i] = score[prev] + C[i]

        loop_num = (K - k) // loop_len # ループをあと何回回れるか
        loop_rem = (K - k) % loop_len

        if loop_num > 0 and loop_rem == 0:
            loop_num -= 1
            loop_rem = loop_len

        j = i   # ループを開始するノードから
        temp = 0
        rem_max = 0
        for _ in range(loop_rem):
            j = P[j]
            temp += C[j]
            rem_max = max(rem_max, temp)

        max_score = max(max_score, score[i] + loop_num * loop_score + rem_max)
        break

print(max_score)

# Optimized solution
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])
for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score < 0:
                max_score = max(max_score, max(score[1:]))
            else:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break

print(max_score)
# Original solution
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])
for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score < 0:
                max_score = max(max_score, max(score[1:]))
            else:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break

print(max_score)

# Optimized solution
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])
for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break

print(max_score)
# Original solution
n, k = list(map(int,input().split()))
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), max(score), ans)

    #print('sum_cycle:', sum_cycle, 'cycle:', cycle, 'ans:', ans)
    #print(i+1, score)
print(ans)

# Optimized solution
n, k = list(map(int,input().split()))
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = -float('inf')
for i in range(n):
    score = []
    check = [0] * n
    now = i
    cycle = 0
    for _ in range(k):
        go = p[now] - 1
        if check[go] == 1:
            break
        now = go
        if len(score) == 0:
            score.append(c[now])
        else:
            score.append(score[-1] + c[now])
        check[now] = 1
        cycle += 1

    t = k // cycle
    m = k % cycle

    sum_cycle = score[-1]
    if m == 0:
        ans = max(sum_cycle * t, sum_cycle * (t - 1) + max(score), max(score), ans)
    else:
        ans = max(sum_cycle * t + max(score[:m]), sum_cycle * (t - 1) + max(score), max(score), ans)

print(ans)
# Original solution
n, k = list(map(int, input().split()))
# 先頭に0番要素を追加すれば、配列の要素番号と入力pが一致する
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

ans = -1e9*1e9
s = [None] * n
# 開始ノードをsi
for si in range(1, n+1):

    # 閉ループのスコアを取り出す
    x = si
    #s = list()  # ノードの各スコアを保存
    s_len = 0
    tot = 0     # ループ1周期のスコア
    while 1:
        x = p[x]
        #s.append(c[x])
        s[s_len] = c[x]
        s_len += 1
        tot += c[x]
        if (x == si): break # 始点に戻ったら終了
    
    # siを開始とする閉ループのスコア(s)を全探
    #s_len = len(s)
    t = 0
    for i in range(s_len):
        t += s[i]   # i回移動したときのスコア
        if (i+1 > k): break # 移動回数の上限を超えたら終了
        # 1周期のスコアが正ならば、可能な限り周回する
        if tot > 0:
            e = (k-(i+1))//s_len # 周回数
            now = t + tot*e      # i回移動と周回スコアの合計
        else:
            now = t
        ans = max(ans, now)

print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
# 先頭に0番要素を追加すれば、配列の要素番号と入力pが一致する
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

ans = -float('inf')
# 開始ノードをsi
for si in range(1, n+1):

    # 閉ループのスコアを取り出す
    x = si
    s = list()  # ノードの各スコアを保存
    #s_len = 0
    tot = 0     # ループ1周期のスコア
    while 1:
        x = p[x]
        s.append(c[x])
        #s[s_len] = c[x]
        #s_len += 1
        tot += c[x]
        if (x == si): break # 始点に戻ったら終了
    
    # siを開始とする閉ループのスコア(s)を全探
    s_len = len(s)
    t = 0
    for i in range(s_len):
        t += s[i]   # i回移動したときのスコア
        if (i+1 > k): break # 移動回数の上限を超えたら終了
        # 1周期のスコアが正ならば、可能な限り周回する
        if tot > 0:
            e = (k-(i+1))//s_len # 周回数
            now = t + tot*e      # i回移動と周回スコアの合計
        else:
            now = t
        ans = max(ans, now)

print(ans)
# Original solution
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []
lp_sum = []
range_s = []
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		lp_sum.append((lp_cum[-1][-1], len(lp[-1])))
		range_s.append(set())
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				range_s[-1].add((lp_cum[-1][x] - lp_cum[-1][y], x-y))
				if x < len(lp_cum[-1])-1 or y > 0:
					range_s[-1].add((lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y))

ok, ng = -10**9, 10**18+1

while ng-ok > 1:
	x = (ok+ng)//2
	flg = False
	for i in range(len(lp)):
		s_sum, s_num = lp_sum[i]
		if s_sum <= 0:
			for r_sum, r_num in range_s[i]:
				if r_sum >= x and r_num <= k:
					flg = True
					break
		else:
			for r_sum, r_num in range_s[i]:
				if r_num > k:
					continue
				if r_sum >= x:
					flg = True
					break
				rep = (x-1-r_sum) // s_sum + 1
				if rep * s_num + r_num <= k:
					flg = True
					break
	if flg:
		ok = x
	else:
		ng = x

print(ok)

# Optimized solution
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []

ans = -10**9
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		t_sum, t_num = lp_cum[-1][-1], len(lp[-1])
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				r_sum, r_num = lp_cum[-1][x] - lp_cum[-1][y], x-y
				if t_sum <= 0:
					if r_num <= k:
						ans = max(ans, r_sum)
				else:
					if r_num <= k:
						ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))
				if x < len(lp_cum[-1])-1 or y > 0:
					r_sum, r_num = lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y
					if t_sum <= 0:
						if r_num <= k:
							ans = max(ans, r_sum)
					else:
						if r_num <= k:
							ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))

print(ans)
# Original solution
N,K=list(map(int,input().split()))
P=list([int(x)-1 for x in input().split()])
C=list(map(int,input().split()))

dp = []
init = 0

if K<=N:
  ans = set()
  for i in range(N):
    tmp,now,last = [0],i,0
    for _ in range(K):
      last += C[P[now]]
      ans.add(last)
      now=P[now]
  print((max(ans)))
  exit()
ans = -10**9
for i in range(N):
  tmp,now,done = [0],i,set()
  for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
  tmp = tmp[1:]
  cnt = len(tmp)
  m = max(tmp)
  ans = max(ans,m,tmp[-1]*(K//cnt-1)+m)
  if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp)
print(ans)




# Optimized solution
N,K=list(map(int,input().split()))
P=list([int(x)-1 for x in input().split()])
C=list(map(int,input().split()))

if K<=N:
  ans = -10**9
  for i in range(N):
    now,last = i,0
    for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
  print(ans)
  exit()
ans = -10**9
for i in range(N):
  tmp,now,done = [0],i,set()
  for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
  tmp = tmp[1:]
  cnt = len(tmp)
  m = max(tmp)
  ans = max(ans,m,tmp[-1]*(K//cnt-1)+m)
  if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp)
print(ans)
# Original solution
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))

for i in range(n):
    p[i] -= 1
def score(a, k):
    now = p[a]
    s = c[now]
    cnt = 1
    l = [s]
    while now != a and cnt < k:
        now = p[now]
        s += c[now]
        l.append(s)
        cnt += 1
    if cnt == k:
        return max(l)
    if 2 * cnt >= k:
        s = (k // cnt - 1) * s
        k %= cnt
        k += cnt
    else:
        k -= cnt
    cnt = 0
    while cnt < k:
        now = p[now]
        s += c[now]
        l.append(s)
        cnt += 1
    return max(l)
print((max(score(i, k) for i in range(n))))

# Optimized solution
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))

for i in range(n):
    p[i] -= 1
def score(a, k):
    now = p[a]
    s = c[now]
    cnt = 1
    l = [s]
    while now != a and cnt < k:
        now = p[now]
        s += c[now]
        l.append(s)
        cnt += 1
    if cnt == k:
        return max(l)
    l.append((k // cnt - 1) * s + max(l))
    s = (k // cnt) * s
    k %= cnt
    cnt=0
    while cnt < k:
        now = p[now]
        s += c[now]
        l.append(s)
        cnt += 1
    return max(l)
print((max(score(i, k) for i in range(n))))
# Original solution
n,k=list(map(int,input().split()))
p=list([int(x)-1 for x in input().split()])
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[]
  for _ in range(k):
    i=p[i]
    x.append(c[i])
  anss=-10**20
  s=x[0]
  anss=max(anss,s)
  for i in x[1:]:
    s+=i
    anss=max(anss,s)
  ans=max(ans,anss)
print(ans)

# Optimized solution
def solve(n,x,k):
  ans=-10**20
  s=0
  sx=sum(x)
  for i in range(n):
    if (i+1)>k:break
    s+=x[i]
    d=((k-i-1)//n)*sx
    ans=max(ans,s+d,s)
  return ans

n,k=list(map(int,input().split()))
p=list([int(x)-1 for x in input().split()])
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[c[i]]
  j=p[i]
  while i!=j:
    x.append(c[j])
    j=p[j]
  x=x[1:]+[x[0]]
  ans=max(ans,solve(len(x),x,k))
print(ans)