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)
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)
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)

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)

-from collections import defaultdict - -for init in range(1, N + 1):  # 初めの場所をiとする -    score = defaultdict(int)  # int/bool/list.... -    visited = [-1] * (N + 1) -    visited[init] = 0  # 何回移動後に着いたか？ +for init in range(1, N + 1):  # 初めの場所をinitとする +    score = [0]  # k回移動後のスコア -        prev = i -        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 +        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