N, K = gets.chomp.split(' ').map(&:to_i)
pmt = gets.chomp.split(' ').map(&:to_i).unshift(nil)
c = gets.chomp.split(' ').map(&:to_i).unshift(nil)

dp = Array.new(N + 1) { [] }

ans = -Float::INFINITY
1.upto(N) do |n|
    1.upto(K) do |k|
        # puts "n = #{n}, k = #{k}"
        pos = n
        restK = k
        score = 0
        max = -Float::INFINITY
        k.times do
            if dp[pos][restK] then
                score += dp[pos][restK]
                max = [max, score].max
                break
            end
            pos = pmt[pos]
            restK -= 1
            score += c[pos]
            max = [max, score].max
        end
        dp[n][k] = max
        # p dp
        ans = [ans, dp[n][k]].max
    end
end

puts ans