N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
loop_flags = [False] * N
loops = []
for i in range(N):
    if loop_flags[i]:
        continue
    next_cell = i
    loop = []
    while not loop_flags[next_cell]:
        loop_flags[next_cell] = True
        loop.append(C[next_cell])
        next_cell = P[next_cell] - 1
    if loop:
        loops.append(loop)
# print(loops)
ans = C[0]
for loop in loops:
    work_loop_sum = []
    now = 0
    tmp_ans = 0
    one_loop_point = sum(loop)
    if one_loop_point <= 0 or K <= len(loop):
        for i in range(1, min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i])
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
    else:
        for i in range(min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]

print(ans) 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 loop_flags = [False] * N loop_flags [False] * N [False] False N loops = [] loops [] for i in range(N):
    if loop_flags[i]:
        continue
    next_cell = i
    loop = []
    while not loop_flags[next_cell]:
        loop_flags[next_cell] = True
        loop.append(C[next_cell])
        next_cell = P[next_cell] - 1
    if loop:
        loops.append(loop)
# print(loops) i range(N) range N if loop_flags[i]:
        continue
     loop_flags[i] loop_flags i continue next_cell = i next_cell i loop = [] loop [] while not loop_flags[next_cell]:
        loop_flags[next_cell] = True
        loop.append(C[next_cell])
        next_cell = P[next_cell] - 1
     not loop_flags[next_cell] loop_flags[next_cell] loop_flags next_cell loop_flags[next_cell] = True loop_flags[next_cell] loop_flags next_cell True loop.append(C[next_cell]) loop.append loop append C[next_cell] C next_cell next_cell = P[next_cell] - 1 next_cell P[next_cell] - 1 P[next_cell] P next_cell 1 if loop:
        loops.append(loop)
# print(loops) loop loops.append(loop) loops.append loops append loop ans = C[0] ans C[0] C 0 for loop in loops:
    work_loop_sum = []
    now = 0
    tmp_ans = 0
    one_loop_point = sum(loop)
    if one_loop_point <= 0 or K <= len(loop):
        for i in range(1, min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i])
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
    else:
        for i in range(min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] loop loops work_loop_sum = [] work_loop_sum [] now = 0 now 0 tmp_ans = 0 tmp_ans 0 one_loop_point = sum(loop) one_loop_point sum(loop) sum loop if one_loop_point <= 0 or K <= len(loop):
        for i in range(1, min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i])
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
    else:
        for i in range(min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] one_loop_point <= 0 or K <= len(loop) one_loop_point <= 0 one_loop_point 0 K <= len(loop) K len(loop) len loop for i in range(1, min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i])
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
     i range(1, min(K + 1, len(loop) + 1)) range 1 min(K + 1, len(loop) + 1) min K + 1 K 1 len(loop) + 1 len(loop) len loop 1 tmp_ans = sum(loop[:i]) tmp_ans sum(loop[:i]) sum loop[:i] loop :i i for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
     j range(len(loop)) range len(loop) len loop ans = max(tmp_ans, ans) ans max(tmp_ans, ans) max tmp_ans ans tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans - loop[j] tmp_ans loop[j] loop j loop[(i + j) % len(loop)] loop (i + j) % len(loop) i + j i j len(loop) len loop for i in range(min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] i range(min(K + 1, len(loop) + 1)) range min(K + 1, len(loop) + 1) min K + 1 K 1 len(loop) + 1 len(loop) len loop 1 tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point tmp_ans sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point sum(loop[:i]) sum loop[:i] loop :i i ((K - i) // len(loop)) * one_loop_point (K - i) // len(loop) K - i K i len(loop) len loop one_loop_point for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] j range(len(loop)) range len(loop) len loop ans = max(tmp_ans, ans) ans max(tmp_ans, ans) max tmp_ans ans tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans - loop[j] tmp_ans loop[j] loop j loop[(i + j) % len(loop)] loop (i + j) % len(loop) i + j i j len(loop) len loop print(ans) print ans C = list(map(int, input().split())) list(map(int, input().split())) C ans = max(tmp_ans, ans) max(tmp_ans, ans) ans loops = [] [] loops now = 0 0 now tmp_ans = sum(loop[:i]) sum(loop[:i]) tmp_ans P = list(map(int, input().split())) list(map(int, input().split())) P tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans ans = C[0] C[0] ans N, K = map(int, input().split()) map(int, input().split()) N tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point tmp_ans tmp_ans = 0 0 tmp_ans K = map(int, input().split()) map(int, input().split()) K loop = [] [] loop next_cell = i i next_cell tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans - loop[j] + loop[(i + j) % len(loop)] tmp_ans loop_flags = [False] * N [False] * N loop_flags one_loop_point = sum(loop) sum(loop) one_loop_point next_cell = P[next_cell] - 1 P[next_cell] - 1 next_cell work_loop_sum = [] [] work_loop_sum ans = max(tmp_ans, ans) max(tmp_ans, ans) ans
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
loop_flags = [False] * N
loops = []
for i in range(N):
    if loop_flags[i]:
        continue
    next_cell = i
    loop = []
    while not loop_flags[next_cell]:
        loop_flags[next_cell] = True
        loop.append(C[next_cell])
        next_cell = P[next_cell] - 1
    if loop:
        loops.append(loop)
# print(loops)
ans = C[0]
for loop in loops:
    work_loop_sum = []
    now = 0
    tmp_ans = 0
    one_loop_point = sum(loop)
    if one_loop_point <= 0 or K <= len(loop):
        for i in range(1, min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i])
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]
    else:
        for i in range(min(K + 1, len(loop) + 1)):
            tmp_ans = sum(loop[:i]) + ((K - i) // len(loop)) * one_loop_point
            for j in range(len(loop)):
                ans = max(tmp_ans, ans)
                tmp_ans = tmp_ans - loop[j] + loop[(i + j) % len(loop)]

print(ans)