N, K = gets.split.map(&:to_i)
P = gets.split.map{|i|i.to_i - 1}
C = gets.split.map(&:to_i)
max = -10 ** 10
N.times{|n|
    score = [0]
    i = n
    m = -10 ** 10
    loop{
        i = P[i]
        score << score[-1] + C[i]
        if i == n
            break
        end
    }
    #warn score.inspect
    s = score[-1]
    if s < 0
        s = 0
    end
    score.size.times{|i|
        score[i] += (K - i) / (score.size - 1) * s
        if m < score[i]
            m = score[i]
        end
    }
    #warn score.inspect
    #warn m
    if max < m
        max = m
    end
}
puts max
