import java.io.IOException
import java.lang.NumberFormatException
import java.lang.StringBuilder
import kotlin.math.max
import kotlin.math.min

object DMain{
    fun solve(){
        val n = nextInt()
        val k = nextInt()
        val p = IntArray(n){ nextInt() - 1}
        val c = LongArray(n){ nextLong() }

        var ans = Long.MIN_VALUE
        for (i in 0 until n){
            var sum = 0L
            var mmax = Long.MIN_VALUE
            var pos = i
            var num = 0
            while(true){
                num++
                pos = p[pos]
                sum += c[pos]
                mmax = max(mmax, sum)
                if(pos == i) break
            }
            if(num == 1){
                ans = max(ans, max(sum, sum * k))
            }
            if(num >= k || sum <= 0){
                mmax = Long.MIN_VALUE
                sum = 0L
                pos = i
                for (i in 1..min(num, k)){
                    pos = p[pos]
                    sum += c[pos]
                    mmax = max(mmax, sum)
                }
                ans = max(ans, mmax)
            }else{
                val msum = sum
                val mmmax = mmax
                mmax = Long.MIN_VALUE
                sum = 0L
                pos = i
                for(i in 1..k%num){
                    pos = p[pos]
                    sum += c[pos]
                    mmax = max(mmax, sum)
                }
                ans = max(ans, max(mmax + msum * (k / num), mmmax + msum * (k / num - 1)))
            }
        }
        println(ans)

    }

    // Scanner based on uwi-san's Java code
    val stream = System.`in`
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0

    fun hasNextByte(): Boolean{
        if(ptr < buflen){
            return true
        }else{
            ptr = 0
            try{
                buflen = stream.read(buffer)
            }catch (e: IOException){
                e.printStackTrace()
            }
            if(buflen <= 0){
                return false
            }
        }
        return true
    }

    fun readByte() =
            if(hasNextByte()) buffer[ptr++]
            else -1

    fun isPrintableChar(c: Byte) = c in 33..126

    fun hasNext(): Boolean{
        while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
        return hasNextByte()
    }

    fun next(): String{
        if(!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while(isPrintableChar(b)){
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }

    fun nextLong(): Long{
        if(!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if(b == '-'.toByte()){
            minus = true
            b = readByte()
        }
        if(b !in '0'.toByte()..'9'.toByte())
            throw NumberFormatException()
        while(true){
            if(b in '0'.toByte()..'9'.toByte()){
                n *= 10
                n += b - '0'.toByte()
            }else if(!isPrintableChar(b)){
                return if(minus) -n else n
            }else{
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextInt(): Int{
        val n1 = nextLong()
        if(n1 !in Int.MIN_VALUE..Int.MAX_VALUE)
            throw NumberFormatException()
        return n1.toInt()
    }

    fun NextDouble() = next().toDouble()
}

fun main(){
    DMain.solve()
}


Measured memory_bytes: 35348