import           Data.List
import qualified Data.Vector.Unboxed as V
getIntList = map read . words <$> getLine
main = do
  [n,k] <- getIntList
  p <- V.fromList . map pred <$> getIntList
  c <- V.fromList <$> getIntList
  let cp = V.fromList [c V.! (p V.! i) | i <- [0..n-1]]
      acc i = tail . scanl' (+) 0 $ map (cp V.!) $ takeWhile (/=i) (iterate (p V.!) (p V.! i)) ++ [i]
      ans i =
        if val > 0
          then
            if rem /= 0
              then num * val + maximum (take rem list)
              else (num-1) * val + maximum list
          else maximum list
        where list = acc i
              val = last list
              len = length list
              (num,rem) = k `divMod` len
  (print . maximum . map ans) [0..n-1]