import qualified Data.Vector.Unboxed as V
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getIntList = readIntList <$> BS.getLine

main = do
  [n,k] <- getIntList
  p <- V.fromList . map (+(-1)) <$> getIntList
  c <- V.fromList <$> getIntList
  let acc i = scanl1 (+) $ map ((c V.!).(p V.!)) $ takeWhile (/=i) (iterate (p V.!) (p V.! i)) ++ [i] -- O(n)
      ans i = -- O(n)
        if val > 0
          then
            if rem /= 0
              then num * val + maximum (take (fromIntegral rem) list)
              else (num-1) * val + maximum list
          else maximum list
        where list = acc i
              val = last list
              len = fromIntegral $ length list
              num = k `div` len
              rem = k `mod` len
  print $ maximum $ map ans [0..n-1] -- n * O(n) = O(n^2)
