N, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i)
C = gets.split.map(&:to_i)

loop = {}

result = C[0]
N.times do |i|
    point = 0
    k = K
    x = i
    hist = [i]
    while k > 0 do
        k -= 1
        n = P[x] - 1
        point += C[n]
        hist.push(n)
        if n == i
            hist.each do |h|
                loop[h] = {
                    point: point,
                    count: K - k
                }
            end
        end
        if l = loop[n]
            cnt = l[:count]
            if l[:point] > 0
                point += (k / cnt) * l[:point]
            end
            k = k % cnt
        end
        x = n
        if point > result
            result = point
        end
    end
end
puts result