output_description: Print the maximum possible score at the end of the game.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N K
      P_1 P_2 \cdots P_N
      C_1 C_2 \cdots C_N
attempt: #!/usr/bin/env python3
  
  import sys
  
  
  def main():
      # inf = float('inf')              # sys.float_info.max = 1.79e+308
      inf = 2**63 - 1  # (for fast JIT compile in PyPy) 9.22e+18
      sys.setrecursionlimit(10**6)  # 1000 -> 1000000
  
      def input():
          return sys.stdin.readline().rstrip()
  
      def ii():
          return int(input())
  
      def isp():
          return input().split()
  
      def mi():
          return map(int, input().split())
  
      def mi_0():
          return map(lambda x: int(x) - 1, input().split())
  
      def lmi():
          return list(map(int, input().split()))
  
      def lmi_0():
          return list(map(lambda x: int(x) - 1, input().split()))
  
      def li():
          return list(input())
  
      def debug(x):
          print(x, file=sys.stderr)
  
      def calc_score(start):
          order = [None] * n
          accum = [None] * n
          order[start] = 0
          accum[start] = 0
          place = start
          cnt = 0
          current_score = 0
          M = -inf
  
          while cnt < k:
              place = P[place]  # 移動
              cnt += 1  # 移動回数
              current_score += C[place]  # 移動後の index でスコアに追加
              M = max(
                  M, current_score
              )  # [スタートの次点, 現在地] まで任意の場所をゴールに選ぶときのスコア最大値
              if order[place] is None:
                  # 未訪問
                  order[place] = cnt
                  accum[place] = current_score
              else:
                  # 訪問ずみ (loop)
                  head_len = order[place]
                  loop_len = cnt - head_len
                  head_score = accum[place]
                  loop_gain = current_score - accum[place]
                  if loop_gain <= 0:
                      # debug('encountered loop, but non-positive loop.')
                      return M
                  else:
                      # debug('encountered positive loop.')
                      loop_times, res = divmod(k - head_len, loop_len)
                      # place からスタートして "0 以上" res 以下だけ進むときの
                      res_score = 0  # 稼ぐ最大スコア
                      tmp_score = 0  # 現在の累積スコア
                      tmp_place = place  # 現在地
                      for _ in range(res):
                          tmp_place = P[tmp_place]
                          tmp_score += C[tmp_place]  # 移動後の index でスコアに追加
                          res_score = max(res_score, tmp_score)
                      # 限界まで loop を回した上で 0 ~ res だけ貪欲に稼ぐ vs 限界 - 1 回 loop を回し確実にもう一回好きなところでとめられるようにして稼ぐ
                      loop_score = head_score + loop_times * loop_gain + res_score
                      stop_score = M + (loop_times - 1) * loop_gain
                      return max(loop_score, stop_score)
  
          # debug('never encountered loop.')
          return M
  
      n, k = mi()
      P = lmi_0()
      C = lmi()
  
      ans = -inf
      for i in range(n):
          score = calc_score(i)
          # debug(f'start: {i} ans: {score}')
          ans = max(ans, score)
      print(ans)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  Takahashi will play a game using a piece on an array of squares numbered 1, 2,
  \cdots, N. Square i has an integer C_i written on it. Also, he is given a
  permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
  
  Now, he will choose one square and place the piece on that square. Then, he
  will make the following move some number of times between 1 and K (inclusive):
  
    * In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
  
  Help him by finding the maximum possible score at the end of the game. (The
  score is 0 at the beginning of the game.)
samples: [{"input": "5 2\n    2 4 5 1 3\n    3 4 -10 -8 8", "output": "8\n    \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n  * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n  * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n  * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n  * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n  * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n    2 1\n    10 -7", "output": "13\n    \n\n* * *"}, {"input": "3 3\n    3 1 2\n    -1000 -2000 -3000", "output": "-1000\n    \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n    9 1 6 7 8 4 3 2 10 5\n    695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n    \n\nThe absolute value of the answer may be enormous."}]