-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE BangPatterns #-}
import Data.Char (isSpace)
import Data.Int (Int64)
import Data.List (unfoldr)
import qualified Data.Vector.Unboxed as U
import qualified Data.ByteString.Char8 as BS
import qualified Data.IntSet as IntSet

cycles :: Int -> U.Vector Int -> [[Int]]
cycles !n perm = loop (IntSet.fromDistinctAscList [0..n-1])
  where
    loop s | IntSet.null s = []
           | otherwise = let (m,s') = IntSet.deleteFindMin s
                             (cyc,s'') = oneCycle m m [m] s'
                         in reverse cyc : loop s''
    oneCycle m0 m xs s = let m' = perm U.! m
                         in if m' == m0 then
                              (xs, s)
                            else
                              oneCycle m0 m' (m':xs) (IntSet.delete m' s)

solve :: Int -> Int -> U.Vector Int -> U.Vector Int64 -> Int64
solve !n !k perm c = maximum $ map solveOneCycle $ cycles n perm
  where
    solveOneCycle cyc =
      let scores = map (c U.!) cyc
          cycle_len = length scores
          t = sum scores
          scores' = U.fromListN (2 * cycle_len - 1) $ take (2 * cycle_len - 1) $ cycle scores
          ss = U.scanl' (+) 0 scores'
      in if t <= 0 || k <= cycle_len then
           maximum [ ss U.! j - ss U.! i | i <- [0..cycle_len-1], j <- [i+1..min (2*cycle_len-1) (i+k)]]
         else
           let (q,r) = k `quotRem` cycle_len
               -- q >= 1
           in t * fromIntegral q + maximum [ ss U.! j - ss U.! i | i <- [0..cycle_len-1], j <- [i+1..min (2*cycle_len-1) (i+r)]]

main = do
  [n,k] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  perm <- U.map (subtract 1) <$> U.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  c <- U.map fromIntegral <$> U.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  print $ solve n k perm c
