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

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
    --print $ loops
    let remCs = map (optForLoop k) loops
    print $ maximum $ zipWith re sums remCs

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 = if k == length lp then k else k `rem` length lp in
    maximum $ map (kukan lp) [1..remK]

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


re s ku = if s > 0 then s + ku else ku

