N, K = map(int, input().split())
P = list(map(int, input().split()))
for i in range(N):
    P[i] -= 1
C = list(map(int, input().split()))
 
ans = -(10 ** 18)
 
for si in range(N):
    x = si
    loop = []
    total = 0
    while 1:
        x = P[x]
        loop.append(C[x])
        total += C[x]
        if x == si:
            break
    T = len(loop)  # 周期の大きさ
    tmp = 0
    for i in range(T):
        tmp += loop[i]
        if K < i + 1:
            break
        now = tmp
        if total > 0:  # loopの和が正ならloopを回した方がお得
            loop_times = (K - (i + 1)) // T  # 何周できるか
            now += total * loop_times
        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 for i in range(N):
    P[i] -= 1 i range(N) range N P[i] -= 1 P[i] P i 1 C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -(10 ** 18) ans -(10 ** 18) 10 ** 18 10 18 for si in range(N):
    x = si
    loop = []
    total = 0
    while 1:
        x = P[x]
        loop.append(C[x])
        total += C[x]
        if x == si:
            break
    T = len(loop)  # 周期の大きさ
    tmp = 0
    for i in range(T):
        tmp += loop[i]
        if K < i + 1:
            break
        now = tmp
        if total > 0:  # loopの和が正ならloopを回した方がお得
            loop_times = (K - (i + 1)) // T  # 何周できるか
            now += total * loop_times
        ans = max(ans, now)
  si range(N) range N x = si x si loop = [] loop [] total = 0 total 0 while 1:
        x = P[x]
        loop.append(C[x])
        total += C[x]
        if x == si:
            break
     1 x = P[x] x P[x] P x loop.append(C[x]) loop.append loop append C[x] C x total += C[x] total C[x] C x if x == si:
            break
     x == si x si break T = len(loop) T len(loop) len loop tmp = 0 tmp 0 for i in range(T):
        tmp += loop[i]
        if K < i + 1:
            break
        now = tmp
        if total > 0:  # loopの和が正ならloopを回した方がお得
            loop_times = (K - (i + 1)) // T  # 何周できるか
            now += total * loop_times
        ans = max(ans, now)
  i range(T) range T tmp += loop[i] tmp loop[i] loop i if K < i + 1:
            break
         K < i + 1 K i + 1 i 1 break now = tmp now tmp if total > 0:  # loopの和が正ならloopを回した方がお得
            loop_times = (K - (i + 1)) // T  # 何周できるか
            now += total * loop_times
         total > 0 total 0 loop_times = (K - (i + 1)) // T loop_times (K - (i + 1)) // T K - (i + 1) K i + 1 i 1 T now += total * loop_times now total * loop_times total loop_times ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans ans = max(ans, now) max(ans, now) ans T = len(loop) len(loop) T loop_times = (K - (i + 1)) // T (K - (i + 1)) // T loop_times P = list(map(int, input().split())) list(map(int, input().split())) P K = map(int, input().split()) map(int, input().split()) K loop = [] [] loop tmp = 0 0 tmp total += C[x] C[x] total x = P[x] P[x] x tmp += loop[i] loop[i] tmp now += total * loop_times total * loop_times now N, K = map(int, input().split()) map(int, input().split()) N x = si si x total = 0 0 total C = list(map(int, input().split())) list(map(int, input().split())) C ans = -(10 ** 18) -(10 ** 18) ans now = tmp tmp now
N, K = map(int, input().split())
P = list(map(int, input().split()))
for i in range(N):
    P[i] -= 1
C = list(map(int, input().split()))
 
ans = -(10 ** 18)
 
for si in range(N):
    x = si
    loop = []
    total = 0
    while 1:
        x = P[x]
        loop.append(C[x])
        total += C[x]
        if x == si:
            break
    T = len(loop)  # 周期の大きさ
    tmp = 0
    for i in range(T):
        tmp += loop[i]
        if K < i + 1:
            break
        now = tmp
        if total > 0:  # loopの和が正ならloopを回した方がお得
            loop_times = (K - (i + 1)) // T  # 何周できるか
            now += total * loop_times
        ans = max(ans, now)
 
print(ans)