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

P, C = [list(map(int, input().split())) for _ in range(2)]

c = [1] * N

m = [[0] * (N+1) for i in range(N)]

for i in range(N):

    m[i][1] = (C[P[i]-1])

ans = max(C)

for i in range(N):

    j = i

    while P[j]-1 != i:

        j = P[j]-1

        c[i] += 1

        m[i][c[i]] = m[i][c[i]-1] + C[P[j]-1]

    if m[i][c[i]] > 0:

        ans = max(ans, m[i][c[i]] * (K//c[i]-1) + max(m[i] + [m[i][c[i]] + m[i][j+1] for j in range(K%c[i])]))

    else:

        ans = max(ans, max(m[i][1:min(c[i],K)+1]))

print(ans)

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

P, C = [list(map(int, input().split())) for _ in range(2)]

ans = max(C)

for i in range(N):

    m = [0] * (N+1)

    k = i

    for j in range(N):

        m[j+1] = m[j] + C[k]

        k = P[k]-1

        if k == i:

            break

    if m[j+1] > 0:

        ans = max(ans, m[j+1] * (K//(j+1)-1) + max(m + [m[j+1] + m[k+1] for k in range(K%(j+1))]))

    else:

        ans = max(ans, max(m[1:min(j+1,K)+1]))

print(ans)

N, K = list(map(int, input().split()))
P, C = [list(map(int, input().split())) for _ in range(2)]
c = [1] * N
m = [[0] * (N + 1) for i in range(N)]
for i in range(N):
    m[i][1] = C[P[i] - 1]
ans = max(C)
for i in range(N):
    j = i
    while P[j] - 1 != i:
        j = P[j] - 1
        c[i] += 1
        m[i][c[i]] = m[i][c[i] - 1] + C[P[j] - 1]
    if m[i][c[i]] > 0:
        ans = max(
            ans,
            m[i][c[i]] * (K // c[i] - 1)
            + max(m[i] + [m[i][c[i]] + m[i][j + 1] for j in range(K % c[i])]),
        )
    else:
        ans = max(ans, max(m[i][1 : min(c[i], K) + 1]))
print(ans)

N, K = list(map(int, input().split()))
P, C = [list(map(int, input().split())) for _ in range(2)]
ans = max(C)
for i in range(N):
    m = [0] * (N + 1)
    k = i
    for j in range(N):
        m[j + 1] = m[j] + C[k]
        k = P[k] - 1
        if k == i:
            break
    if m[j + 1] > 0:
        ans = max(
            ans,
            m[j + 1] * (K // (j + 1) - 1)
            + max(m + [m[j + 1] + m[k + 1] for k in range(K % (j + 1))]),
        )
    else:
        ans = max(ans, max(m[1 : min(j + 1, K) + 1]))
print(ans)

-c = [1] * N -m = [[0] * (N + 1) for i in range(N)] -for i in range(N): -    m[i][1] = C[P[i] - 1] -    j = i -    while P[j] - 1 != i: -        j = P[j] - 1 -        c[i] += 1 -        m[i][c[i]] = m[i][c[i] - 1] + C[P[j] - 1] -    if m[i][c[i]] > 0: +    m = [0] * (N + 1) +    k = i +    for j in range(N): +        m[j + 1] = m[j] + C[k] +        k = P[k] - 1 +        if k == i: +            break +    if m[j + 1] > 0: -            m[i][c[i]] * (K // c[i] - 1) -            + max(m[i] + [m[i][c[i]] + m[i][j + 1] for j in range(K % c[i])]), +            m[j + 1] * (K // (j + 1) - 1) +            + max(m + [m[j + 1] + m[k + 1] for k in range(K % (j + 1))]), -        ans = max(ans, max(m[i][1 : min(c[i], K) + 1])) +        ans = max(ans, max(m[1 : min(j + 1, K) + 1]))