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

point_loop_list = [0 for _ in range(N)]
point_set = set()
loop_list = []
for i in range(N):
    if i in point_set:
        continue
    loop = [i]
    j = P[i] - 1
    while j != i:
        loop.append(j)
        point_set.add(j)
        j = P[j] - 1
    loop += loop
    loop_list.append(loop)

# print(loop_list)

loop_sum_list = [0 for _ in range(N)]

for loop in loop_list:
    loop_len = len(loop) // 2
    s = sum([C[j] for j in loop[:loop_len]])

    for i, p in enumerate(loop[:loop_len]):
        point_loop_list[p] = loop[i:i+loop_len]
        loop_sum_list[p] = s


ans = min(C)

for l, s in zip(point_loop_list, loop_sum_list):
    p = 0
    loop_len = len(l)
    if s > 0:
        num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1
        p += s * num_loop
        m = 0
        cs = 0
        for i in range(K - loop_len * num_loop):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    else:
        m = C[l[0]]
        cs = m
        for i in range(1, loop_len):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    ans = max(p, ans)

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 0 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) 0 point_loop_list = [0 for _ in range(N)] point_loop_list [0 for _ in range(N)] point_set = set() point_set set() set loop_list = [] loop_list [] for i in range(N):
    if i in point_set:
        continue
    loop = [i]
    j = P[i] - 1
    while j != i:
        loop.append(j)
        point_set.add(j)
        j = P[j] - 1
    loop += loop
    loop_list.append(loop)

# print(loop_list) i range(N) range N if i in point_set:
        continue
     i in point_set i point_set continue loop = [i] loop [i] i j = P[i] - 1 j P[i] - 1 P[i] P i 1 while j != i:
        loop.append(j)
        point_set.add(j)
        j = P[j] - 1
     j != i j i loop.append(j) loop.append loop append j point_set.add(j) point_set.add point_set add j j = P[j] - 1 j P[j] - 1 P[j] P j 1 loop += loop loop loop loop_list.append(loop) loop_list.append loop_list append loop 0 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) 0 loop_sum_list = [0 for _ in range(N)] loop_sum_list [0 for _ in range(N)] for loop in loop_list:
    loop_len = len(loop) // 2
    s = sum([C[j] for j in loop[:loop_len]])

    for i, p in enumerate(loop[:loop_len]):
        point_loop_list[p] = loop[i:i+loop_len]
        loop_sum_list[p] = s loop loop_list loop_len = len(loop) // 2 loop_len len(loop) // 2 len(loop) len loop 2 C[j] for j in loop[:loop_len] for j in loop[:loop_len] j loop[:loop_len] loop :loop_len loop_len for j in loop[:loop_len] C[j] C j s = sum([C[j] for j in loop[:loop_len]]) s sum([C[j] for j in loop[:loop_len]]) sum [C[j] for j in loop[:loop_len]] for i, p in enumerate(loop[:loop_len]):
        point_loop_list[p] = loop[i:i+loop_len]
        loop_sum_list[p] = s i p enumerate(loop[:loop_len]) enumerate loop[:loop_len] loop :loop_len loop_len point_loop_list[p] = loop[i:i+loop_len] point_loop_list[p] point_loop_list p loop[i:i+loop_len] loop i:i+loop_len i i+loop_len i loop_len loop_sum_list[p] = s loop_sum_list[p] loop_sum_list p s ans = min(C) ans min(C) min C for l, s in zip(point_loop_list, loop_sum_list):
    p = 0
    loop_len = len(l)
    if s > 0:
        num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1
        p += s * num_loop
        m = 0
        cs = 0
        for i in range(K - loop_len * num_loop):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    else:
        m = C[l[0]]
        cs = m
        for i in range(1, loop_len):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    ans = max(p, ans) l s zip(point_loop_list, loop_sum_list) zip point_loop_list loop_sum_list p = 0 p 0 loop_len = len(l) loop_len len(l) len l if s > 0:
        num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1
        p += s * num_loop
        m = 0
        cs = 0
        for i in range(K - loop_len * num_loop):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    else:
        m = C[l[0]]
        cs = m
        for i in range(1, loop_len):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
     s > 0 s 0 num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1 num_loop K // loop_len if K % loop_len != 0 else K // loop_len - 1 K % loop_len != 0 K % loop_len K loop_len 0 K // loop_len K loop_len K // loop_len - 1 K // loop_len K loop_len 1 p += s * num_loop p s * num_loop s num_loop m = 0 m 0 cs = 0 cs 0 for i in range(K - loop_len * num_loop):
            cs += C[l[i]]
            m = max(m, cs)
         i range(K - loop_len * num_loop) range K - loop_len * num_loop K loop_len * num_loop loop_len num_loop cs += C[l[i]] cs C[l[i]] C l[i] l i m = max(m, cs) m max(m, cs) max m cs p += m p m m = C[l[0]] m C[l[0]] C l[0] l 0 cs = m cs m for i in range(1, loop_len):
            cs += C[l[i]]
            m = max(m, cs)
         i range(1, loop_len) range 1 loop_len cs += C[l[i]] cs C[l[i]] C l[i] l i m = max(m, cs) m max(m, cs) max m cs p += m p m ans = max(p, ans) ans max(p, ans) max p ans print(ans) print ans loop = [i] [i] loop p += s * num_loop s * num_loop p j = P[j] - 1 P[j] - 1 j cs = m m cs s = sum([C[j] for j in loop[:loop_len]]) sum([C[j] for j in loop[:loop_len]]) s j = P[i] - 1 P[i] - 1 j p += m m p loop_sum_list = [0 for _ in range(N)] [0 for _ in range(N)] loop_sum_list num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1 K // loop_len if K % loop_len != 0 else K // loop_len - 1 num_loop C = list(map(int, input().split())) list(map(int, input().split())) C m = 0 0 m ans = max(p, ans) max(p, ans) ans loop_len = len(l) len(l) loop_len loop_list = [] [] loop_list loop += loop loop loop m = C[l[0]] C[l[0]] m cs += C[l[i]] C[l[i]] cs m = max(m, cs) max(m, cs) m ans = min(C) min(C) ans m = max(m, cs) max(m, cs) m point_loop_list = [0 for _ in range(N)] [0 for _ in range(N)] point_loop_list loop_len = len(loop) // 2 len(loop) // 2 loop_len P = list(map(int, input().split())) list(map(int, input().split())) P point_set = set() set() point_set p += m m p N, K = map(int, input().split()) map(int, input().split()) N cs = 0 0 cs K = map(int, input().split()) map(int, input().split()) K p = 0 0 p cs += C[l[i]] C[l[i]] cs
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

point_loop_list = [0 for _ in range(N)]
point_set = set()
loop_list = []
for i in range(N):
    if i in point_set:
        continue
    loop = [i]
    j = P[i] - 1
    while j != i:
        loop.append(j)
        point_set.add(j)
        j = P[j] - 1
    loop += loop
    loop_list.append(loop)

# print(loop_list)

loop_sum_list = [0 for _ in range(N)]

for loop in loop_list:
    loop_len = len(loop) // 2
    s = sum([C[j] for j in loop[:loop_len]])

    for i, p in enumerate(loop[:loop_len]):
        point_loop_list[p] = loop[i:i+loop_len]
        loop_sum_list[p] = s


ans = min(C)

for l, s in zip(point_loop_list, loop_sum_list):
    p = 0
    loop_len = len(l)
    if s > 0:
        num_loop = K // loop_len if K % loop_len != 0 else K // loop_len - 1
        p += s * num_loop
        m = 0
        cs = 0
        for i in range(K - loop_len * num_loop):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    else:
        m = C[l[0]]
        cs = m
        for i in range(1, loop_len):
            cs += C[l[i]]
            m = max(m, cs)
        p += m
    ans = max(p, ans)

print(ans)

