func main() {
    let nk = readLine()!.split(separator: " ").map{Int($0)!}
    let pList = readLine()!.split(separator: " ").map{Int($0)!}
    let cList = readLine()!.split(separator: " ").map{Int($0)!}
    var arrayOfMaxScore = [Int]()
    for n in 1...nk[0] {
        // starting from n
        var k = 1
        var maxScore = cList[n-1]
        var score = cList[n-1]
        var nextPosition = pList[n-1]
        k += 1
        while k <= nk[1] {
            score += cList[nextPosition-1]
            nextPosition = pList[nextPosition-1]
            if maxScore < score {
                maxScore = score
            }
            k += 1
        }
        arrayOfMaxScore.append(maxScore)
    }
    print(arrayOfMaxScore.max()!)
    return
}

main()

