import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val N, K = sc.nextInt
  val P = Array.fill(N)(sc.nextInt)
  val C = Array.fill(N)(sc.nextLong)

  val dp = Array.fill(N, K)((BigInt(0), 0))

  for (i <- 0 until N; j <- 0 until K) {
    val prevScore = if (j == 0) BigInt(0) else dp(i)(j - 1)._1
    val pos = if (j == 0) P(i) - 1 else P(dp(i)(j - 1)._2) - 1
    dp(i)(j) = (prevScore + C(pos), pos)
  }

  val z = dp.flatMap(_.map(_._1)).max

  println(z)
}
