n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
dic = {}
for i in range(1, n+1):
  dic[i] = [p[i-1], c[i-1]]
INF = 10**20
ans = -INF
for j in range(1, n+1):
  state = [True] * 5001
  max_score = -INF
  max_idx = 0
  score = 0
  now = dic[j][0]
  cnt = 0
  lst = []
  flg = True
  for _ in range(k):
    if not state[now]:
      flg = False
      break
    score += dic[now][1]
    cnt += 1
    if max_score < score:
      max_score = score
      max_idx = cnt
    lst.append(score)
    state[now] = False
    now = dic[now][0]
  if score <= 0 or flg:
    ans = max(max_score, ans)
  else:
    q = k // cnt
    r = k % cnt
    if r >= max_idx:
      x = score * q + max_score
      ans = max(ans, x)
    else:
      if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score)
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 dic = {} dic {} for i in range(1, n+1):
  dic[i] = [p[i-1], c[i-1]] i range(1, n+1) range 1 n+1 n 1 dic[i] = [p[i-1], c[i-1]] dic[i] dic i [p[i-1], c[i-1]] p[i-1] p i-1 i 1 c[i-1] c i-1 i 1 INF = 10**20 INF 10**20 10 20 ans = -INF ans -INF INF for j in range(1, n+1):
  state = [True] * 5001
  max_score = -INF
  max_idx = 0
  score = 0
  now = dic[j][0]
  cnt = 0
  lst = []
  flg = True
  for _ in range(k):
    if not state[now]:
      flg = False
      break
    score += dic[now][1]
    cnt += 1
    if max_score < score:
      max_score = score
      max_idx = cnt
    lst.append(score)
    state[now] = False
    now = dic[now][0]
  if score <= 0 or flg:
    ans = max(max_score, ans)
  else:
    q = k // cnt
    r = k % cnt
    if r >= max_idx:
      x = score * q + max_score
      ans = max(ans, x)
    else:
      if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score) j range(1, n+1) range 1 n+1 n 1 state = [True] * 5001 state [True] * 5001 [True] True 5001 max_score = -INF max_score -INF INF max_idx = 0 max_idx 0 score = 0 score 0 now = dic[j][0] now dic[j][0] [j] dic j 0 cnt = 0 cnt 0 lst = [] lst [] flg = True flg True for _ in range(k):
    if not state[now]:
      flg = False
      break
    score += dic[now][1]
    cnt += 1
    if max_score < score:
      max_score = score
      max_idx = cnt
    lst.append(score)
    state[now] = False
    now = dic[now][0]
   _ range(k) range k if not state[now]:
      flg = False
      break
     not state[now] state[now] state now flg = False flg False break score += dic[now][1] score dic[now][1] [now] dic now 1 cnt += 1 cnt 1 if max_score < score:
      max_score = score
      max_idx = cnt
     max_score < score max_score score max_score = score max_score score max_idx = cnt max_idx cnt lst.append(score) lst.append lst append score state[now] = False state[now] state now False now = dic[now][0] now dic[now][0] [now] dic now 0 if score <= 0 or flg:
    ans = max(max_score, ans)
  else:
    q = k // cnt
    r = k % cnt
    if r >= max_idx:
      x = score * q + max_score
      ans = max(ans, x)
    else:
      if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score) score <= 0 or flg score <= 0 score 0 flg ans = max(max_score, ans) ans max(max_score, ans) max max_score ans q = k // cnt q k // cnt k cnt r = k % cnt r k % cnt k cnt if r >= max_idx:
      x = score * q + max_score
      ans = max(ans, x)
    else:
      if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score) r >= max_idx r max_idx x = score * q + max_score x score * q + max_score score * q score q max_score ans = max(ans, x) ans max(ans, x) max ans x if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score) q >= 1 q 1 if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
       r == 0 r 0 x = score * (q - 1) + max_score x score * (q - 1) + max_score score * (q - 1) score q - 1 q 1 max_score ans = max(ans, x) ans max(ans, x) max ans x x = score * (q - 1) + max_score x score * (q - 1) + max_score score * (q - 1) score q - 1 q 1 max_score y = score * q + max(lst[:r]) y score * q + max(lst[:r]) score * q score q max(lst[:r]) max lst[:r] lst :r r ans = max(ans, x, y) ans max(ans, x, y) max ans x y ans = max(ans, max_score) ans max(ans, max_score) max ans max_score print(ans) print ans cnt = 0 0 cnt max_score = score score max_score x = score * (q - 1) + max_score score * (q - 1) + max_score x lst = [] [] lst x = score * q + max_score score * q + max_score x state = [True] * 5001 [True] * 5001 state ans = max(max_score, ans) max(max_score, ans) ans ans = max(ans, x) max(ans, x) ans p = list(map(int, input().split())) list(map(int, input().split())) p y = score * q + max(lst[:r]) score * q + max(lst[:r]) y r = k % cnt k % cnt r q = k // cnt k // cnt q score += dic[now][1] dic[now][1] score flg = False False flg ans = max(ans, x) max(ans, x) ans cnt += 1 1 cnt ans = max(ans, x, y) max(ans, x, y) ans k = map(int, input().split()) map(int, input().split()) k ans = max(ans, max_score) max(ans, max_score) ans ans = -INF -INF ans c = list(map(int, input().split())) list(map(int, input().split())) c flg = True True flg INF = 10**20 10**20 INF max_idx = 0 0 max_idx x = score * (q - 1) + max_score score * (q - 1) + max_score x now = dic[j][0] dic[j][0] now now = dic[now][0] dic[now][0] now max_idx = cnt cnt max_idx max_score = -INF -INF max_score score = 0 0 score dic = {} {} dic 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()))
dic = {}
for i in range(1, n+1):
  dic[i] = [p[i-1], c[i-1]]
INF = 10**20
ans = -INF
for j in range(1, n+1):
  state = [True] * 5001
  max_score = -INF
  max_idx = 0
  score = 0
  now = dic[j][0]
  cnt = 0
  lst = []
  flg = True
  for _ in range(k):
    if not state[now]:
      flg = False
      break
    score += dic[now][1]
    cnt += 1
    if max_score < score:
      max_score = score
      max_idx = cnt
    lst.append(score)
    state[now] = False
    now = dic[now][0]
  if score <= 0 or flg:
    ans = max(max_score, ans)
  else:
    q = k // cnt
    r = k % cnt
    if r >= max_idx:
      x = score * q + max_score
      ans = max(ans, x)
    else:
      if q >= 1:
        if r == 0:
          x = score * (q - 1) + max_score
          ans = max(ans, x)
        else:
          x = score * (q - 1) + max_score
          y = score * q + max(lst[:r])
          ans = max(ans, x, y)
      else:
        ans = max(ans, max_score)
print(ans)
    
