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**29

for start in range(N):

    now = start

    point = 0

    for j in range(K):

        now = P[now]

        point += C[now]

        ans = max(ans, point)

        if now == start:

            break

    loop_cnt = j+1

    left_k = K - loop_cnt

    if point < 0: continue

    tmp = point

    point *= left_k // loop_cnt + 1

    point -= tmp

    ans = max(ans, point)

    for j in range(left_k%loop_cnt+loop_cnt):

        now = P[now]

        point += C[now]

        ans = max(ans, point)



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



move_db = [[0] * N for _ in range(65)]

point_db = [[0] * N for _ in range(65)]

maxpoint_db = [[0] * N for _ in range(65)]



for i, (p, c) in enumerate(zip(P, C)):

    move_db[0][i] = p

    point_db[0][i] = c

    maxpoint_db[0][i] = c



for i in range(40):

    for j in range(N):

        move_db[i+1][j] = move_db[i][move_db[i][j]]

        point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j]

        maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]])



res = -10 ** 19

for i in range(N):

    tmp, now = 0, i

    for d in range(32):

        if K&(1<<d):

            res = max(res, tmp + maxpoint_db[d][now])

            tmp += point_db[d][now]

            now = move_db[d][now]



print(res)

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**29)
for start in range(N):
    now = start
    point = 0
    for j in range(K):
        now = P[now]
        point += C[now]
        ans = max(ans, point)
        if now == start:
            break
    loop_cnt = j + 1
    left_k = K - loop_cnt
    if point < 0:
        continue
    tmp = point
    point *= left_k // loop_cnt + 1
    point -= tmp
    ans = max(ans, point)
    for j in range(left_k % loop_cnt + loop_cnt):
        now = P[now]
        point += C[now]
        ans = max(ans, point)
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()))
move_db = [[0] * N for _ in range(65)]
point_db = [[0] * N for _ in range(65)]
maxpoint_db = [[0] * N for _ in range(65)]
for i, (p, c) in enumerate(zip(P, C)):
    move_db[0][i] = p
    point_db[0][i] = c
    maxpoint_db[0][i] = c
for i in range(40):
    for j in range(N):
        move_db[i + 1][j] = move_db[i][move_db[i][j]]
        point_db[i + 1][j] = point_db[i][move_db[i][j]] + point_db[i][j]
        maxpoint_db[i + 1][j] = max(
            maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]]
        )
res = -(10**19)
for i in range(N):
    tmp, now = 0, i
    for d in range(32):
        if K & (1 << d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now]
print(res)

-ans = -(10**29) -for start in range(N): -    now = start -    point = 0 -    for j in range(K): -        now = P[now] -        point += C[now] -        ans = max(ans, point) -        if now == start: -            break -    loop_cnt = j + 1 -    left_k = K - loop_cnt -    if point < 0: -        continue -    tmp = point -    point *= left_k // loop_cnt + 1 -    point -= tmp -    ans = max(ans, point) -    for j in range(left_k % loop_cnt + loop_cnt): -        now = P[now] -        point += C[now] -        ans = max(ans, point) -print(ans) +move_db = [[0] * N for _ in range(65)] +point_db = [[0] * N for _ in range(65)] +maxpoint_db = [[0] * N for _ in range(65)] +for i, (p, c) in enumerate(zip(P, C)): +    move_db[0][i] = p +    point_db[0][i] = c +    maxpoint_db[0][i] = c +for i in range(40): +    for j in range(N): +        move_db[i + 1][j] = move_db[i][move_db[i][j]] +        point_db[i + 1][j] = point_db[i][move_db[i][j]] + point_db[i][j] +        maxpoint_db[i + 1][j] = max( +            maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]] +        ) +res = -(10**19) +for i in range(N): +    tmp, now = 0, i +    for d in range(32): +        if K & (1 << d): +            res = max(res, tmp + maxpoint_db[d][now]) +            tmp += point_db[d][now] +            now = move_db[d][now] +print(res)