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

P=list([int(x)-1 for x in input().split()])

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



DP=[[C[P[n]]] for n in range(N)]



for start in range(N):

    visited=set()

    cur=P[start]

    while cur not in visited:

        visited.add(cur)

        cur = P[cur]

        DP[start].append(C[cur]+DP[start][-1])





for n in range(N):

    DP[n]=DP[n][:-1]



ans=-float("inf")



for start in range(N):

    for goal in range(min(K,len(DP[start]))):

        no_loop_sum=DP[start][goal]

        remain_hops=K-goal-1

        if DP[start][-1]>0:

            loops=remain_hops//len(DP[start])

        else:

            loops=0

        ans=max(ans,no_loop_sum+DP[start][-1]*loops)

print(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=-float("inf")



for start in range(N):

    cur=P[start]

    scores=[C[cur]]

    for _ in range(K):

        if cur==start:

            break

        cur = P[cur]

        scores.append(C[cur]+scores[-1])



    for goal in range(len(scores)):

        no_loop_sum=scores[goal]

        remain_hops=K-goal-1



        if scores[-1]>0:

            loops=remain_hops//len(scores)

        else:

            loops=0

        ans=max(ans,no_loop_sum+scores[-1]*loops)

print(ans)

N, K = list(map(int, input().split()))
P = list([int(x) - 1 for x in input().split()])
C = list(map(int, input().split()))
DP = [[C[P[n]]] for n in range(N)]
for start in range(N):
    visited = set()
    cur = P[start]
    while cur not in visited:
        visited.add(cur)
        cur = P[cur]
        DP[start].append(C[cur] + DP[start][-1])
for n in range(N):
    DP[n] = DP[n][:-1]
ans = -float("inf")
for start in range(N):
    for goal in range(min(K, len(DP[start]))):
        no_loop_sum = DP[start][goal]
        remain_hops = K - goal - 1
        if DP[start][-1] > 0:
            loops = remain_hops // len(DP[start])
        else:
            loops = 0
        ans = max(ans, no_loop_sum + DP[start][-1] * loops)
print(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 = -float("inf")
for start in range(N):
    cur = P[start]
    scores = [C[cur]]
    for _ in range(K):
        if cur == start:
            break
        cur = P[cur]
        scores.append(C[cur] + scores[-1])
    for goal in range(len(scores)):
        no_loop_sum = scores[goal]
        remain_hops = K - goal - 1
        if scores[-1] > 0:
            loops = remain_hops // len(scores)
        else:
            loops = 0
        ans = max(ans, no_loop_sum + scores[-1] * loops)
print(ans)

-DP = [[C[P[n]]] for n in range(N)] -for start in range(N): -    visited = set() -    cur = P[start] -    while cur not in visited: -        visited.add(cur) -        cur = P[cur] -        DP[start].append(C[cur] + DP[start][-1]) -for n in range(N): -    DP[n] = DP[n][:-1] -    for goal in range(min(K, len(DP[start]))): -        no_loop_sum = DP[start][goal] +    cur = P[start] +    scores = [C[cur]] +    for _ in range(K): +        if cur == start: +            break +        cur = P[cur] +        scores.append(C[cur] + scores[-1]) +    for goal in range(len(scores)): +        no_loop_sum = scores[goal] -        if DP[start][-1] > 0: -            loops = remain_hops // len(DP[start]) +        if scores[-1] > 0: +            loops = remain_hops // len(scores) -        ans = max(ans, no_loop_sum + DP[start][-1] * loops) +        ans = max(ans, no_loop_sum + scores[-1] * loops)