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: import sys
  
  input_methods = ["clipboard", "file", "key"]
  using_method = 0
  input_method = input_methods[using_method]
  
  tin = lambda: map(int, input().split())
  lin = lambda: list(tin())
  mod = 1000000007
  
  # +++++
  
  
  def main():
      # a = int(input())
      n, k = tin()
      # s = input()
      al = lin()
      cl = lin()
      dd = [0] * n
      for i in range(n):
          if dd[i] != 0:
              continue
          cc = set()
          pos = i
          point = 0
          n_node = 0
          while pos not in cc:
              cc.add(pos)
              pos = al[pos] - 1
              point += cl[pos]
              n_node += 1
  
          for p in cc:
              dd[p] = (point, n_node)
  
      ret = -mod * mod
      for i in range(n):
          pi, ni = dd[i]
          if pi < 0:
              point = 0
              pmax = -mod * mod
              pos = i
              for _ in range(ni):
                  cc.add(pos)
                  pos = al[pos] - 1
                  point += cl[pos]
                  pmax = max(pmax, point)
              ret = max(ret, pmax)
          elif k < ni * 2:
              point = 0
              pmax = -mod * mod
              pos = i
              for _ in range(k):
                  cc.add(pos)
                  pos = al[pos] - 1
                  point += cl[pos]
                  pmax = max(pmax, point)
              ret = max(ret, pmax)
          else:
              point = pi * ((k // ni) - 1)
              nk = k % (ni) + ni
              pos = i
              pmax = point
              for _ in range(nk):
                  cc.add(pos)
                  pos = al[pos] - 1
                  point += cl[pos]
                  pmax = max(point, pmax)
              ret = max(ret, pmax)
      return ret
  
  
  # +++++
  isTest = False
  
  
  def pa(v):
      if isTest:
          print(v)
  
  
  def input_clipboard():
      import clipboard
  
      input_text = clipboard.get()
      input_l = input_text.splitlines()
      for l in input_l:
          yield l
  
  
  if __name__ == "__main__":
      if sys.platform == "ios":
          if input_method == input_methods[0]:
              ic = input_clipboard()
              input = lambda: ic.__next__()
          elif input_method == input_methods[1]:
              sys.stdin = open("inputFile.txt")
          else:
              pass
          isTest = True
      else:
          pass
          # input = sys.stdin.readline
  
      ret = main()
      if ret is not None:
          print(ret)
  
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."}]