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

def section_max(dp, z)
  segtree = SegmentTree.new(dp)
  (0 ... z).inject(-Float::INFINITY) do |s, offset|
    if K >= z
      m = segtree.max(offset + 1, offset + z + K % z + 1) 
      r = K / z - 1
      max(s, r * max(0, dp[z]) + m - dp[offset])
    else
      m = segtree.max(offset + 1, offset + K + 1)
      max(s, m - dp[offset])
    end
  end
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) }
ans = -1e20

used = Array.new(N + 1)
(1 .. N).each do |s|
  next if used[s]
  cycle = extract_cycle(s, used)
  z = cycle.size
  
  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
  ans = max(ans, section_max(score, z))
end
puts ans
