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

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)

-INF = float("inf") -max_score = -INF +max_score = max(C[1:]) -    # max_init = -INF -            # max_init = max(max_init, score[i]) -        # max_init = max(max_init, score[i]) -        max_score = max(max_score, score[i]) -        if k == K: -            break  # これ以上移動できない +        # max_score = max(max_score, score[i]) +        # if k == K: +        #     break # これ以上移動できない -        if loop_rem == 0: +        if loop_num > 0 and loop_rem == 0: -        # max_init = max(max_init, score[start] + loop_max) -    # max_score = max(max_score, max_init) -if max_score == -INF: -    max_score = max(C[1:])