
fun main(args: Array<String>) {
    val (n, k) = getInts()
    val p = getInts().map { it - 1 }
    val c = getLongs()

    val visit = BooleanArray(n) { false }

    var out = Long.MIN_VALUE
    (0 until n).forEach {
        out = Math.max(out, go(n, k, p, c, it, visit))
    }
    println(out)
}

fun go(n: Int, k:Int, p: List<Int>, c: List<Long>, index: Int, visit: BooleanArray): Long {
    if (visit[index]) return Long.MIN_VALUE

    val ring = ArrayList<Int>()
    val score = ArrayList<Long>()
    var now = index
    while (true) {
        ring.add(now)
        visit[now] = true

        now = p[now]
        score.add(c[now])
        if (now == ring.first()) break
    }

    if (ring.size >= k) {
        return sumScore(score, k)
    } else {
        val sum = score.sum()
        if (sum < 0) {
            return sumScore(score, ring.size)
        } else {
            val turn = k / ring.size
            val remain = k % ring.size
            if (remain == 0) {
                return sumScore(score, ring.size) + (sum * (turn - 1))
            } else {
                return sumScore(score, remain) + (sum * turn)
            }
        }
    }
}

fun sumScore(score: List<Long>, k: Int): Long {
    var mm = Long.MIN_VALUE
    score.indices.forEach { skip ->
        var sum = 0L
        var m = Long.MIN_VALUE
        (0 until k).map { score[(it + skip) % score.size] }.forEach {
            sum += it
            m = Math.max(m, sum)
        }
        mm = Math.max(mm, m)
    }
    return mm
}

fun getInt() = readLine()!!.toInt()
fun getInts() = readLine()!!.split(" ").map { it.toInt() }
fun getLong() = readLine()!!.toLong()
fun getLongs() = readLine()!!.split(" ").map { it.toLong() }

