import java.io.BufferedReader
import java.util.*
import kotlin.math.max

fun main() = abc175d()

@OptIn(ExperimentalStdlibApi::class)
fun abc175d() = SimpleFastIOWithToken_abc175d().exec {
    val (n, k) = readInt() to readLong()
    val nextIndices = IntArray(n) { readInt() - 1 }
    val points = LongArray(n) { readLong() }

    val cycleSizeByGroup = IntArray(n)
    val incomePerCycleByGroup = LongArray(n)
    val unionFind = UnionFind(n)
    nextIndices.forEachIndexed { from, to -> unionFind.unite(from, to) }

    for (i in (0 until n).map { unionFind.root(it) }.distinct()) {
        var pointPerCycle = 0L
        var currIndex = i
        val visited = BooleanArray(n)
        while (true) {
            if (visited[currIndex]) break
            visited[currIndex] = true
            currIndex = nextIndices[currIndex]
            pointPerCycle += points[currIndex]
        }
        cycleSizeByGroup[i] = visited.count { it }
        incomePerCycleByGroup[i] = pointPerCycle
    }

    var answer = Long.MIN_VALUE

    for (startIndex in 0 until n) {
        val group = unionFind.root(startIndex)
        val cycleSize = cycleSizeByGroup[group]
        val pointPerCycle = incomePerCycleByGroup[group]

        var currIndex = startIndex
        var point = if (pointPerCycle > 0) pointPerCycle * (k / cycleSize - 1) else 0
        repeat((k % cycleSize + cycleSize).toInt()) {
            currIndex = nextIndices[currIndex]
            point += points[currIndex]
            answer = max(answer, point)
        }
    }

    println(answer)
}

private class UnionFind(initSize: Int) {
    private val parent = IntArray(initSize) { it }
    private val size = LongArray(initSize) { 1L }

    fun root(x: Int): Int {
        if (x == parent[x]) return x

        val xr = root(parent[x])
        parent[x] = xr
        return xr
    }

    fun unite(x: Int, y: Int) {
        val xr = root(x)
        val yr = root(y)
        if (xr == yr) return

        if (size[xr] >= size[yr]) {
            size[xr] += size[yr]
            parent[yr] = xr
        } else {
            size[yr] += size[xr]
            parent[xr] = yr
        }
    }

    fun isSame(x: Int, y: Int): Boolean = root(x) == root(y)

    fun unite(pair: Pair<Int, Int>): Unit = unite(pair.first, pair.second)
    fun isSame(pair: Pair<Int, Int>): Boolean = isSame(pair.first, pair.second)

    fun size(x: Int): Long = size[root(x)]
}

// region kokokara template dayo (^o^)
@Suppress("unused", "HasPlatformType", "MemberVisibilityCanBePrivate", "ClassName")
private class SimpleFastIOWithToken_abc175d(private val separator: String = System.lineSeparator()) {
    private val br: BufferedReader = System.`in`.bufferedReader()
    private var st: StringTokenizer = StringTokenizer("")
    private val sb: StringBuilder = StringBuilder()
    private fun prepareNext() = run { if (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) }

    fun readString(): String = run { prepareNext() }.run { st.nextToken() }
    fun readInt(): Int = Integer.valueOf(readString())
    fun readLong(): Long = java.lang.Long.valueOf(readString())

    fun println(): Unit = run { sb.append(separator) }
    fun print(any: Any): Unit = run { sb.append(any) }
    fun println(any: Any): Unit = run { sb.append(any.toString() + separator) }

    fun exec(action: SimpleFastIOWithToken_abc175d.() -> Unit) {
        var t: Throwable? = null
        Thread(null, { action() }, "solve", 128 * 1024 * 1024)
            .apply { setUncaughtExceptionHandler { _, t1 -> t = t1 } }
            .apply { start() }.join()
        t?.let { throw it }
        kotlin.io.print(sb)
    }

    fun readLine(): Nothing = error("readLine is disabled.")
}
// endregion kokomade template dayo (^o^)


Measured memory_bytes: 44032