n, k = map(int, input().split())
pp = list(map(int, input().split()))
p = [pp[i]-1 for i in range(n)]
c = list(map(int, input().split()))

ans = -(10**9+1)
for i in range(n):
  s = 0
  x = i
  for j in range(k):
    x = p[x]
    s += c[x]
    ans = max(ans,s)
    if i == x:
      break
  a = int(k/(j+1))
  m = k%(j+1)
  if m == 0 and a > 1:
    m = j+1
    a -= 1
  s *= a
  ans = max(ans,s)
  for l in range(m):
    x = p[x]
    s += c[x]
    ans = max(ans, s)
print (ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k pp = list(map(int, input().split())) pp list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split pp[i]-1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) pp[i]-1 pp[i] pp i 1 p = [pp[i]-1 for i in range(n)] p [pp[i]-1 for i in range(n)] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -(10**9+1) ans -(10**9+1) 10**9+1 10**9 10 9 1 for i in range(n):
  s = 0
  x = i
  for j in range(k):
    x = p[x]
    s += c[x]
    ans = max(ans,s)
    if i == x:
      break
  a = int(k/(j+1))
  m = k%(j+1)
  if m == 0 and a > 1:
    m = j+1
    a -= 1
  s *= a
  ans = max(ans,s)
  for l in range(m):
    x = p[x]
    s += c[x]
    ans = max(ans, s) i range(n) range n s = 0 s 0 x = i x i for j in range(k):
    x = p[x]
    s += c[x]
    ans = max(ans,s)
    if i == x:
      break
   j range(k) range k x = p[x] x p[x] p x s += c[x] s c[x] c x ans = max(ans,s) ans max(ans,s) max ans s if i == x:
      break
   i == x i x break a = int(k/(j+1)) a int(k/(j+1)) int k/(j+1) k j+1 j 1 m = k%(j+1) m k%(j+1) k j+1 j 1 if m == 0 and a > 1:
    m = j+1
    a -= 1
   m == 0 and a > 1 m == 0 m 0 a > 1 a 1 m = j+1 m j+1 j 1 a -= 1 a 1 s *= a s a ans = max(ans,s) ans max(ans,s) max ans s for l in range(m):
    x = p[x]
    s += c[x]
    ans = max(ans, s) l range(m) range m x = p[x] x p[x] p x s += c[x] s c[x] c x ans = max(ans, s) ans max(ans, s) max ans s print (ans) print ans m = k%(j+1) k%(j+1) m pp = list(map(int, input().split())) list(map(int, input().split())) pp s += c[x] c[x] s a = int(k/(j+1)) int(k/(j+1)) a n, k = map(int, input().split()) map(int, input().split()) n c = list(map(int, input().split())) list(map(int, input().split())) c x = i i x ans = max(ans,s) max(ans,s) ans p = [pp[i]-1 for i in range(n)] [pp[i]-1 for i in range(n)] p k = map(int, input().split()) map(int, input().split()) k ans = -(10**9+1) -(10**9+1) ans ans = max(ans,s) max(ans,s) ans x = p[x] p[x] x s += c[x] c[x] s s *= a a s a -= 1 1 a s = 0 0 s x = p[x] p[x] x ans = max(ans, s) max(ans, s) ans m = j+1 j+1 m
n, k = map(int, input().split())
pp = list(map(int, input().split()))
p = [pp[i]-1 for i in range(n)]
c = list(map(int, input().split()))

ans = -(10**9+1)
for i in range(n):
  s = 0
  x = i
  for j in range(k):
    x = p[x]
    s += c[x]
    ans = max(ans,s)
    if i == x:
      break
  a = int(k/(j+1))
  m = k%(j+1)
  if m == 0 and a > 1:
    m = j+1
    a -= 1
  s *= a
  ans = max(ans,s)
  for l in range(m):
    x = p[x]
    s += c[x]
    ans = max(ans, s)
print (ans)
