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

for i in range(N):
    P[i] -= 1

ans = -10 ** 18
for si in range(N):
    x = si
    c = []
    tot = 0
    while True:
        x = P[x]
        c.append(C[x])
        tot += C[x]
        if x == si:
            break

    s = 0
    for k in range(len(c)):
        if k + 1 > K:
            break
        s += c[k]
        ss = s
        if tot > 0:
            ss += tot * int((K - (k + 1)) // len(c))
        ans = max(ss, ans)

print(ans) N, K = map(int, input().split()) N 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 for i in range(N):
    P[i] -= 1 i range(N) range N P[i] -= 1 P[i] P i 1 ans = -10 ** 18 ans -10 ** 18 -10 18 for si in range(N):
    x = si
    c = []
    tot = 0
    while True:
        x = P[x]
        c.append(C[x])
        tot += C[x]
        if x == si:
            break

    s = 0
    for k in range(len(c)):
        if k + 1 > K:
            break
        s += c[k]
        ss = s
        if tot > 0:
            ss += tot * int((K - (k + 1)) // len(c))
        ans = max(ss, ans) si range(N) range N x = si x si c = [] c [] tot = 0 tot 0 while True:
        x = P[x]
        c.append(C[x])
        tot += C[x]
        if x == si:
            break

     True x = P[x] x P[x] P x c.append(C[x]) c.append c append C[x] C x tot += C[x] tot C[x] C x if x == si:
            break

     x == si x si break s = 0 s 0 for k in range(len(c)):
        if k + 1 > K:
            break
        s += c[k]
        ss = s
        if tot > 0:
            ss += tot * int((K - (k + 1)) // len(c))
        ans = max(ss, ans) k range(len(c)) range len(c) len c if k + 1 > K:
            break
         k + 1 > K k + 1 k 1 K break s += c[k] s c[k] c k ss = s ss s if tot > 0:
            ss += tot * int((K - (k + 1)) // len(c))
         tot > 0 tot 0 ss += tot * int((K - (k + 1)) // len(c)) ss tot * int((K - (k + 1)) // len(c)) tot int((K - (k + 1)) // len(c)) int (K - (k + 1)) // len(c) K - (k + 1) K k + 1 k 1 len(c) len c ans = max(ss, ans) ans max(ss, ans) max ss ans print(ans) print ans P = list(map(int, input().split())) list(map(int, input().split())) P ss = s s ss x = si si x ans = max(ss, ans) max(ss, ans) ans ans = -10 ** 18 -10 ** 18 ans s = 0 0 s s += c[k] c[k] s tot = 0 0 tot K = map(int, input().split()) map(int, input().split()) K x = P[x] P[x] x tot += C[x] C[x] tot c = [] [] c ss += tot * int((K - (k + 1)) // len(c)) tot * int((K - (k + 1)) // len(c)) ss C = list(map(int, input().split())) list(map(int, input().split())) C N, K = map(int, input().split()) map(int, input().split()) N
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

for i in range(N):
    P[i] -= 1

ans = -10 ** 18
for si in range(N):
    x = si
    c = []
    tot = 0
    while True:
        x = P[x]
        c.append(C[x])
        tot += C[x]
        if x == si:
            break

    s = 0
    for k in range(len(c)):
        if k + 1 > K:
            break
        s += c[k]
        ss = s
        if tot > 0:
            ss += tot * int((K - (k + 1)) // len(c))
        ans = max(ss, ans)

print(ans)