import scala.collection.mutable

//object D {
object Main {
  import InOut._
  def main(args: Array[String]): Unit = {

    val n, k = nextInt
    val ps = nextInts(n).map(_ - 1)
    val cs = nextInts(n)
    val visited = Array.fill(n){ false }
    var res: Long = cs.max

    for (i <- 0 until n) {
      if (!visited(i) && res > 0) {
        val builder = new mutable.ArrayBuilder.ofInt
        var p = i
        while (!visited(p)) {
          visited(p) = true
          builder += p
          p = ps(p)
        }
        val cycle = builder.result()
        val cycleSum = cycle.map(p => cs(p).toLong).sum
        var cycleSubRes, cycleSubRes2 = 0L
        var j = 0
        val kk = Math.min(k, cycle.length - 1)
        val kMod = k % cycle.length
        while (j < cycle.length) {
          var sum = 0L
          var len = 0
          while (len < kk) {
            sum += cs(cycle((j + len) % cycle.length))
            if (sum > cycleSubRes) {
              cycleSubRes = sum
            }
            if (sum > cycleSubRes2 && len < kMod) {
              cycleSubRes2 = sum
            }
            len += 1
          }
          j += 1
        }
        if (cycleSum > 0 && k > cycle.length) {
          cycleSubRes += cycleSum * (k / cycle.length - 1)
        }
        if (cycleSum > 0) {
          cycleSubRes2 += cycleSum * (k / cycle.length)
        }
        val cycleRes = Math.max(cycleSubRes, cycleSubRes2)
        if (cycleRes > res) {
          res = cycleRes
        }
      }
    }

    out.println(res)

    out.flush()
  }
 
  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}
