N, K = map(int, input().split())
P = [int(i)-1 for i in input().split()]
C = [int(i) for i in input().split()]


ans = -1e18
for si in range(N):
    x = si
    s = []
    tot = 0
    while True:
        x = P[x]
        s.append(C[x])
        tot += C[x]
        if x == si:
            break
    l = len(s)
    t = 0
    for i in range(l):
        t += s[i]
        if i+1 > K:
            break
        now = t
        if tot > 0:
            e = (K-i-1)//l
            now += tot*e
        ans = max(ans, now)
print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 P = [int(i)-1 for i in input().split()] P [int(i)-1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = [int(i) for i in input().split()] C [int(i) for i in input().split()] ans = -1e18 ans -1e18 for si in range(N):
    x = si
    s = []
    tot = 0
    while True:
        x = P[x]
        s.append(C[x])
        tot += C[x]
        if x == si:
            break
    l = len(s)
    t = 0
    for i in range(l):
        t += s[i]
        if i+1 > K:
            break
        now = t
        if tot > 0:
            e = (K-i-1)//l
            now += tot*e
        ans = max(ans, now) si range(N) range N x = si x si s = [] s [] tot = 0 tot 0 while True:
        x = P[x]
        s.append(C[x])
        tot += C[x]
        if x == si:
            break
     True x = P[x] x P[x] P x s.append(C[x]) s.append s append C[x] C x tot += C[x] tot C[x] C x if x == si:
            break
     x == si x si break l = len(s) l len(s) len s t = 0 t 0 for i in range(l):
        t += s[i]
        if i+1 > K:
            break
        now = t
        if tot > 0:
            e = (K-i-1)//l
            now += tot*e
        ans = max(ans, now) i range(l) range l t += s[i] t s[i] s i if i+1 > K:
            break
         i+1 > K i+1 i 1 K break now = t now t if tot > 0:
            e = (K-i-1)//l
            now += tot*e
         tot > 0 tot 0 e = (K-i-1)//l e (K-i-1)//l K-i-1 K-i K i 1 l now += tot*e now tot*e tot e ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans t += s[i] s[i] t now = t t now x = si si x x = P[x] P[x] x s = [] [] s e = (K-i-1)//l (K-i-1)//l e N, K = map(int, input().split()) map(int, input().split()) N tot = 0 0 tot P = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] P ans = -1e18 -1e18 ans ans = max(ans, now) max(ans, now) ans l = len(s) len(s) l C = [int(i) for i in input().split()] [int(i) for i in input().split()] C now += tot*e tot*e now K = map(int, input().split()) map(int, input().split()) K t = 0 0 t tot += C[x] C[x] tot
N, K = map(int, input().split())
P = [int(i)-1 for i in input().split()]
C = [int(i) for i in input().split()]


ans = -1e18
for si in range(N):
    x = si
    s = []
    tot = 0
    while True:
        x = P[x]
        s.append(C[x])
        tot += C[x]
        if x == si:
            break
    l = len(s)
    t = 0
    for i in range(l):
        t += s[i]
        if i+1 > K:
            break
        now = t
        if tot > 0:
            e = (K-i-1)//l
            now += tot*e
        ans = max(ans, now)
print(ans)
