n, k = gets.split.map(&:to_i)
pp = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

x = [k, n].min
max_x = Array.new(n, -1<<40)

n.times do |st|
    now = st
    sum = 0
    x.times do 
        now = pp[now] - 1
        sum += c[now]
        max_x[st] = [max_x[st], sum].max
    end
end

if k <= n
    p max_x.max
    exit
end

max_loop = Array.new(n)
n.times do |st|
    next if max_loop[st]
    now = st
    sum = 0
    n.times do |i|
        now = pp[now] - 1
        sum += c[now]
        if now == st
            max_loop[now] = [sum, i+1]
            break
        end
        max_loop[now] = [st, nil]
    end
end

max_m = Array.new(n, 0)

n.times do |st|
    y = max_loop[st][1] ? st : max_loop[st][0]
    next if max_loop[y][0] <= 0
    m = k % max_loop[y][1]
    if m == 0
        max_m[st] = max_x[st]
        next
    end
    now = st
    sum = 0
    m.times do
        now = pp[now] - 1
        sum += c[now]
        max_m[st] = [max_m[st], sum].max
    end
end

p (n.times.map do |i|
    y = max_loop[i][1] ? i : max_loop[i][0]
    [max_x[i], max_loop[y][0] * ((k - 1) / max_loop[y][1]) + max_m[i]].max
end.max)