N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = list(map(lambda x:x-1, P))

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


print(ans) N, K = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input 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 ans = -float('inf') ans -float('inf') float('inf') float 'inf' for si in range(N):
    s = []
    tot = 0
    x = si
    while True:
        x = P[x]
        s.append(C[x])
        tot += C[x]
        if x==si:
            break
    l = len(s)
    now = 0
    t = 0
    for i in range(l):
        if i+1 > K: break
        t += s[i]
        now = t
        if tot >0:
            e = (K-(i+1))//l
            now += tot * e
        ans = max(ans, now) si range(N) range N s = [] s [] tot = 0 tot 0 x = si x si 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 now = 0 now 0 t = 0 t 0 for i in range(l):
        if i+1 > K: break
        t += s[i]
        now = t
        if tot >0:
            e = (K-(i+1))//l
            now += tot * e
        ans = max(ans, now) i range(l) range l if i+1 > K: break
         i+1 > K i+1 i 1 K t += s[i] t s[i] s i 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+1 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 K = list(map(int, input().split())) list(map(int, input().split())) K s = [] [] s tot += C[x] C[x] tot ans = max(ans, now) max(ans, now) ans now = t t now e = (K-(i+1))//l (K-(i+1))//l e t += s[i] s[i] t now = 0 0 now P = list(map(int, input().split())) list(map(int, input().split())) P C = list(map(int, input().split())) list(map(int, input().split())) C t = 0 0 t now += tot * e tot * e now l = len(s) len(s) l tot = 0 0 tot N, K = list(map(int, input().split())) list(map(int, input().split())) N P = list(map(lambda x:x-1, P)) list(map(lambda x:x-1, P)) P x = si si x x = P[x] P[x] x ans = -float('inf') -float('inf') ans
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = list(map(lambda x:x-1, P))

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


print(ans)