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

result = -float('inf')
for i in range(N):
    p = P[i] - 1
    c = C[p]
    k = K - 1
    t = c
    while p != i and k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    if k == 0 or c <= 0:
        result = max(result, t)
        continue
    l = K - k
    c = c * (K // l - 1)
    k = K - (K // l - 1) * l
    t = c
    while k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    result = max(result, t)
print(result) 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 result = -float('inf') result -float('inf') float('inf') float 'inf' for i in range(N):
    p = P[i] - 1
    c = C[p]
    k = K - 1
    t = c
    while p != i and k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    if k == 0 or c <= 0:
        result = max(result, t)
        continue
    l = K - k
    c = c * (K // l - 1)
    k = K - (K // l - 1) * l
    t = c
    while k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    result = max(result, t) i range(N) range N p = P[i] - 1 p P[i] - 1 P[i] P i 1 c = C[p] c C[p] C p k = K - 1 k K - 1 K 1 t = c t c while p != i and k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
     p != i and k != 0 p != i p i k != 0 k 0 p = P[p] - 1 p P[p] - 1 P[p] P p 1 c += C[p] c C[p] C p t = max(t, c) t max(t, c) max t c k -= 1 k 1 if k == 0 or c <= 0:
        result = max(result, t)
        continue
     k == 0 or c <= 0 k == 0 k 0 c <= 0 c 0 result = max(result, t) result max(result, t) max result t continue l = K - k l K - k K k c = c * (K // l - 1) c c * (K // l - 1) c K // l - 1 K // l K l 1 k = K - (K // l - 1) * l k K - (K // l - 1) * l K (K // l - 1) * l K // l - 1 K // l K l 1 l t = c t c while k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
     k != 0 k 0 p = P[p] - 1 p P[p] - 1 P[p] P p 1 c += C[p] c C[p] C p t = max(t, c) t max(t, c) max t c k -= 1 k 1 result = max(result, t) result max(result, t) max result t print(result) print result result = max(result, t) max(result, t) result result = -float('inf') -float('inf') result l = K - k K - k l p = P[i] - 1 P[i] - 1 p c += C[p] C[p] c t = c c t p = P[p] - 1 P[p] - 1 p k -= 1 1 k C = list(map(int, input().split())) list(map(int, input().split())) C k -= 1 1 k t = max(t, c) max(t, c) t t = max(t, c) max(t, c) t t = c c t p = P[p] - 1 P[p] - 1 p result = max(result, t) max(result, t) result c += C[p] C[p] c c = C[p] C[p] c k = K - (K // l - 1) * l K - (K // l - 1) * l k N, K = map(int, input().split()) map(int, input().split()) N K = map(int, input().split()) map(int, input().split()) K c = c * (K // l - 1) c * (K // l - 1) c P = list(map(int, input().split())) list(map(int, input().split())) P k = K - 1 K - 1 k
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

result = -float('inf')
for i in range(N):
    p = P[i] - 1
    c = C[p]
    k = K - 1
    t = c
    while p != i and k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    if k == 0 or c <= 0:
        result = max(result, t)
        continue
    l = K - k
    c = c * (K // l - 1)
    k = K - (K // l - 1) * l
    t = c
    while k != 0:
        p = P[p] - 1
        c += C[p]
        t = max(t, c)
        k -= 1
    result = max(result, t)
print(result)
