N, K = map(int, input().split())

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

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

ans = float("-inf")
for i in range(N):
    cycle = []
    pos = i
    total = 0
    while True:
        pos = P[pos]
        cycle.append(C[pos])
        total += C[pos]
        if pos == i: break

    l = len(cycle)
    t = 0
    for j in range(l):
        t += cycle[j]
        if j + 1 > K: break
        now = t

        if total > 0:
            cnt = (K-j-1) // l
            now += total * cnt
        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 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(len(P)):
    P[i] -= 1 i range(len(P)) range len(P) len P P[i] -= 1 P[i] P i 1 ans = float("-inf") ans float("-inf") float "-inf" for i in range(N):
    cycle = []
    pos = i
    total = 0
    while True:
        pos = P[pos]
        cycle.append(C[pos])
        total += C[pos]
        if pos == i: break

    l = len(cycle)
    t = 0
    for j in range(l):
        t += cycle[j]
        if j + 1 > K: break
        now = t

        if total > 0:
            cnt = (K-j-1) // l
            now += total * cnt
        ans = max(ans, now) i range(N) range N cycle = [] cycle [] pos = i pos i total = 0 total 0 while True:
        pos = P[pos]
        cycle.append(C[pos])
        total += C[pos]
        if pos == i: break

     True pos = P[pos] pos P[pos] P pos cycle.append(C[pos]) cycle.append cycle append C[pos] C pos total += C[pos] total C[pos] C pos if pos == i: break

     pos == i pos i l = len(cycle) l len(cycle) len cycle t = 0 t 0 for j in range(l):
        t += cycle[j]
        if j + 1 > K: break
        now = t

        if total > 0:
            cnt = (K-j-1) // l
            now += total * cnt
        ans = max(ans, now) j range(l) range l t += cycle[j] t cycle[j] cycle j if j + 1 > K: break
         j + 1 > K j + 1 j 1 K now = t now t if total > 0:
            cnt = (K-j-1) // l
            now += total * cnt
         total > 0 total 0 cnt = (K-j-1) // l cnt (K-j-1) // l K-j-1 K-j K j 1 l now += total * cnt now total * cnt total cnt ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans K = map(int, input().split()) map(int, input().split()) K ans = max(ans, now) max(ans, now) ans P = list(map(int, input().split())) list(map(int, input().split())) P pos = i i pos total += C[pos] C[pos] total t = 0 0 t now = t t now cnt = (K-j-1) // l (K-j-1) // l cnt total = 0 0 total N, K = map(int, input().split()) map(int, input().split()) N now += total * cnt total * cnt now ans = float("-inf") float("-inf") ans cycle = [] [] cycle pos = P[pos] P[pos] pos l = len(cycle) len(cycle) l C = list(map(int, input().split())) list(map(int, input().split())) C t += cycle[j] cycle[j] t
N, K = map(int, input().split())

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

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

ans = float("-inf")
for i in range(N):
    cycle = []
    pos = i
    total = 0
    while True:
        pos = P[pos]
        cycle.append(C[pos])
        total += C[pos]
        if pos == i: break

    l = len(cycle)
    t = 0
    for j in range(l):
        t += cycle[j]
        if j + 1 > K: break
        now = t

        if total > 0:
            cnt = (K-j-1) // l
            now += total * cnt
        ans = max(ans, now)
print(ans)             