{-# LANGUAGE
    BangPatterns, MultiWayIf, TupleSections, TypeApplications #-}
{-# OPTIONS_GHC -O2 #-}


import Control.Arrow
import Control.Concurrent
import Control.Monad
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Char8 as C
import Data.Bits
import Data.Complex
import Data.Char
import Data.Function
import Data.Function.HT
import qualified Data.IntMap.Strict as IM
import qualified Data.IntSet as IS
import Data.List
import Data.Monoid
import Data.Ord
import qualified Data.Sequence as Sq
import Data.Vector.Algorithms.Search
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import Debug.Trace
import System.IO

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

d :: Int -> Int -> Double
d x y = fromIntegral x / fromIntegral y


data LoopK = L {lenL :: Int, sumL :: Int, remC :: Int, fremC :: Int}

lpB :: [Int] -> LoopK
lpB [a,b,c,d] = L a b c d

main = do
    [n,k] <- r' :: IO [Int]
    ps <- U.map pred <$> r n
    cs <- r n
    visited <- UM.replicate n False
    loops <- filter (/=[]) <$> (mapM (\f -> findloop f ps cs visited) [0..n-1])
    let sums = map sum loops
    let remCs = map (optForLoop k) loops
    let fremCs = map (fullOptForLoop k) loops
    -- print $ loops
    -- print $ sums
    -- print $ remCs
    -- print $ fremCs
    print $ maximum $ map ((re k).lpB) $ transpose [(map length loops), sums, remCs, fremCs]

findloop :: Int -> U.Vector Int -> U.Vector Int -> UM.IOVector Bool -> IO [Int]
findloop from ps cs visited = do
    x <- UM.read visited from
    if x then return [] else do
        let resP = (from :) $ takeWhile (/= from) $ tail $ iterate (ps U.!) from
        mapM_ (\a -> UM.write visited a True) resP
        return $ map (\p -> cs U.! p) resP

optForLoop :: Int -> [Int] ->  Int
optForLoop k lp = let remK = k `rem` length lp in
    if remK == 0 then 0 else maximum $ map (kukan lp) [1..remK]

fullOptForLoop :: Int -> [Int] ->  Int
fullOptForLoop k lp = maximum $ map (kukan lp) [1..min k (length lp)]



kukan :: [Int] -> Int -> Int
kukan lp len = maximum $ zipWith (-) (drop len (scanl1 (+) (cycle lp))) (scanl1 (+) lp)

re :: Int -> LoopK -> Int
re k l = if sumL l > 0
    then max ((floor $ d k (lenL l)) * sumL l + remC l) ((pred $ floor $ d k (lenL l)) * sumL l + fremC l)
    else fremC l

