n, k = map(int, input().split())
p_list = list(map(lambda x: int(x) - 1, input().split()))
c_list = list(map(int, input().split()))

max_score = -(1 << 64)
cycles = []
cycled = [False] * n
for i in range(n):
    if cycled[i]:
        continue

    cycle = [i]
    cycled[i] = True
    while p_list[cycle[-1]] != cycle[0]:
        cycle.append(p_list[cycle[-1]])
        cycled[cycle[-1]] = True

    cycles.append(cycle)

for cycle in cycles:
    cycle_len = len(cycle)

    accum = [0]
    for i in range(cycle_len * 2):
        accum.append(accum[-1] + c_list[cycle[i % cycle_len]])

    cycle_score = accum[cycle_len]

    max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)]

    if cycle_len >= k:
        score = max(max_series_sums[1:k + 1])
    elif cycle_score <= 0:
        score = max(max_series_sums[1:])
    else:
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]))

    max_score = max(max_score, score)

print(max_score) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p_list = list(map(lambda x: int(x) - 1, input().split())) p_list 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 = list(map(int, input().split())) c_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split max_score = -(1 << 64) max_score -(1 << 64) 1 << 64 1 64 cycles = [] cycles [] cycled = [False] * n cycled [False] * n [False] False n for i in range(n):
    if cycled[i]:
        continue

    cycle = [i]
    cycled[i] = True
    while p_list[cycle[-1]] != cycle[0]:
        cycle.append(p_list[cycle[-1]])
        cycled[cycle[-1]] = True

    cycles.append(cycle) i range(n) range n if cycled[i]:
        continue

     cycled[i] cycled i continue cycle = [i] cycle [i] i cycled[i] = True cycled[i] cycled i True while p_list[cycle[-1]] != cycle[0]:
        cycle.append(p_list[cycle[-1]])
        cycled[cycle[-1]] = True

     p_list[cycle[-1]] != cycle[0] p_list[cycle[-1]] p_list cycle[-1] cycle -1 cycle[0] cycle 0 cycle.append(p_list[cycle[-1]]) cycle.append cycle append p_list[cycle[-1]] p_list cycle[-1] cycle -1 cycled[cycle[-1]] = True cycled[cycle[-1]] cycled cycle[-1] cycle -1 True cycles.append(cycle) cycles.append cycles append cycle for cycle in cycles:
    cycle_len = len(cycle)

    accum = [0]
    for i in range(cycle_len * 2):
        accum.append(accum[-1] + c_list[cycle[i % cycle_len]])

    cycle_score = accum[cycle_len]

    max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)]

    if cycle_len >= k:
        score = max(max_series_sums[1:k + 1])
    elif cycle_score <= 0:
        score = max(max_series_sums[1:])
    else:
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]))

    max_score = max(max_score, score) cycle cycles cycle_len = len(cycle) cycle_len len(cycle) len cycle accum = [0] accum [0] 0 for i in range(cycle_len * 2):
        accum.append(accum[-1] + c_list[cycle[i % cycle_len]])

     i range(cycle_len * 2) range cycle_len * 2 cycle_len 2 accum.append(accum[-1] + c_list[cycle[i % cycle_len]]) accum.append accum append accum[-1] + c_list[cycle[i % cycle_len]] accum[-1] accum -1 c_list[cycle[i % cycle_len]] c_list cycle[i % cycle_len] cycle i % cycle_len i cycle_len cycle_score = accum[cycle_len] cycle_score accum[cycle_len] accum cycle_len max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1) for length in range(cycle_len + 1) length range(cycle_len + 1) range cycle_len + 1 cycle_len 1 for length in range(cycle_len + 1) max([accum[i + length] - accum[i]
                            for i in range(cycle_len)]) max [accum[i + length] - accum[i]
                            for i in range(cycle_len)] max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)] max_series_sums [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)] if cycle_len >= k:
        score = max(max_series_sums[1:k + 1])
    elif cycle_score <= 0:
        score = max(max_series_sums[1:])
    else:
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]))

     cycle_len >= k cycle_len k score = max(max_series_sums[1:k + 1]) score max(max_series_sums[1:k + 1]) max max_series_sums[1:k + 1] max_series_sums 1:k + 1 1 k + 1 k 1 elif cycle_score <= 0:
        score = max(max_series_sums[1:])
     cycle_score <= 0 cycle_score 0 score = max(max_series_sums[1:]) score max(max_series_sums[1:]) max max_series_sums[1:] max_series_sums 1: 1 max_cycle_num, cycle_rem = divmod(k, cycle_len) max_cycle_num divmod(k, cycle_len) divmod k cycle_len cycle_rem score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1])) score max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1])) max cycle_score * (max_cycle_num - 1) + max(max_series_sums) cycle_score * (max_cycle_num - 1) cycle_score max_cycle_num - 1 max_cycle_num 1 max(max_series_sums) max max_series_sums cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]) cycle_score * max_cycle_num cycle_score max_cycle_num max(max_series_sums[:cycle_rem + 1]) max max_series_sums[:cycle_rem + 1] max_series_sums :cycle_rem + 1 cycle_rem + 1 cycle_rem 1 max_score = max(max_score, score) max_score max(max_score, score) max max_score score print(max_score) print max_score max_cycle_num, cycle_rem = divmod(k, cycle_len) divmod(k, cycle_len) max_cycle_num cycled = [False] * n [False] * n cycled cycle = [i] [i] cycle score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1])) max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1])) score k = map(int, input().split()) map(int, input().split()) k cycles = [] [] cycles cycle_len = len(cycle) len(cycle) cycle_len max_score = max(max_score, score) max(max_score, score) max_score score = max(max_series_sums[1:]) max(max_series_sums[1:]) score max_score = -(1 << 64) -(1 << 64) max_score score = max(max_series_sums[1:k + 1]) max(max_series_sums[1:k + 1]) score cycle_rem = divmod(k, cycle_len) divmod(k, cycle_len) cycle_rem p_list = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) p_list n, k = map(int, input().split()) map(int, input().split()) n c_list = list(map(int, input().split())) list(map(int, input().split())) c_list max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)] [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)] max_series_sums accum = [0] [0] accum cycle_score = accum[cycle_len] accum[cycle_len] cycle_score
n, k = map(int, input().split())
p_list = list(map(lambda x: int(x) - 1, input().split()))
c_list = list(map(int, input().split()))

max_score = -(1 << 64)
cycles = []
cycled = [False] * n
for i in range(n):
    if cycled[i]:
        continue

    cycle = [i]
    cycled[i] = True
    while p_list[cycle[-1]] != cycle[0]:
        cycle.append(p_list[cycle[-1]])
        cycled[cycle[-1]] = True

    cycles.append(cycle)

for cycle in cycles:
    cycle_len = len(cycle)

    accum = [0]
    for i in range(cycle_len * 2):
        accum.append(accum[-1] + c_list[cycle[i % cycle_len]])

    cycle_score = accum[cycle_len]

    max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)]

    if cycle_len >= k:
        score = max(max_series_sums[1:k + 1])
    elif cycle_score <= 0:
        score = max(max_series_sums[1:])
    else:
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]))

    max_score = max(max_score, score)

print(max_score)
