fun main() {
    val nk = nextIntList()
    val pn = nextIntList()
    val cn = nextLongList()
    val ans = solve(nk[1], pn, cn)
    println(ans)
}

//movingCount < 10^9
//1回以上movingCount以下の回数の抽選を行う
//最初にコマをおいたあと移動する場所はpn[i]に記載されている
//移動回数分数値がベタガキされていく →　再帰関数
fun solve(movingCount: Int, pn: List<Int>, cn: List<Long>): Long {
    val track = createTrack(pn = pn)
    val trackSum = track.map { tr ->
        tr to tr.map {
            cn[it - 1]
        }.sum()
    }

    val mostHigh = trackSum.flatMap {
        val trackListSize = it.first.size

        //トラックの和が負のとき
        if (it.second <= 0) {
            val attemptCount = if (movingCount < trackListSize) movingCount else trackListSize

            it.first.map { index ->
                calculateRecursion(attemptCount, index, 0, Long.MIN_VALUE, pn, cn)
            }
        } else {
            val actualLength = if (movingCount % trackListSize == 0) trackListSize else movingCount % trackListSize
            val actualLoop =
                if (movingCount % trackListSize == 0) movingCount / trackListSize - 1 else movingCount / trackListSize
            it.first.map { index ->
                calculateRecursion(actualLength, index, 0, Long.MIN_VALUE, pn, cn) + it.second * actualLoop
            }
        }
    }
    return mostHigh.max()!!
}

tailrec fun calculateRecursion(
    movingCount: Int,
    index: Int,
    sum: Long,
    max: Long,
    pn: List<Int>,
    cn: List<Long>
): Long {
    return when (movingCount) {
        0 -> 0
        1 -> {
            kotlin.math.max(max, sum + cn[index - 1])
        }
        else -> {
            val newSum = sum + cn[index - 1]
            val newIndex = pn[index - 1]
            val newMax = kotlin.math.max(newSum, max)
            calculateRecursion(movingCount - 1, newIndex, newSum, newMax, pn, cn)
        }
    }
}

fun createTrack(
    pn: List<Int>
): List<List<Int>> {
    tailrec fun innerFunction(
        innerIndex: Int = 1,
        innerEmptyPn: List<Int>,
        innerTrack: List<Int>,
        innerProduct: List<List<Int>>
    ): List<List<Int>> {
        return if (innerEmptyPn.isEmpty()) {
            listOf(innerTrack, *innerProduct.toTypedArray())
        } else {
            val isTrackFinished = innerTrack.contains(pn[innerIndex - 1])

            val newIndex = if (isTrackFinished) innerEmptyPn[0] else pn[innerIndex - 1]
            val newTrack = if (isTrackFinished) listOf() else listOf(newIndex, *innerTrack.toTypedArray())
            val newProduct = if (isTrackFinished) {
                listOf(innerTrack, *innerProduct.toTypedArray())
            } else {
                innerProduct
            }

            val newEmptyPn = innerEmptyPn.filterNot { it == pn[innerIndex - 1] }
            innerFunction(newIndex, newEmptyPn, newTrack, newProduct)
        }
    }
    return innerFunction(1, pn, listOf(), listOf())
}


fun nextInt(): Int {
    return readLine()!!.toInt()
}

fun nextIntList(): List<Int> {
    return readLine()!!.split(" ").map { it.toInt() }
}

fun nextLongList(): List<Long> {
    return readLine()!!.split(" ").map { it.toLong() }
}

fun nextString(): String {
    return readLine()!!
}

Measured memory_bytes: 62724