n, k = map(int, input().split())
positions = [int(i) for i in input().split()]
scores = [int(i) for i in input().split()]
if max(scores) <= 0:
  print(max(scores))
else:
  positions = [0] + positions
  scores = [0] + scores
  max_point = -pow(10, 9)
  #print(max_point)
  for i in range(1, n + 1):
    s = []
    f_pos = positions[i]
    score = scores[f_pos]
    s.append(score)
    while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
    loop = len(s)
    if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
    max_point = max(max_point, point)
  print(max_point) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i positions = [int(i) for i in input().split()] positions [int(i) for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i scores = [int(i) for i in input().split()] scores [int(i) for i in input().split()] if max(scores) <= 0:
  print(max(scores))
else:
  positions = [0] + positions
  scores = [0] + scores
  max_point = -pow(10, 9)
  #print(max_point)
  for i in range(1, n + 1):
    s = []
    f_pos = positions[i]
    score = scores[f_pos]
    s.append(score)
    while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
    loop = len(s)
    if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
    max_point = max(max_point, point)
  print(max_point) max(scores) <= 0 max(scores) max scores 0 print(max(scores)) print max(scores) max scores positions = [0] + positions positions [0] + positions [0] 0 positions scores = [0] + scores scores [0] + scores [0] 0 scores max_point = -pow(10, 9) max_point -pow(10, 9) pow(10, 9) pow 10 9 for i in range(1, n + 1):
    s = []
    f_pos = positions[i]
    score = scores[f_pos]
    s.append(score)
    while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
    loop = len(s)
    if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
    max_point = max(max_point, point)
   i range(1, n + 1) range 1 n + 1 n 1 s = [] s [] f_pos = positions[i] f_pos positions[i] positions i score = scores[f_pos] score scores[f_pos] scores f_pos s.append(score) s.append s append score while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
     f_pos != i f_pos i f_pos = positions[f_pos] f_pos positions[f_pos] positions f_pos score += scores[f_pos] score scores[f_pos] scores f_pos s.append(score) s.append s append score loop = len(s) loop len(s) len s if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
     loop >= k loop k point = max(s[:k]) point max(s[:k]) max s[:k] s :k k elif s[-1] <= 0:
      point = max(s)
     s[-1] <= 0 s[-1] s -1 0 point = max(s) point max(s) max s loop_c = k // loop loop_c k // loop k loop r = k % loop r k % loop k loop point1 = s[-1] * loop_c point1 s[-1] * loop_c s[-1] s -1 loop_c if r != 0:
        point1 += max(s[:r])
       r != 0 r 0 point1 += max(s[:r]) point1 max(s[:r]) max s[:r] s :r r point2 = s[-1] * (loop_c - 1) point2 s[-1] * (loop_c - 1) s[-1] s -1 loop_c - 1 loop_c 1 point2 += max(0, max(s)) point2 max(0, max(s)) max 0 max(s) max s point = max(point1, point2) point max(point1, point2) max point1 point2 max_point = max(max_point, point) max_point max(max_point, point) max max_point point print(max_point) print max_point score += scores[f_pos] scores[f_pos] score positions = [0] + positions [0] + positions positions positions = [int(i) for i in input().split()] [int(i) for i in input().split()] positions scores = [0] + scores [0] + scores scores point1 += max(s[:r]) max(s[:r]) point1 point = max(point1, point2) max(point1, point2) point s = [] [] s n, k = map(int, input().split()) map(int, input().split()) n max_point = max(max_point, point) max(max_point, point) max_point f_pos = positions[f_pos] positions[f_pos] f_pos loop_c = k // loop k // loop loop_c r = k % loop k % loop r point1 = s[-1] * loop_c s[-1] * loop_c point1 point = max(s) max(s) point f_pos = positions[i] positions[i] f_pos scores = [int(i) for i in input().split()] [int(i) for i in input().split()] scores loop = len(s) len(s) loop point2 += max(0, max(s)) max(0, max(s)) point2 k = map(int, input().split()) map(int, input().split()) k point = max(s[:k]) max(s[:k]) point point2 = s[-1] * (loop_c - 1) s[-1] * (loop_c - 1) point2 max_point = -pow(10, 9) -pow(10, 9) max_point score = scores[f_pos] scores[f_pos] score
n, k = map(int, input().split())
positions = [int(i) for i in input().split()]
scores = [int(i) for i in input().split()]
if max(scores) <= 0:
  print(max(scores))
else:
  positions = [0] + positions
  scores = [0] + scores
  max_point = -pow(10, 9)
  #print(max_point)
  for i in range(1, n + 1):
    s = []
    f_pos = positions[i]
    score = scores[f_pos]
    s.append(score)
    while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
    loop = len(s)
    if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
    max_point = max(max_point, point)
  print(max_point)
