from sys import stdin
input = stdin.readline

def main():
    N, K = map(int, input().rstrip().split())
    P = list(map(int, input().rstrip().split()))
    P = list(map(lambda x: x -1, P))
    C = list(map(int, input().rstrip().split()))
    ans = -10**18
    for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
    print(ans)

if __name__ == "__main__":
    main()
     from sys import stdin input = stdin.readline input stdin.readline stdin readline def main():
    N, K = map(int, input().rstrip().split())
    P = list(map(int, input().rstrip().split()))
    P = list(map(lambda x: x -1, P))
    C = list(map(int, input().rstrip().split()))
    ans = -10**18
    for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
    print(ans) main N, K = map(int, input().rstrip().split()) N map(int, input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split K P = list(map(int, input().rstrip().split())) P list(map(int, input().rstrip().split())) list map(int, input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split P = list(map(lambda x: x -1, P)) P list(map(lambda x: x -1, P)) list map(lambda x: x -1, P) map lambda x: x -1 x -1 x 1 x P C = list(map(int, input().rstrip().split())) C list(map(int, input().rstrip().split())) list map(int, input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split ans = -10**18 ans -10**18 -10 18 for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
     i range(N) range N x = i x i s = [] s [] total = 0 total 0 while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
         True x = P[x] x P[x] P x s.append(C[x]) s.append s append C[x] C x total += C[x] total C[x] C x if x == i:
                break
         x == i x i break l = len(s) l len(s) len s t = 0 t 0 for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
     j range(l) range l t += s[j] t s[j] s j if j + 1 > K:
                break
             j + 1 > K j + 1 j 1 K break now = t now t if total > 0:
                e = (K - j - 1) // l
                now += total * e
             total > 0 total 0 e = (K - j - 1) // l e (K - j - 1) // l K - j - 1 K - j K j 1 l 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 input = stdin.readline stdin.readline input def main():
    N, K = map(int, input().rstrip().split())
    P = list(map(int, input().rstrip().split()))
    P = list(map(lambda x: x -1, P))
    C = list(map(int, input().rstrip().split()))
    ans = -10**18
    for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
    print(ans) def main():
    N, K = map(int, input().rstrip().split())
    P = list(map(int, input().rstrip().split()))
    P = list(map(lambda x: x -1, P))
    C = list(map(int, input().rstrip().split()))
    ans = -10**18
    for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
    print(ans) main
from sys import stdin
input = stdin.readline

def main():
    N, K = map(int, input().rstrip().split())
    P = list(map(int, input().rstrip().split()))
    P = list(map(lambda x: x -1, P))
    C = list(map(int, input().rstrip().split()))
    ans = -10**18
    for i in range(N):
        x = i
        s = []
        total = 0
        while(True):
            x = P[x]
            s.append(C[x])
            total += C[x]
            if x == i:
                break
        l = len(s)
        t = 0
        for j in range(l):
            t += s[j]
            if j + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - j - 1) // l
                now += total * e
            ans = max(ans, now)
    print(ans)

if __name__ == "__main__":
    main()
    
