#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

    print(ans)


if __name__ == '__main__':
    main() def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

    print(ans) main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(x) - 1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) - 1 int(x) int x 1 P = [int(x) - 1 for x in input().split()] P [int(x) - 1 for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C = [int(x) for x in input().split()] C [int(x) for x in input().split()] ans = -10 ** 9 ans -10 ** 9 -10 9 for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

     start range(N) range N now = start now start cycle = [] cycle [] total = 0 total 0 for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
         _ [0] * N [0] 0 N now = P[now] now P[now] P now cycle.append(C[now]) cycle.append cycle append C[now] C now total += C[now] total C[now] C now if now == start:
                break
        # 最大スコアを探索
         now == start now start break length = len(cycle) length len(cycle) len cycle score = 0 score 0 for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

     move range(length) range length score += cycle[move] score cycle[move] cycle move if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
             move >= K move K break res = score res score if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
             total > 0 total 0 laps = (K - (move + 1)) // length laps (K - (move + 1)) // length K - (move + 1) K move + 1 move 1 length res += total * laps res total * laps total laps ans = max(ans, res) ans max(ans, res) max ans res print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

    print(ans) def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

    print(ans) main
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探索
        now = start
        cycle = []
        total = 0
        for _ in [0] * N:  # while Trueでもおｋ
            now = P[now]
            cycle.append(C[now])
            total += C[now]
            if now == start:
                break
        # 最大スコアを探索
        length = len(cycle)
        score = 0
        for move in range(length):
            score += cycle[move]
            # 移動回数 >= Kなら，ループ途中でも探索終了
            if move >= K:
                break
            # ループを周回するので，それを利用して探索を省略
            res = score
            if total > 0:
                laps = (K - (move + 1)) // length
                res += total * laps
            ans = max(ans, res)

    print(ans)


if __name__ == '__main__':
    main()
