N,K = gets.split.map &:to_i
P = gets.split.map{_1.to_i - 1}
C = gets.split.map &:to_i

groups = []
used = []
N.times do |i|
    next if used[i]
    group = []
    j = 0
    until used[i]
        group[j] = i
        used[i] = true
        i = P[i]
        j+=1
    end

    groups << group
end

scores = groups.map do |group|
    m = group.count
    l = K / m - 1
    sum = group.map{C[_1]}.sum
    sum = sum > 0 ? sum * l : 0

    max = -10**10 
    k1 = K % m + m
    m.times do |i|
        s = 0
        k1.times do |offset|
            idx = (i+offset) % group.count
            s += C[group[idx]]
            max = [max,s].max
        end
    end

    sum + max
end

p scores.max