import sys

input = sys.stdin.readline
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
 
ans = -float('inf')
for s in range(N):
    S = [] 
    i = P[s] - 1
    S.append(C[i])
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
 
    if K <= len(S):
        tmp = max(S[:K])
    elif S[-1] <= 0:
        tmp = max(S)
    else:
        # ループの中で最大となるところで止める
        tmp1 = S[-1] * (K // len(S) - 1)
        tmp1 += max(S)
        # 限界まで進む
        tmp2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            tmp2 += max(0, max(S[:r]))
        tmp = max(tmp1, tmp2)
 
    ans = max(ans, tmp)
 
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 = -float('inf') ans -float('inf') float('inf') float 'inf' for s in range(N):
    S = [] 
    i = P[s] - 1
    S.append(C[i])
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
 
    if K <= len(S):
        tmp = max(S[:K])
    elif S[-1] <= 0:
        tmp = max(S)
    else:
        # ループの中で最大となるところで止める
        tmp1 = S[-1] * (K // len(S) - 1)
        tmp1 += max(S)
        # 限界まで進む
        tmp2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            tmp2 += max(0, max(S[:r]))
        tmp = max(tmp1, tmp2)
 
    ans = max(ans, tmp)
  s range(N) range N S = [] S [] i = P[s] - 1 i P[s] - 1 P[s] P s 1 S.append(C[i]) S.append S append C[i] C i while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
 
     i != s i s i = P[i] - 1 i P[i] - 1 P[i] P i 1 S.append(S[-1] + C[i]) S.append S append S[-1] + C[i] S[-1] S -1 C[i] C i if K <= len(S):
        tmp = max(S[:K])
    elif S[-1] <= 0:
        tmp = max(S)
    else:
        # ループの中で最大となるところで止める
        tmp1 = S[-1] * (K // len(S) - 1)
        tmp1 += max(S)
        # 限界まで進む
        tmp2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            tmp2 += max(0, max(S[:r]))
        tmp = max(tmp1, tmp2)
 
     K <= len(S) K len(S) len S tmp = max(S[:K]) tmp max(S[:K]) max S[:K] S :K K elif S[-1] <= 0:
        tmp = max(S)
     S[-1] <= 0 S[-1] S -1 0 tmp = max(S) tmp max(S) max S tmp1 = S[-1] * (K // len(S) - 1) tmp1 S[-1] * (K // len(S) - 1) S[-1] S -1 K // len(S) - 1 K // len(S) K len(S) len S 1 tmp1 += max(S) tmp1 max(S) max S tmp2 = S[-1] * (K // len(S)) tmp2 S[-1] * (K // len(S)) S[-1] S -1 K // len(S) K len(S) len S r = K % len(S) r K % len(S) K len(S) len S if r != 0:
            tmp2 += max(0, max(S[:r]))
         r != 0 r 0 tmp2 += max(0, max(S[:r])) tmp2 max(0, max(S[:r])) max 0 max(S[:r]) max S[:r] S :r r tmp = max(tmp1, tmp2) tmp max(tmp1, tmp2) max tmp1 tmp2 ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans tmp2 = S[-1] * (K // len(S)) S[-1] * (K // len(S)) tmp2 ans = max(ans, tmp) max(ans, tmp) ans tmp2 += max(0, max(S[:r])) max(0, max(S[:r])) tmp2 tmp = max(S[:K]) max(S[:K]) tmp i = P[i] - 1 P[i] - 1 i i = P[s] - 1 P[s] - 1 i tmp1 += max(S) max(S) tmp1 r = K % len(S) K % len(S) r S = [] [] S tmp = max(S) max(S) tmp N, K = map(int, input().split()) map(int, input().split()) N input = sys.stdin.readline sys.stdin.readline input C = list(map(int, input().split())) list(map(int, input().split())) C tmp = max(tmp1, tmp2) max(tmp1, tmp2) tmp tmp1 = S[-1] * (K // len(S) - 1) S[-1] * (K // len(S) - 1) tmp1 K = map(int, input().split()) map(int, input().split()) K P = list(map(int, input().split())) list(map(int, input().split())) P ans = -float('inf') -float('inf') ans
import sys

input = sys.stdin.readline
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
 
ans = -float('inf')
for s in range(N):
    S = [] 
    i = P[s] - 1
    S.append(C[i])
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
 
    if K <= len(S):
        tmp = max(S[:K])
    elif S[-1] <= 0:
        tmp = max(S)
    else:
        # ループの中で最大となるところで止める
        tmp1 = S[-1] * (K // len(S) - 1)
        tmp1 += max(S)
        # 限界まで進む
        tmp2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            tmp2 += max(0, max(S[:r]))
        tmp = max(tmp1, tmp2)
 
    ans = max(ans, tmp)
 
print(ans)