
import scala.io.StdIn

object Main extends App {
  val Array(n,kMax)= StdIn.readLine().split(' ').map(_.toLong)
  val permutations:Array[Long] = StdIn.readLine().split(' ').map(_.toLong)
  val scores:Array[Long] = StdIn.readLine().split(' ').map(_.toLong)

  def maxPoint(posInit:Long):Long={
    var pos=posInit
    var currentScore=0L
    (for(k <- 1 to kMax.toInt)yield {
      pos = permutations((pos-1).toInt)
      currentScore = currentScore + scores((pos-1).toInt)
      currentScore
    }).max
  }

  val ans = (for(posInit <- 1L to n)yield{
    maxPoint(posInit)
  }).max

  println(ans)
}






