class SegmentTree
  MIN = -Float::INFINITY
  def initialize(data)
    @offset = 1 << data.length.bit_length
    @data = Array.new(@offset * 2, MIN)
    data.each_with_index do |v, i|
      @data[@offset + i] = v
    end
    i = @offset - 1
    while i > 0
      @data[i] = merge(@data[2 * i], @data[2 * i + 1])
      i -= 1
    end
  end
  def merge(l, r); l > r ? l : r; end
  def max(a, b, k = 1, l = 0, r = @offset)
    mid = (l + r) / 2
    return MIN if b <= l || r <= a
    return @data[k] if a <= l && r <= b
    return merge(max(a,b,2 * k, l, mid), max(a,b,2 * k + 1, mid, r))
  end
end

def max(a,b); a > b ? a : b; end
def min(a,b); a < b ? a : b; end

def extract_cycle(s, used)
  cycle = []
  u = s
  until used[u]
    used[u] = true
    cycle << u  
    u = P[u]
  end
  return cycle
end

N, K = gets.split.map(&:to_i)
P = gets.split.map(&:to_i).tap{|array| array.unshift(0) }
C = gets.split.map(&:to_i).tap{|array| array.unshift(0) }

used = Array.new(N + 1)
ans = (1 .. N).inject(-1e20) do |max_score, s|
  next max_score if used[s]
  # sを始点とする閉路を抽出
  cycle = extract_cycle(s, used)
  z = cycle.size
  
  # 閉路の各頂点から最大1周分のスコアを計算できるように事前準備
  score = Array.new(3 * z + 1, 0)
  (1 .. z * 3).each do |i|
    v = cycle[i % z]
    score[i] = score[i - 1] + C[v]
  end

  # 閉路上の任意の点 s を始点としたときの最大スコアを 
  # セグ木を使って O(N log N) で計算
  
  sz = max(0, score[z])
  k = min(K, z + K % z)
  r = (K - k) / z

  st = SegmentTree.new(score)
  cycle_score = (0 ... z).map{|s| r * sz + st.max(s + 1, s + k + 1) - score[s]}.max

  max(max_score, cycle_score)
end

puts ans
