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


groups = []
groups_c = []
used = []
N.times do |i|
    next if used[i]
    group = []
    c = []
    until used[i]
        group << i
        c << C[i]
        used[i] = true
        i = P[i]
    end

    groups << group
    groups_c << c
end

ans = -10**10
groups.count.times.map do |gi|
    group = groups[gi]
    c = groups_c[gi]
    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
        idx = i
        k1.times do 
            s += c[idx]
            idx = (idx+1)%m
            max = s if s > max
        end
    end

    score = sum + max
    ans = score if score > ans
end

p ans