class Problem
  attr_accessor *(?a..?z).to_a.map(&:to_sym)
  MAX = 30
  def initialize
    @n,@k = gets.split.map(&:to_i)
    @p = gets.split.map(&:to_i).map{ _1 - 1 }
    @c = gets.split.map(&:to_i)
  end

  def solve
    ans = 0
    rec.each do |cycle|
      cnt = query(cycle)
      ans = cnt if ans < cnt
    end
    ans
  end

  def query(cycle)
    costs = cycle.map{ c[_1] }
    n = costs.size
    maxi = Array.new([n,k].min + 1, -10**10)
    dc = costs + costs
    n.times do |i|
      1.upto([n,k].min) do |j|
        cnt = dc[i,j].sum 
        maxi[j] = cnt if maxi[j] < cnt
      end
    end

    if costs.sum <= 0
      return maxi.max
    else
      x = costs.sum * (k / n) + maxi[1..(k % n)].max
      return [x, maxi.max]
    end
  end

  def rec
    g = []
    seen = Array.new(n, false)
    n.times do |i|
      next if seen[i]
      seen[i] = true
      h = [i]
      j = p[i]
      while i != j
        seen[j] = true
        h << j
        j = p[j]
      end
      g << h
    end
    return g
  end

  def show(ans)
    puts ans
  end
end

Problem.new.instance_eval do
  show(solve)
end