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