import sys

from collections import deque



N, K = list(map(int, sys.stdin.readline().split()))

P = list(map(int, sys.stdin.readline().split()))

C = list(map(int, sys.stdin.readline().split()))



# 重み付き有向グラフ

# edges = [[] for _ in range(N)]

# for i in range(N):

#     edges[i].append((P[i]-1, C[i]))



# DFSで閉路と経路の最大取得スコアを調査

ans = -float("inf")

for i in range(N):

    q = deque([(i, 0, 0)])

    cycle_score = -1

    cycle_count = -1

    cycle_start = None

    scores = {} # (score, count)

    while q:

        node1, score, count = q.popleft()

        # print(node1, score, count)

        if K < count:

            break

        if 0 < count: 

            ans = max(ans, score)

        if node1 in scores:

            # 閉路

            if score > 0:

                # print("plus cycle detected")

                # cycle.add((score, count, i))

                cycle_score = score

                cycle_count = count

                cycle_start = node1

            break

        scores[node1] = (score, count)

        # for で回しているが、実際は一択

        # for node2, weight in edges[node1]:

        #     q.appendleft((node2, score + weight, count + 1))

        node2 = P[node1] - 1

        weight = C[node2]

        q.append((node2, score + weight, count + 1))



    # cycleを回れる限界の-1から始めて、最大のスコアを算出

    if cycle_start is not None:

        cycle_len = cycle_count - scores[cycle_start][1]

        # print("cycle_len", cycle_len, "cycle_score", cycle_score - scores[cycle_start][0])

        loop = (K - scores[cycle_start][1]) // cycle_len - 1



        count = loop * cycle_len + scores[cycle_start][1]

        score = scores[cycle_start][0] + loop * (cycle_score - scores[cycle_start][0])

        # print(count, score, loop)

        q = deque([(cycle_start, score, count)])

        while q:

            node1, score, count = q.popleft()

            # print(node1, score, count)

            if count > K:

                break



            ans = max(ans, score)

            # for node2, weight in edges[node1]:

            #     q.appendleft((node2, score + weight, count + 1))

            node2 = P[node1] - 1

            weight = C[node2]

            q.append((node2, score + weight, count + 1))



print(ans)
import sys



input = sys.stdin.readline

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

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

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

 

ans = -float('inf')

for s in range(N):

    S = [] 

    i = P[s] - 1

    S.append(C[i])

    while i != s:

        i = P[i] - 1

        S.append(S[-1] + C[i])

 

    if K <= len(S):

        tmp = max(S[:K])

    elif S[-1] <= 0:

        tmp = max(S)

    else:

        # ループの中で最大となるところで止める

        tmp1 = S[-1] * (K // len(S) - 1)

        tmp1 += max(S)

        # 限界まで進む

        tmp2 = S[-1] * (K // len(S))

        r = K % len(S)

        if r != 0:

            tmp2 += max(0, max(S[:r]))

        tmp = max(tmp1, tmp2)

 

    ans = max(ans, tmp)

 

print(ans)
import sys
from collections import deque

N, K = list(map(int, sys.stdin.readline().split()))
P = list(map(int, sys.stdin.readline().split()))
C = list(map(int, sys.stdin.readline().split()))
# 重み付き有向グラフ
# edges = [[] for _ in range(N)]
# for i in range(N):
#     edges[i].append((P[i]-1, C[i]))
# DFSで閉路と経路の最大取得スコアを調査
ans = -float("inf")
for i in range(N):
    q = deque([(i, 0, 0)])
    cycle_score = -1
    cycle_count = -1
    cycle_start = None
    scores = {}  # (score, count)
    while q:
        node1, score, count = q.popleft()
        # print(node1, score, count)
        if K < count:
            break
        if 0 < count:
            ans = max(ans, score)
        if node1 in scores:
            # 閉路
            if score > 0:
                # print("plus cycle detected")
                # cycle.add((score, count, i))
                cycle_score = score
                cycle_count = count
                cycle_start = node1
            break
        scores[node1] = (score, count)
        # for で回しているが、実際は一択
        # for node2, weight in edges[node1]:
        #     q.appendleft((node2, score + weight, count + 1))
        node2 = P[node1] - 1
        weight = C[node2]
        q.append((node2, score + weight, count + 1))
    # cycleを回れる限界の-1から始めて、最大のスコアを算出
    if cycle_start is not None:
        cycle_len = cycle_count - scores[cycle_start][1]
        # print("cycle_len", cycle_len, "cycle_score", cycle_score - scores[cycle_start][0])
        loop = (K - scores[cycle_start][1]) // cycle_len - 1
        count = loop * cycle_len + scores[cycle_start][1]
        score = scores[cycle_start][0] + loop * (cycle_score - scores[cycle_start][0])
        # print(count, score, loop)
        q = deque([(cycle_start, score, count)])
        while q:
            node1, score, count = q.popleft()
            # print(node1, score, count)
            if count > K:
                break
            ans = max(ans, score)
            # for node2, weight in edges[node1]:
            #     q.appendleft((node2, score + weight, count + 1))
            node2 = P[node1] - 1
            weight = C[node2]
            q.append((node2, score + weight, count + 1))
print(ans)

import sys

input = sys.stdin.readline
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = -float("inf")
for s in range(N):
    S = []
    i = P[s] - 1
    S.append(C[i])
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
    if K <= len(S):
        tmp = max(S[:K])
    elif S[-1] <= 0:
        tmp = max(S)
    else:
        # ループの中で最大となるところで止める
        tmp1 = S[-1] * (K // len(S) - 1)
        tmp1 += max(S)
        # 限界まで進む
        tmp2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            tmp2 += max(0, max(S[:r]))
        tmp = max(tmp1, tmp2)
    ans = max(ans, tmp)
print(ans)

-from collections import deque -N, K = list(map(int, sys.stdin.readline().split())) -P = list(map(int, sys.stdin.readline().split())) -C = list(map(int, sys.stdin.readline().split())) -# 重み付き有向グラフ -# edges = [[] for _ in range(N)] -# for i in range(N): -#     edges[i].append((P[i]-1, C[i])) -# DFSで閉路と経路の最大取得スコアを調査 +input = sys.stdin.readline +N, K = list(map(int, input().split())) +P = list(map(int, input().split())) +C = list(map(int, input().split())) -for i in range(N): -    q = deque([(i, 0, 0)]) -    cycle_score = -1 -    cycle_count = -1 -    cycle_start = None -    scores = {}  # (score, count) -    while q: -        node1, score, count = q.popleft() -        # print(node1, score, count) -        if K < count: -            break -        if 0 < count: -            ans = max(ans, score) -        if node1 in scores: -            # 閉路 -            if score > 0: -                # print("plus cycle detected") -                # cycle.add((score, count, i)) -                cycle_score = score -                cycle_count = count -                cycle_start = node1 -            break -        scores[node1] = (score, count) -        # for で回しているが、実際は一択 -        # for node2, weight in edges[node1]: -        #     q.appendleft((node2, score + weight, count + 1)) -        node2 = P[node1] - 1 -        weight = C[node2] -        q.append((node2, score + weight, count + 1)) -    # cycleを回れる限界の-1から始めて、最大のスコアを算出 -    if cycle_start is not None: -        cycle_len = cycle_count - scores[cycle_start][1] -        # print("cycle_len", cycle_len, "cycle_score", cycle_score - scores[cycle_start][0]) -        loop = (K - scores[cycle_start][1]) // cycle_len - 1 -        count = loop * cycle_len + scores[cycle_start][1] -        score = scores[cycle_start][0] + loop * (cycle_score - scores[cycle_start][0]) -        # print(count, score, loop) -        q = deque([(cycle_start, score, count)]) -        while q: -            node1, score, count = q.popleft() -            # print(node1, score, count) -            if count > K: -                break -            ans = max(ans, score) -            # for node2, weight in edges[node1]: -            #     q.appendleft((node2, score + weight, count + 1)) -            node2 = P[node1] - 1 -            weight = C[node2] -            q.append((node2, score + weight, count + 1)) +for s in range(N): +    S = [] +    i = P[s] - 1 +    S.append(C[i]) +    while i != s: +        i = P[i] - 1 +        S.append(S[-1] + C[i]) +    if K <= len(S): +        tmp = max(S[:K]) +    elif S[-1] <= 0: +        tmp = max(S) +    else: +        # ループの中で最大となるところで止める +        tmp1 = S[-1] * (K // len(S) - 1) +        tmp1 += max(S) +        # 限界まで進む +        tmp2 = S[-1] * (K // len(S)) +        r = K % len(S) +        if r != 0: +            tmp2 += max(0, max(S[:r])) +        tmp = max(tmp1, tmp2) +    ans = max(ans, tmp)