n,k = list(map(int,input().split()))

p = list(map(int,input().split()))

for i in range(n):

    p[i] -= 1

c = list(map(int,input().split()))



reach = [False]*n

x = []

for i in range(n):

    if reach[i]:continue

    buf = []

    cur = i

    while not reach[cur]:

        buf.append(c[cur])

        reach[cur] = True

        cur = p[cur]

    x.append(buf)



ans = max(c)

import itertools

for i in x:

    n = k//len(i)

    ind = k%len(i)

    if n>0:

        n -= 1

        ind += len(i)

    else:

        ind = min(k,len(i)*2)

    ii = i+i+i

    r = list(itertools.accumulate(ii))

    a = max(0,r[len(i)-1]*n)

    for j in range(len(i)):

        for z in range(1,ind+1):

            s = r[j+z]-r[j]

            if z<ind+1:ans = max(ans,a+s)

            else:ans = max(ans,s)

print(ans)

n,k = list(map(int,input().split()))

p = list(map(int,input().split()))

for i in range(n):

    p[i] -= 1

c = list(map(int,input().split()))



reach = [False]*n

x = []

for i in range(n):

    if reach[i]:continue

    buf = []

    cur = i

    while not reach[cur]:

        buf.append(c[cur])

        reach[cur] = True

        cur = p[cur]

    x.append(buf)



ans = max(c)

import itertools

for i in x:

    n = k//len(i)

    ind = k%len(i)

    if n>0:

        n -= 1

        ind += len(i)

    else:

        ind = min(k,len(i)*2)

    ii = i+i+i

    r = list(itertools.accumulate(ii))

    a = max(0,r[len(i)-1]*n)

    for j in range(len(i)):

        for z in range(1,ind+1):

            s = r[j+z]-r[j]

            ans = max(ans,a+s)

print(ans)

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
for i in range(n):
    p[i] -= 1
c = list(map(int, input().split()))
reach = [False] * n
x = []
for i in range(n):
    if reach[i]:
        continue
    buf = []
    cur = i
    while not reach[cur]:
        buf.append(c[cur])
        reach[cur] = True
        cur = p[cur]
    x.append(buf)
ans = max(c)
import itertools

for i in x:
    n = k // len(i)
    ind = k % len(i)
    if n > 0:
        n -= 1
        ind += len(i)
    else:
        ind = min(k, len(i) * 2)
    ii = i + i + i
    r = list(itertools.accumulate(ii))
    a = max(0, r[len(i) - 1] * n)
    for j in range(len(i)):
        for z in range(1, ind + 1):
            s = r[j + z] - r[j]
            if z < ind + 1:
                ans = max(ans, a + s)
            else:
                ans = max(ans, s)
print(ans)

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
for i in range(n):
    p[i] -= 1
c = list(map(int, input().split()))
reach = [False] * n
x = []
for i in range(n):
    if reach[i]:
        continue
    buf = []
    cur = i
    while not reach[cur]:
        buf.append(c[cur])
        reach[cur] = True
        cur = p[cur]
    x.append(buf)
ans = max(c)
import itertools

for i in x:
    n = k // len(i)
    ind = k % len(i)
    if n > 0:
        n -= 1
        ind += len(i)
    else:
        ind = min(k, len(i) * 2)
    ii = i + i + i
    r = list(itertools.accumulate(ii))
    a = max(0, r[len(i) - 1] * n)
    for j in range(len(i)):
        for z in range(1, ind + 1):
            s = r[j + z] - r[j]
            ans = max(ans, a + s)
print(ans)

-            if z < ind + 1: -                ans = max(ans, a + s) -            else: -                ans = max(ans, s) +            ans = max(ans, a + s)