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|
    total = 0
    if group.count < K then
        sum = group.map{C[_1]}.sum * ((K.to_f / group.count).ceil - 1)
        total = sum if sum>0
    end

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

    total + max
end

p scores.max