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

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

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



ans = -1*10**9

for si in range(N):

  x = si

  total = 0

  l = 0

  s = list()

  while(1):

    x = p[x]-1

    s.append(c[x])

    l += 1

    total += c[x]

    if x == si:

      break

  t = 0

  for i in range(l):

    now = 0

    if (i+1) > K:

      break

    t += s[i]

    now += t

    if total > 0:

      e = (K-(i+1)) // l

      now += total * e

    ans = max(ans, now)

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

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

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

ans = -1* 10**9



for si in range(N):

  s = list()

  total = 0

  l = 0

  x = si

  while(1):

    x = p[x] - 1

    s.append(c[x])

    total += c[x]

    l += 1

    if x == si:

      break

  t = 0

  for j in range(l):

    t += s[j]

    now = t

    if j >= K:

      break

    if total > 0:

      e = (K-j-1)//l

      now += total * e

    ans = max(ans, now)



print(ans)
N, K = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -1 * 10**9
for si in range(N):
    x = si
    total = 0
    l = 0
    s = list()
    while 1:
        x = p[x] - 1
        s.append(c[x])
        l += 1
        total += c[x]
        if x == si:
            break
    t = 0
    for i in range(l):
        now = 0
        if (i + 1) > K:
            break
        t += s[i]
        now += t
        if total > 0:
            e = (K - (i + 1)) // l
            now += total * e
        ans = max(ans, now)
print(ans)

N, K = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = -1 * 10**9
for si in range(N):
    s = list()
    total = 0
    l = 0
    x = si
    while 1:
        x = p[x] - 1
        s.append(c[x])
        total += c[x]
        l += 1
        if x == si:
            break
    t = 0
    for j in range(l):
        t += s[j]
        now = t
        if j >= K:
            break
        if total > 0:
            e = (K - j - 1) // l
            now += total * e
        ans = max(ans, now)
print(ans)

-    x = si +    s = list() -    s = list() +    x = si +        total += c[x] -        total += c[x] -    for i in range(l): -        now = 0 -        if (i + 1) > K: +    for j in range(l): +        t += s[j] +        now = t +        if j >= K: -        t += s[i] -        now += t -            e = (K - (i + 1)) // l +            e = (K - j - 1) // l