func readTwoInts(separator: String.Element = " ") -> (a: Int, b: Int) {
    let ints = readLine()!.split(separator: separator).map { Int($0)! }
    return (a: ints[0], b: ints[1])
}

func readInts(separator: String.Element = " ") -> [Int] {
    return readLine()!.split(separator: separator).map { Int($0)! }
}

func mainD() {
    let (N, K) = readTwoInts()
    let nextPoints = readInts()
    let scores = readInts()

    var answers: [[Int]] = .init(repeating:[], count: N)
    var remainCount: [Int] = .init(repeating: 0, count: N)
    var roopPositions: [[Int]] = .init(repeating: [], count: N)
    for i in 1...N {
        var roop = Set<Int>()
        var position = i

        for j in 1...K {
            let previousCount = roop.count
            roop.insert(position)
            let currentCount = roop.count

            // ループに入ったら終わり
            if previousCount == currentCount {
                remainCount[i - 1] = K - j + 1

                var roop2 = Set<Int>()

                var previous = nextPoints[position - 1]
                while true {
                    let previousCount = roop2.count
                    roop2.insert(previous)
                    let currentCount = roop2.count

                    // ループの終了
                    if previousCount == currentCount {
                        break
                    }

                    roopPositions[i - 1].append(previous)

                    previous = nextPoints[previous - 1]
                }

                break
            }

            let nextPosition = nextPoints[position - 1]

            if j == 1 {
                answers[i - 1].append(scores[nextPosition - 1])
            } else {
                answers[i - 1].append((answers[i - 1][j - 2] + scores[nextPosition - 1]))
            }
            position = nextPosition
        }
    }

    // 集計処理

    remainCount.enumerated().forEach { (index, remain) in
        if remain != 0 {
            let additional = roopPositions[index].reduce(0) { $0 + scores[$1 - 1] } * Int(remain / roopPositions[index].count)
            answers[index].append(answers[index].last! + additional)

            let leftCount = remain % roopPositions[index].count
            for i in 0..<leftCount {
                answers[index][K - remain] += scores[roopPositions[index][i] - 1]
            }
        }
    }

    // answersは 大きくてもN * N
    let eachNMax = answers.map { $0.max()! }
    print(eachNMax.max()!)
}
mainD()