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)
  dic = {}
  for i in range(1, n + 1):
    f_pos = i
    #print('f_pos',f_pos)
    if f_pos not in dic:
      point_loop = 0
      loop = 0
      pos_set = set([f_pos])
      while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
    else:
      loop, point_loop = dic[f_pos]
    #print(loop, point)
    loop_c = k // loop
    r = k % loop
    if loop_c == 0 or point_loop <= 0:
      if point_loop <= 0:
        if loop_c > 0:
          r = loop
      point = 0
      for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
    else:
      point1, point2 = 0, 0
      point1 = loop_c * point_loop
      max_point1 = point1
      for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
      f_pos = i
      point2 = (loop_c - 1) * point_loop
      max_point2 = point2
      for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
      max_point = max(max_point, max(max_point1, max_point2))
  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)
  dic = {}
  for i in range(1, n + 1):
    f_pos = i
    #print('f_pos',f_pos)
    if f_pos not in dic:
      point_loop = 0
      loop = 0
      pos_set = set([f_pos])
      while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
    else:
      loop, point_loop = dic[f_pos]
    #print(loop, point)
    loop_c = k // loop
    r = k % loop
    if loop_c == 0 or point_loop <= 0:
      if point_loop <= 0:
        if loop_c > 0:
          r = loop
      point = 0
      for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
    else:
      point1, point2 = 0, 0
      point1 = loop_c * point_loop
      max_point1 = point1
      for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
      f_pos = i
      point2 = (loop_c - 1) * point_loop
      max_point2 = point2
      for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
      max_point = max(max_point, max(max_point1, max_point2))
  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 dic = {} dic {} for i in range(1, n + 1):
    f_pos = i
    #print('f_pos',f_pos)
    if f_pos not in dic:
      point_loop = 0
      loop = 0
      pos_set = set([f_pos])
      while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
    else:
      loop, point_loop = dic[f_pos]
    #print(loop, point)
    loop_c = k // loop
    r = k % loop
    if loop_c == 0 or point_loop <= 0:
      if point_loop <= 0:
        if loop_c > 0:
          r = loop
      point = 0
      for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
    else:
      point1, point2 = 0, 0
      point1 = loop_c * point_loop
      max_point1 = point1
      for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
      f_pos = i
      point2 = (loop_c - 1) * point_loop
      max_point2 = point2
      for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
      max_point = max(max_point, max(max_point1, max_point2))
   i range(1, n + 1) range 1 n + 1 n 1 f_pos = i f_pos i if f_pos not in dic:
      point_loop = 0
      loop = 0
      pos_set = set([f_pos])
      while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
    else:
      loop, point_loop = dic[f_pos]
    #print(loop, point)
     f_pos not in dic f_pos dic point_loop = 0 point_loop 0 loop = 0 loop 0 pos_set = set([f_pos]) pos_set set([f_pos]) set [f_pos] f_pos while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
     True f_pos = positions[f_pos] f_pos positions[f_pos] positions f_pos point_loop += scores[f_pos] point_loop scores[f_pos] scores f_pos loop += 1 loop 1 if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
         f_pos in pos_set f_pos pos_set for j in pos_set:
            dic[j] = [loop, point_loop]
           j pos_set dic[j] = [loop, point_loop] dic[j] dic j [loop, point_loop] loop point_loop break pos_set.add(f_pos) pos_set.add pos_set add f_pos loop, point_loop = dic[f_pos] loop dic[f_pos] dic f_pos point_loop loop_c = k // loop loop_c k // loop k loop r = k % loop r k % loop k loop if loop_c == 0 or point_loop <= 0:
      if point_loop <= 0:
        if loop_c > 0:
          r = loop
      point = 0
      for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
    else:
      point1, point2 = 0, 0
      point1 = loop_c * point_loop
      max_point1 = point1
      for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
      f_pos = i
      point2 = (loop_c - 1) * point_loop
      max_point2 = point2
      for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
      max_point = max(max_point, max(max_point1, max_point2))
   loop_c == 0 or point_loop <= 0 loop_c == 0 loop_c 0 point_loop <= 0 point_loop 0 if point_loop <= 0:
        if loop_c > 0:
          r = loop
       point_loop <= 0 point_loop 0 if loop_c > 0:
          r = loop
       loop_c > 0 loop_c 0 r = loop r loop point = 0 point 0 for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
     _ range(r) range r f_pos = positions[f_pos] f_pos positions[f_pos] positions f_pos point += scores[f_pos] point scores[f_pos] scores f_pos max_point = max(max_point, point) max_point max(max_point, point) max max_point point point1, point2 = 0, 0 point1 0 point2 0 point1 = loop_c * point_loop point1 loop_c * point_loop loop_c point_loop max_point1 = point1 max_point1 point1 for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
       _ range(r) range r f_pos = positions[f_pos] f_pos positions[f_pos] positions f_pos point1 += scores[f_pos] point1 scores[f_pos] scores f_pos max_point1 = max(point1, max_point1) max_point1 max(point1, max_point1) max point1 max_point1 f_pos = i f_pos i point2 = (loop_c - 1) * point_loop point2 (loop_c - 1) * point_loop loop_c - 1 loop_c 1 point_loop max_point2 = point2 max_point2 point2 for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
       _ range(loop) range loop f_pos = positions[f_pos] f_pos positions[f_pos] positions f_pos point2 += scores[f_pos] point2 scores[f_pos] scores f_pos max_point2 = max(point2, max_point2) max_point2 max(point2, max_point2) max point2 max_point2 max_point = max(max_point, max(max_point1, max_point2)) max_point max(max_point, max(max_point1, max_point2)) max max_point max(max_point1, max_point2) max max_point1 max_point2 print(max_point) print max_point point_loop = 0 0 point_loop point_loop = dic[f_pos] dic[f_pos] point_loop r = k % loop k % loop r n, k = map(int, input().split()) map(int, input().split()) n loop += 1 1 loop f_pos = positions[f_pos] positions[f_pos] f_pos max_point1 = max(point1, max_point1) max(point1, max_point1) max_point1 loop = 0 0 loop scores = [0] + scores [0] + scores scores max_point1 = point1 point1 max_point1 f_pos = positions[f_pos] positions[f_pos] f_pos dic = {} {} dic loop, point_loop = dic[f_pos] dic[f_pos] loop r = loop loop r point2 = 0, 0 0 point2 point_loop += scores[f_pos] scores[f_pos] point_loop k = map(int, input().split()) map(int, input().split()) k f_pos = positions[f_pos] positions[f_pos] f_pos point1 += scores[f_pos] scores[f_pos] point1 pos_set = set([f_pos]) set([f_pos]) pos_set point2 = (loop_c - 1) * point_loop (loop_c - 1) * point_loop point2 point2 += scores[f_pos] scores[f_pos] point2 point1, point2 = 0, 0 0 point1 loop_c = k // loop k // loop loop_c max_point2 = max(point2, max_point2) max(point2, max_point2) max_point2 point = 0 0 point max_point = max(max_point, point) max(max_point, point) max_point f_pos = i i f_pos f_pos = positions[f_pos] positions[f_pos] f_pos point += scores[f_pos] scores[f_pos] point max_point = max(max_point, max(max_point1, max_point2)) max(max_point, max(max_point1, max_point2)) max_point max_point2 = point2 point2 max_point2 max_point = -pow(10, 9) -pow(10, 9) max_point scores = [int(i) for i in input().split()] [int(i) for i in input().split()] scores positions = [int(i) for i in input().split()] [int(i) for i in input().split()] positions f_pos = i i f_pos point1 = loop_c * point_loop loop_c * point_loop point1 positions = [0] + positions [0] + positions positions
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)
  dic = {}
  for i in range(1, n + 1):
    f_pos = i
    #print('f_pos',f_pos)
    if f_pos not in dic:
      point_loop = 0
      loop = 0
      pos_set = set([f_pos])
      while True:
        f_pos = positions[f_pos]
        point_loop += scores[f_pos]
        #print('pos', f_pos)
        #print(point)
        loop += 1
        if f_pos in pos_set:
          for j in pos_set:
            dic[j] = [loop, point_loop]
          break
        pos_set.add(f_pos)
    else:
      loop, point_loop = dic[f_pos]
    #print(loop, point)
    loop_c = k // loop
    r = k % loop
    if loop_c == 0 or point_loop <= 0:
      if point_loop <= 0:
        if loop_c > 0:
          r = loop
      point = 0
      for _ in range(r):
        f_pos = positions[f_pos]
        point += scores[f_pos]
        max_point = max(max_point, point)
    else:
      point1, point2 = 0, 0
      point1 = loop_c * point_loop
      max_point1 = point1
      for _ in range(r):
        f_pos = positions[f_pos]
        point1 += scores[f_pos]
        max_point1 = max(point1, max_point1)
      f_pos = i
      point2 = (loop_c - 1) * point_loop
      max_point2 = point2
      for _ in range(loop):
        f_pos = positions[f_pos]
        point2 += scores[f_pos]
        max_point2 = max(point2, max_point2)
      max_point = max(max_point, max(max_point1, max_point2))
  print(max_point)
