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

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

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

r = -10**9

for i in range(n):

  ans=c[i]

  e =[ans]

  f = i

  a = 1

  while a<k:

    i = p[i]-1

    if i == f:

      break

    a+=1

    ans+=c[i]

    e.append(ans)

  if e[-1]>0 and a<k:

    w = k%len(e)

    if w!=0:

      y = k//len(e)*e[-1]+max(e[:w])

    else:

      y =( k//len(e)-1)*e[-1]+max(e)

    g=y

  else:

    g=max(e)

  r = max(r,g)

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

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

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

g = []

for i in range(n):

  ans=c[i]

  e =[ans]

  f = i

  a = 1

  while a<k:

    i = p[i]-1

    if i == f:

      break

    a+=1

    ans+=c[i]

    e.append(ans)

  if e[-1]>0 and a<k:

    w = k%len(e)

    if w!=0:

      y = k//len(e)*e[-1]+max(e[:w])

    else:

      y =( k//len(e)-1)*e[-1]+max(e)

    g.append(y)

  else:

    g.append(max(e))

print((max(g)))

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
r = -(10**9)
for i in range(n):
    ans = c[i]
    e = [ans]
    f = i
    a = 1
    while a < k:
        i = p[i] - 1
        if i == f:
            break
        a += 1
        ans += c[i]
        e.append(ans)
    if e[-1] > 0 and a < k:
        w = k % len(e)
        if w != 0:
            y = k // len(e) * e[-1] + max(e[:w])
        else:
            y = (k // len(e) - 1) * e[-1] + max(e)
        g = y
    else:
        g = max(e)
    r = max(r, g)
print(r)

n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
g = []
for i in range(n):
    ans = c[i]
    e = [ans]
    f = i
    a = 1
    while a < k:
        i = p[i] - 1
        if i == f:
            break
        a += 1
        ans += c[i]
        e.append(ans)
    if e[-1] > 0 and a < k:
        w = k % len(e)
        if w != 0:
            y = k // len(e) * e[-1] + max(e[:w])
        else:
            y = (k // len(e) - 1) * e[-1] + max(e)
        g.append(y)
    else:
        g.append(max(e))
print((max(g)))

-r = -(10**9) +g = [] -        g = y +        g.append(y) -        g = max(e) -    r = max(r, g) -print(r) +        g.append(max(e)) +print((max(g)))