from itertools import accumulate 

N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

ans = -10**18-1
seen = [False]*N
for i in range(N):
    if seen[i]:
        continue
    loop = []
    now = i
    while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
    loop_size = len(loop)
    loop_score = sum(loop)
    cumsum = list(accumulate(loop+loop))
    for j in range(1, min(K, loop_size)+1):
        for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size))
print(ans) from itertools import accumulate N, K = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x: int(x) - 1, input().split())) P list(map(lambda x: int(x) - 1, input().split())) list map(lambda x: int(x) - 1, input().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x 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-1 ans -10**18-1 -10**18 -10 18 1 seen = [False]*N seen [False]*N [False] False N for i in range(N):
    if seen[i]:
        continue
    loop = []
    now = i
    while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
    loop_size = len(loop)
    loop_score = sum(loop)
    cumsum = list(accumulate(loop+loop))
    for j in range(1, min(K, loop_size)+1):
        for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) i range(N) range N if seen[i]:
        continue
     seen[i] seen i continue loop = [] loop [] now = i now i while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
     seen[now] == False seen[now] seen now False seen[now] = True seen[now] seen now True loop.append(C[now]) loop.append loop append C[now] C now now = P[now] now P[now] P now loop_size = len(loop) loop_size len(loop) len loop loop_score = sum(loop) loop_score sum(loop) sum loop cumsum = list(accumulate(loop+loop)) cumsum list(accumulate(loop+loop)) list accumulate(loop+loop) accumulate loop+loop loop loop for j in range(1, min(K, loop_size)+1):
        for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) j range(1, min(K, loop_size)+1) range 1 min(K, loop_size)+1 min(K, loop_size) min K loop_size 1 for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) l range(loop_size) range loop_size r = l+j r l+j l j subseq = cumsum[r] - cumsum[l] subseq cumsum[r] - cumsum[l] cumsum[r] cumsum r cumsum[l] cumsum l ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) ans max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) max ans subseq subseq+loop_score*((K-j)//loop_size) subseq loop_score*((K-j)//loop_size) loop_score (K-j)//loop_size K-j K j loop_size print(ans) print ans loop = [] [] loop r = l+j l+j r now = P[now] P[now] now ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) max(ans, subseq, subseq+loop_score*((K-j)//loop_size)) ans cumsum = list(accumulate(loop+loop)) list(accumulate(loop+loop)) cumsum subseq = cumsum[r] - cumsum[l] cumsum[r] - cumsum[l] subseq loop_score = sum(loop) sum(loop) loop_score seen = [False]*N [False]*N seen now = i i now N, K = list(map(int, input().split())) list(map(int, input().split())) N ans = -10**18-1 -10**18-1 ans K = list(map(int, input().split())) list(map(int, input().split())) K loop_size = len(loop) len(loop) loop_size C = list(map(int, input().split())) list(map(int, input().split())) C P = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) P
from itertools import accumulate 

N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

ans = -10**18-1
seen = [False]*N
for i in range(N):
    if seen[i]:
        continue
    loop = []
    now = i
    while seen[now] == False:
        seen[now] = True
        loop.append(C[now])
        now = P[now]
    loop_size = len(loop)
    loop_score = sum(loop)
    cumsum = list(accumulate(loop+loop))
    for j in range(1, min(K, loop_size)+1):
        for l in range(loop_size):
            r = l+j
            subseq = cumsum[r] - cumsum[l]
            ans = max(ans, subseq, subseq+loop_score*((K-j)//loop_size))
print(ans)