n,k = gets.chomp.split(' ').map(&:to_i)
ps = gets.chomp.split(' ').map(&:to_i)
cs = gets.chomp.split(' ').map(&:to_i)

search = Array.new(n){|i| i.itself}
visited = []
max_ary = []

while search.size > 0
    now = search.shift
    path_sum = []
    sum = 0
    step = 0
    while true
        if !visited.include?(now)
            visited << now
            sum += cs[now]
            path_sum << sum
            step += 1
            now = ps[now] - 1
        else
            if sum <= 0
                max_ary << path_sum.max
                break
            else
                q,r = k.divmod(step)
                loop_sum = q*sum
                if r != 0
                    loop_sum += path_sum[0..(r-1)].max
                end
                path_sum << loop_sum
                max_ary << path_sum.max
                break
            end
        end
    end
    visited = []
end

puts max_ary.max

