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

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res) 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(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 res = -10**10 res -10**10 -10 10 for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand i range(N) range N j = i j i total = 0 total 0 loop_values = [] loop_values [] while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
     True j = P[j] - 1 j P[j] - 1 P[j] P j 1 loop_values.append(C[j]) loop_values.append loop_values append C[j] C j total += C[j] total C[j] C j if j == i:
            break
    
     j == i j i break loop_length = len(loop_values) loop_length len(loop_values) len loop_values point_sum = 0 point_sum 0 for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand j range(loop_length) range loop_length point_sum += loop_values[j] point_sum loop_values[j] loop_values j result_cand = point_sum result_cand point_sum if j+1 > K:
            break

         j+1 > K j+1 j 1 K break if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
         total > 0 total 0 loop_num = (K - j - 1) // loop_length loop_num (K - j - 1) // loop_length K - j - 1 K - j K j 1 loop_length result_cand += total * loop_num result_cand total * loop_num total loop_num if res < result_cand:
            res = result_cand res < result_cand res result_cand res = result_cand res result_cand print(res) print res res = -10**10 -10**10 res C = list(map(int, input().split())) list(map(int, input().split())) C j = P[j] - 1 P[j] - 1 j j = i i j loop_values = [] [] loop_values P = list(map(int, input().split())) list(map(int, input().split())) P result_cand = point_sum point_sum result_cand K = list(map(int, input().split())) list(map(int, input().split())) K loop_length = len(loop_values) len(loop_values) loop_length point_sum = 0 0 point_sum total = 0 0 total total += C[j] C[j] total result_cand += total * loop_num total * loop_num result_cand loop_num = (K - j - 1) // loop_length (K - j - 1) // loop_length loop_num point_sum += loop_values[j] loop_values[j] point_sum N, K = list(map(int, input().split())) list(map(int, input().split())) N res = result_cand result_cand res
N, K = list(map(int, input().split()))
P = list(map(int, input().split()))
C = list(map(int, input().split()))

res = -10**10

for i in range(N):
    j = i
    total = 0
    loop_values = []

    while True:
        j = P[j] - 1
        loop_values.append(C[j])
        total += C[j]
        if j == i:
            break
    
    loop_length = len(loop_values)
    point_sum = 0

    for j in range(loop_length):
        point_sum += loop_values[j]
        result_cand = point_sum

        if j+1 > K:
            break

        if total > 0:
            loop_num = (K - j - 1) // loop_length
            result_cand += total * loop_num
        
        if res < result_cand:
            res = result_cand

print(res)