#!/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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
     start range(N) range N now = start now start loop = [] loop [] total = 0 total 0 while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
         now != start or not loop now != start now start not loop loop loop.append(C[now]) loop.append loop append C[now] C now now = P[now] now P[now] P now total += C[now] total C[now] C now length = len(loop) length len(loop) len loop t = 0 t 0 for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
     i range(length) range length t += loop[i] t loop[i] loop i if i + 1 > K:
                break
             i + 1 > K i + 1 i 1 K break now = t now t if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
             total > 0 total 0 e = (K - (i + 1)) // length e (K - (i + 1)) // length K - (i + 1) K i + 1 i 1 length now += total * e now total * e total e ans = max(ans, now) ans max(ans, now) max ans now 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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    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
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()
