import kotlin.math.*

fun read(d:String = " ") = readLine()!!.split(d)
fun <T> list(d:String = " ",f:String.()->T) = read(d).map {f(it)}
fun list() = list {toInt()}

fun main() {
	val (n,k) = list()
	val p = list().map {it-1}
	val c = list {toLong()}

	val a = Array(n+1) {LongArray(n)}
	val t = Array(n+1){IntArray(n) {it}}

	for(d in 0 until n) {
		for(i in 0 until n) {
			t[d+1][i] = p[t[d][i]]
			a[d+1][i] = a[d][i]+c[t[d+1][i]]
		}
	}

	var r = Long.MIN_VALUE
	for(d in 1..min(n,k))
		r = max(r,a[d].max()!!)
	if(k>n) {
		for(i in 0 until n) {
			val m=(1..n).first {t[it][i]==i}
			for(d in k-m..k)
				r = max(r,a[d%m][i]+a[m][i]*(d/m))
		}
	}
	println(r)
}
