#input of int(split by space)
def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n 
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
      return Array.new(size1){init}
  else
      return Array.new(size2){Array.new(size1){init}}
  end
end

def max(a,b)
    return a>b ? a : b
end

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY

N,K=get_i
P=get_i
C=get_i
ans=-INF
def cal(i)
  log=[0]
  visit=array(N,false)
  k=K
  sum=0
  cnt=0
  max=-INF
  while k>0 and not visit[i]
      visit[i]=true
      i=P[i]-1
      sum+=C[i]
      log<<sum
      k-=1
      cnt+=1
      max=sum if max<sum
  end
  if k>0
    if sum>0
        if (k%cnt).zero?
          return sum*(k/cnt+1)
        else
            return sum*(k/cnt+1)+log[1..k%cnt].max
        end
    else
        return max
    end
  else
      return max
  end
end
N.times do|i|
  ans=max(ans,cal(i))
end
puts ans