import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    P = [int(s) - 1 for s in readline().split()]
    C = list(map(int, readline().split()))

    ans = -INF
    for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

    print(ans)
    return


if __name__ == '__main__':
    main() import sys sys read = sys.stdin.read read sys.stdin.read sys.stdin sys stdin read readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline readlines = sys.stdin.readlines readlines sys.stdin.readlines sys.stdin sys stdin readlines sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = 1 << 60 INF 1 << 60 1 60 MOD = 1000000007 MOD 1000000007 def main():
    N, K = map(int, readline().split())
    P = [int(s) - 1 for s in readline().split()]
    C = list(map(int, readline().split()))

    ans = -INF
    for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

    print(ans)
    return main N, K = map(int, readline().split()) N map(int, readline().split()) map int readline().split() ().split () readline split K int(s) - 1 for s in readline().split() for s in readline().split() s readline().split() ().split () readline split for s in readline().split() int(s) - 1 int(s) int s 1 P = [int(s) - 1 for s in readline().split()] P [int(s) - 1 for s in readline().split()] C = list(map(int, readline().split())) C list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split ans = -INF ans -INF INF for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

     i range(N) range N score = [] score [] total = 0 total 0 x = i x i while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

         True x = P[x] x P[x] P x score.append(C[x]) score.append score append C[x] C x total += C[x] total C[x] C x if x == i:
                break

         x == i x i break l = len(score) l len(score) len score csum = 0 csum 0 for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

     i range(l) range l if i + 1 > K:
                break
             i + 1 > K i + 1 i 1 K break csum += score[i] csum score[i] score i res = csum res csum if total > 0:
                res += (K - (i + 1)) // l * total

             total > 0 total 0 res += (K - (i + 1)) // l * total res (K - (i + 1)) // l * total (K - (i + 1)) // l K - (i + 1) K i + 1 i 1 l total if ans < res:
                ans = res

     ans < res ans res ans = res ans res print(ans) print ans return if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main INF = 1 << 60 1 << 60 INF def main():
    N, K = map(int, readline().split())
    P = [int(s) - 1 for s in readline().split()]
    C = list(map(int, readline().split()))

    ans = -INF
    for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

    print(ans)
    return def main():
    N, K = map(int, readline().split())
    P = [int(s) - 1 for s in readline().split()]
    C = list(map(int, readline().split()))

    ans = -INF
    for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

    print(ans)
    return main readline = sys.stdin.readline sys.stdin.readline readline readlines = sys.stdin.readlines sys.stdin.readlines readlines read = sys.stdin.read sys.stdin.read read MOD = 1000000007 1000000007 MOD
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())
    P = [int(s) - 1 for s in readline().split()]
    C = list(map(int, readline().split()))

    ans = -INF
    for i in range(N):
        score = []
        total = 0
        x = i
        while True:
            x = P[x]
            score.append(C[x])
            total += C[x]
            if x == i:
                break

        l = len(score)
        csum = 0
        for i in range(l):
            if i + 1 > K:
                break
            csum += score[i]
            res = csum
            if total > 0:
                res += (K - (i + 1)) // l * total

            if ans < res:
                ans = res

    print(ans)
    return


if __name__ == '__main__':
    main()
