{-# 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

type Points = Int

data LoopK = L {sumL :: Points, optV :: U.Vector Points} deriving Show
lenL (L s v) = U.length v

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 optVs = map (optVector k) loops
    print $ maximum $ map ((re k).(uncurry L)) $ zip sums optVs

findloop :: Int -> U.Vector Int -> U.Vector Points -> UM.IOVector Bool -> IO [Points]
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

optVector :: Int -> [Points] -> U.Vector Points
optVector k lp = let    cyc = scanl1 (+) (cycle lp)
                        scn = scanl1 (+) lp
                        kuk len = maximum $ zipWith (-) (drop len cyc) scn
    in  U.map kuk (U.generate (min k $ length lp) succ)

re :: Int -> LoopK -> Points
re k l = if sumL l > 0
    then let    remK = if k == lenL l then k else k `rem` lenL l
                remC = U.maximum $ U.take remK (optV l)
                fremC' = if remK == lenL l then 0 else U.maximum $ U.drop remK (optV l)
                fremC = max remC fremC'
        in max ((floor $ d k (lenL l)) * sumL l + remC) ((pred $ floor $ d k (lenL l)) * sumL l + fremC)
    else U.maximum (optV l)

