N, K = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))
ans = -float('inf')
for i in range(1, N+1):
    v = i
    score = 0
    size = 0
    while True:
        size += 1
        score += C[v]
        if i == P[v]:
            break
        v = P[v]

    if score > 0:
        tmp = score*(K//size-1)
        num = K%size+size
    else:
        tmp = 0
        num = min(K, size)
    
    v = i
    for _ in range(num):
        tmp += C[v]
        v = P[v]
        ans = max(tmp, ans)

print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = [0] + list(map(int, input().split())) P [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = [0] + list(map(int, input().split())) C [0] + list(map(int, input().split())) [0] 0 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 i in range(1, N+1):
    v = i
    score = 0
    size = 0
    while True:
        size += 1
        score += C[v]
        if i == P[v]:
            break
        v = P[v]

    if score > 0:
        tmp = score*(K//size-1)
        num = K%size+size
    else:
        tmp = 0
        num = min(K, size)
    
    v = i
    for _ in range(num):
        tmp += C[v]
        v = P[v]
        ans = max(tmp, ans) i range(1, N+1) range 1 N+1 N 1 v = i v i score = 0 score 0 size = 0 size 0 while True:
        size += 1
        score += C[v]
        if i == P[v]:
            break
        v = P[v]

     True size += 1 size 1 score += C[v] score C[v] C v if i == P[v]:
            break
         i == P[v] i P[v] P v break v = P[v] v P[v] P v if score > 0:
        tmp = score*(K//size-1)
        num = K%size+size
    else:
        tmp = 0
        num = min(K, size)
    
     score > 0 score 0 tmp = score*(K//size-1) tmp score*(K//size-1) score K//size-1 K//size K size 1 num = K%size+size num K%size+size K%size K size size tmp = 0 tmp 0 num = min(K, size) num min(K, size) min K size v = i v i for _ in range(num):
        tmp += C[v]
        v = P[v]
        ans = max(tmp, ans) _ range(num) range num tmp += C[v] tmp C[v] C v v = P[v] v P[v] P v ans = max(tmp, ans) ans max(tmp, ans) max tmp ans print(ans) print ans K = map(int, input().split()) map(int, input().split()) K tmp = score*(K//size-1) score*(K//size-1) tmp N, K = map(int, input().split()) map(int, input().split()) N P = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) P ans = -float('inf') -float('inf') ans v = i i v v = P[v] P[v] v num = min(K, size) min(K, size) num tmp = 0 0 tmp v = P[v] P[v] v size += 1 1 size size = 0 0 size v = i i v C = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) C score = 0 0 score score += C[v] C[v] score num = K%size+size K%size+size num tmp += C[v] C[v] tmp ans = max(tmp, ans) max(tmp, ans) ans
N, K = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))
ans = -float('inf')
for i in range(1, N+1):
    v = i
    score = 0
    size = 0
    while True:
        size += 1
        score += C[v]
        if i == P[v]:
            break
        v = P[v]

    if score > 0:
        tmp = score*(K//size-1)
        num = K%size+size
    else:
        tmp = 0
        num = min(K, size)
    
    v = i
    for _ in range(num):
        tmp += C[v]
        v = P[v]
        ans = max(tmp, ans)

print(ans)