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**9-1

for i in range(N):

    seen = [-1]*N

    cumsum = [0]*(N+1)

    now = i

    j = 0

    while seen[now] == -1:

        seen[now] = 1

        now = P[now]

        cumsum[j+1] = cumsum[j] + C[now]

        j += 1

    loop_size = j

    if K <= loop_size:

        ans = max([ans] + cumsum[1:K+1])

    else:

        if cumsum[loop_size] > 0:

            loop_n = K//loop_size - 1

            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]

            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])

            ans = max(ans, score)

        else:

            ans = max([ans] + cumsum[1:loop_size+1])

print(ans)
from itertools import accumulate 



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**18-1

seen = [False]*N

for i in range(N):

    if seen[i]:

        continue

    loop = []

    now = i

    while seen[now] == False:

        seen[now] = True

        loop.append(C[now])

        now = P[now]

    loop_size = len(loop)

    loop_score = sum(loop)

    cumsum = [0]+list(accumulate(loop+loop))

    for j in range(1, min(K, loop_size)+1):

        for l in range(loop_size+1):

            r = l+j

            subseq = cumsum[r] - cumsum[l]

            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size))

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 = -(10**9) - 1
for i in range(N):
    seen = [-1] * N
    cumsum = [0] * (N + 1)
    now = i
    j = 0
    while seen[now] == -1:
        seen[now] = 1
        now = P[now]
        cumsum[j + 1] = cumsum[j] + C[now]
        j += 1
    loop_size = j
    if K <= loop_size:
        ans = max([ans] + cumsum[1 : K + 1])
    else:
        if cumsum[loop_size] > 0:
            loop_n = K // loop_size - 1
            cumsum2 = cumsum[0 : loop_size + 1] + [
                x + cumsum[loop_size] for x in cumsum[1 : loop_size + 1]
            ]
            score = cumsum[loop_size] * loop_n + max(
                cumsum2[0 : loop_size + K % loop_size + 1]
            )
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1 : loop_size + 1])
print(ans)

from itertools import accumulate

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**18) - 1
seen = [False] * N
for i in range(N):
    if seen[i]:
        continue
    loop = []
    now = i
    while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
    loop_size = len(loop)
    loop_score = sum(loop)
    cumsum = [0] + list(accumulate(loop + loop))
    for j in range(1, min(K, loop_size) + 1):
        for l in range(loop_size + 1):
            r = l + j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq + loop_score * ((K - j) // loop_size))
print(ans)

+from itertools import accumulate + -ans = -(10**9) - 1 +ans = -(10**18) - 1 +seen = [False] * N -    seen = [-1] * N -    cumsum = [0] * (N + 1) +    if seen[i]: +        continue +    loop = [] -    j = 0 -    while seen[now] == -1: -        seen[now] = 1 +    while seen[now] == False: +        seen[now] = True +        loop.append(C[now]) -        cumsum[j + 1] = cumsum[j] + C[now] -        j += 1 -    loop_size = j -    if K <= loop_size: -        ans = max([ans] + cumsum[1 : K + 1]) -    else: -        if cumsum[loop_size] > 0: -            loop_n = K // loop_size - 1 -            cumsum2 = cumsum[0 : loop_size + 1] + [ -                x + cumsum[loop_size] for x in cumsum[1 : loop_size + 1] -            ] -            score = cumsum[loop_size] * loop_n + max( -                cumsum2[0 : loop_size + K % loop_size + 1] -            ) -            ans = max(ans, score) -        else: -            ans = max([ans] + cumsum[1 : loop_size + 1]) +    loop_size = len(loop) +    loop_score = sum(loop) +    cumsum = [0] + list(accumulate(loop + loop)) +    for j in range(1, min(K, loop_size) + 1): +        for l in range(loop_size + 1): +            r = l + j +            subseq = cumsum[r] - cumsum[l] +            ans = max(ans, subseq, subseq + loop_score * ((K - j) // loop_size))