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

max_score = -1 * float('inf')

for start_pos in range(N):
    step_score = []
    pos = start_pos
    loop_num = 0
    loop_sum = 0
    step = 0
    while (step < K):
        pos = P[pos]
        loop_sum += C[pos]
        step_score.append(loop_sum)
        step += 1
        if pos == start_pos:
            break
    loop_num = step
    step_max_score = max(step_score)
    if loop_num < K:
        if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
        if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
    max_score = max(step_max_score, max_score)
    #print(start_pos, step_max_score, max(step_score), loop_sum, loop_num)

print(max_score) 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 max_score = -1 * float('inf') max_score -1 * float('inf') -1 float('inf') float 'inf' for start_pos in range(N):
    step_score = []
    pos = start_pos
    loop_num = 0
    loop_sum = 0
    step = 0
    while (step < K):
        pos = P[pos]
        loop_sum += C[pos]
        step_score.append(loop_sum)
        step += 1
        if pos == start_pos:
            break
    loop_num = step
    step_max_score = max(step_score)
    if loop_num < K:
        if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
        if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
    max_score = max(step_max_score, max_score)
    #print(start_pos, step_max_score, max(step_score), loop_sum, loop_num) start_pos range(N) range N step_score = [] step_score [] pos = start_pos pos start_pos loop_num = 0 loop_num 0 loop_sum = 0 loop_sum 0 step = 0 step 0 while (step < K):
        pos = P[pos]
        loop_sum += C[pos]
        step_score.append(loop_sum)
        step += 1
        if pos == start_pos:
            break
     step < K step K pos = P[pos] pos P[pos] P pos loop_sum += C[pos] loop_sum C[pos] C pos step_score.append(loop_sum) step_score.append step_score append loop_sum step += 1 step 1 if pos == start_pos:
            break
     pos == start_pos pos start_pos break loop_num = step loop_num step step_max_score = max(step_score) step_max_score max(step_score) max step_score if loop_num < K:
        if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
        if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
     loop_num < K loop_num K if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
         K // loop_num > 0 K // loop_num K loop_num 0 if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
         K % loop_num != 0 K % loop_num K loop_num 0 step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                ) step_max_score max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                ) max step_max_score max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num) max(step_score[:(K % loop_num)]) max step_score[:(K % loop_num)] step_score :(K % loop_num) K % loop_num K loop_num loop_sum * (K // loop_num) loop_sum K // loop_num K loop_num step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                ) step_max_score max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                ) max step_max_score loop_sum * (K // loop_num) loop_sum K // loop_num K loop_num if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
     K // loop_num > 1 K // loop_num K loop_num 1 step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            ) step_max_score max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            ) max step_max_score max(step_score) + loop_sum * (K // loop_num - 1) max(step_score) max step_score loop_sum * (K // loop_num - 1) loop_sum K // loop_num - 1 K // loop_num K loop_num 1 max_score = max(step_max_score, max_score) max_score max(step_max_score, max_score) max step_max_score max_score print(max_score) print max_score step_max_score = max(step_score) max(step_score) step_max_score N, K = list(map(int, input().split())) list(map(int, input().split())) N step = 0 0 step pos = P[pos] P[pos] pos step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                ) max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                ) step_max_score step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            ) max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            ) step_max_score max_score = -1 * float('inf') -1 * float('inf') max_score loop_num = step step loop_num pos = start_pos start_pos pos step += 1 1 step loop_sum += C[pos] C[pos] loop_sum step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                ) max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                ) step_max_score C = list(map(int, input().split())) list(map(int, input().split())) C max_score = max(step_max_score, max_score) max(step_max_score, max_score) max_score P = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) P K = list(map(int, input().split())) list(map(int, input().split())) K step_score = [] [] step_score loop_sum = 0 0 loop_sum loop_num = 0 0 loop_num
N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

max_score = -1 * float('inf')

for start_pos in range(N):
    step_score = []
    pos = start_pos
    loop_num = 0
    loop_sum = 0
    step = 0
    while (step < K):
        pos = P[pos]
        loop_sum += C[pos]
        step_score.append(loop_sum)
        step += 1
        if pos == start_pos:
            break
    loop_num = step
    step_max_score = max(step_score)
    if loop_num < K:
        if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
        if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
    max_score = max(step_max_score, max_score)
    #print(start_pos, step_max_score, max(step_score), loop_sum, loop_num)

print(max_score)