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

ans = -10**18
for start in range(N):
    now = start
    cycle = [0]

    while True:
        now = P[now] - 1
        cycle.append(cycle[-1] + C[now])

        if now == start: break

    L = len(cycle)-1
    if cycle[-1] < 0 or K <= L:
        score = max(cycle[1:min(L+1,K)+1])
    else:
        score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1])
        score2 = cycle[-1]*(K//L-1) + max(cycle)
        score = max(score1, score2)

    ans = max(ans, score)

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 ans = -10**18 ans -10**18 -10 18 for start in range(N):
    now = start
    cycle = [0]

    while True:
        now = P[now] - 1
        cycle.append(cycle[-1] + C[now])

        if now == start: break

    L = len(cycle)-1
    if cycle[-1] < 0 or K <= L:
        score = max(cycle[1:min(L+1,K)+1])
    else:
        score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1])
        score2 = cycle[-1]*(K//L-1) + max(cycle)
        score = max(score1, score2)

    ans = max(ans, score) start range(N) range N now = start now start cycle = [0] cycle [0] 0 while True:
        now = P[now] - 1
        cycle.append(cycle[-1] + C[now])

        if now == start: break

     True now = P[now] - 1 now P[now] - 1 P[now] P now 1 cycle.append(cycle[-1] + C[now]) cycle.append cycle append cycle[-1] + C[now] cycle[-1] cycle -1 C[now] C now if now == start: break

     now == start now start L = len(cycle)-1 L len(cycle)-1 len(cycle) len cycle 1 if cycle[-1] < 0 or K <= L:
        score = max(cycle[1:min(L+1,K)+1])
    else:
        score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1])
        score2 = cycle[-1]*(K//L-1) + max(cycle)
        score = max(score1, score2)

     cycle[-1] < 0 or K <= L cycle[-1] < 0 cycle[-1] cycle -1 0 K <= L K L score = max(cycle[1:min(L+1,K)+1]) score max(cycle[1:min(L+1,K)+1]) max cycle[1:min(L+1,K)+1] cycle 1:min(L+1,K)+1 1 min(L+1,K)+1 min(L+1,K) min L+1 L 1 K 1 score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1]) score1 cycle[-1]*(K//L) + max(cycle[:K%L+1]) cycle[-1]*(K//L) cycle[-1] cycle -1 K//L K L max(cycle[:K%L+1]) max cycle[:K%L+1] cycle :K%L+1 K%L+1 K%L K L 1 score2 = cycle[-1]*(K//L-1) + max(cycle) score2 cycle[-1]*(K//L-1) + max(cycle) cycle[-1]*(K//L-1) cycle[-1] cycle -1 K//L-1 K//L K L 1 max(cycle) max cycle score = max(score1, score2) score max(score1, score2) max score1 score2 ans = max(ans, score) ans max(ans, score) max ans score print(ans) print ans score2 = cycle[-1]*(K//L-1) + max(cycle) cycle[-1]*(K//L-1) + max(cycle) score2 C = list(map(int,input().split())) list(map(int,input().split())) C cycle = [0] [0] cycle N, K = map(int,input().split()) map(int,input().split()) N now = P[now] - 1 P[now] - 1 now L = len(cycle)-1 len(cycle)-1 L ans = -10**18 -10**18 ans now = start start now score = max(score1, score2) max(score1, score2) score K = map(int,input().split()) map(int,input().split()) K ans = max(ans, score) max(ans, score) ans score = max(cycle[1:min(L+1,K)+1]) max(cycle[1:min(L+1,K)+1]) score P = list(map(int,input().split())) list(map(int,input().split())) P score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1]) cycle[-1]*(K//L) + max(cycle[:K%L+1]) score1
N, K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = -10**18
for start in range(N):
    now = start
    cycle = [0]

    while True:
        now = P[now] - 1
        cycle.append(cycle[-1] + C[now])

        if now == start: break

    L = len(cycle)-1
    if cycle[-1] < 0 or K <= L:
        score = max(cycle[1:min(L+1,K)+1])
    else:
        score1 = cycle[-1]*(K//L) + max(cycle[:K%L+1])
        score2 = cycle[-1]*(K//L-1) + max(cycle)
        score = max(score1, score2)

    ans = max(ans, score)

print(ans)