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

v = Array.new(n)
ans = -1 << 40
n.times do |s|
    next if v[s]
    v[s] = 0
    x = Array.new(n)
    
    d = n.times do |i|
        pre = now = s
        sum = c[now]
        i.times do
            now = pp[now] - 1
            v[now] = 0
            sum += c[now]
        end
        max = sum
        lim = now

        if pp[now] - 1 == s
            x[i] = sum
            break i
        end

        while (now = pp[now] - 1) != lim
            sum += c[now] - c[pre]
            pre = pp[pre] - 1
            max = [max, sum].max
        end

        x[i] = max
    end
    l = x[d]
    d.times{|i| x[i+1] = [x[i+1], x[i]].max}

    if k <= d+1
        ans = [ans, x[k-1]].max
    elsif l <= 0
        ans = [ans, x[d]].max
    else
        y, z = k.divmod(d+1)
        if z == 0
            ans = [ans, l * (y - 1) + x[d]].max
        else
            ans = [ans, l * y + x[z-1], l * (y - 1) + x[d]].max
        end
    end
end

p ans