from itertools import accumulate

n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

check = [False] * n
loops = []

for i in range(n):
    if check[i]:
        continue
    check[i] = True
    next = P[i]
    tmp = [i]
    while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
    loops.append(tmp)

ans = int(-1e9)

for loop in loops:

    num = len(loop)
    div = k // num
    r = k % num
    if r == 0:
        r = num
        div -= 1
    L = [C[p] for p in loop]
    ans_tmp = int(-1e9)
    sumL = sum(L)
    by_loop = max(sumL, 0) * div
    if k > num and sumL <= 0:
        r = num
    L += L[:r]
    ACC = [0] + list(accumulate(L))

    for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

    ans = max(ans, ans_tmp)
print(ans) from itertools import accumulate 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 p - 1 for p in P for p in P p P for p in P p - 1 p 1 P = [p - 1 for p in P] P [p - 1 for p in P] check = [False] * n check [False] * n [False] False n loops = [] loops [] for i in range(n):
    if check[i]:
        continue
    check[i] = True
    next = P[i]
    tmp = [i]
    while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
    loops.append(tmp) i range(n) range n if check[i]:
        continue
     check[i] check i continue check[i] = True check[i] check i True next = P[i] next P[i] P i tmp = [i] tmp [i] i while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
     next != i next i check[next] = True check[next] check next True tmp.append(next) tmp.append tmp append next next = P[next] next P[next] P next loops.append(tmp) loops.append loops append tmp ans = int(-1e9) ans int(-1e9) int -1e9 for loop in loops:

    num = len(loop)
    div = k // num
    r = k % num
    if r == 0:
        r = num
        div -= 1
    L = [C[p] for p in loop]
    ans_tmp = int(-1e9)
    sumL = sum(L)
    by_loop = max(sumL, 0) * div
    if k > num and sumL <= 0:
        r = num
    L += L[:r]
    ACC = [0] + list(accumulate(L))

    for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

    ans = max(ans, ans_tmp) loop loops num = len(loop) num len(loop) len loop div = k // num div k // num k num r = k % num r k % num k num if r == 0:
        r = num
        div -= 1
     r == 0 r 0 r = num r num div -= 1 div 1 C[p] for p in loop for p in loop p loop for p in loop C[p] C p L = [C[p] for p in loop] L [C[p] for p in loop] ans_tmp = int(-1e9) ans_tmp int(-1e9) int -1e9 sumL = sum(L) sumL sum(L) sum L by_loop = max(sumL, 0) * div by_loop max(sumL, 0) * div max(sumL, 0) max sumL 0 div if k > num and sumL <= 0:
        r = num
     k > num and sumL <= 0 k > num k num sumL <= 0 sumL 0 r = num r num L += L[:r] L L[:r] L :r r ACC = [0] + list(accumulate(L)) ACC [0] + list(accumulate(L)) [0] 0 list(accumulate(L)) list accumulate(L) accumulate L for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

     i range(num) range num for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

     j range(i + 1, i + r + 1) range i + 1 i 1 i + r + 1 i + r i r 1 ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop) ans_tmp max(ans_tmp, ACC[j] - ACC[i] + by_loop) max ans_tmp ACC[j] - ACC[i] + by_loop ACC[j] - ACC[i] ACC[j] ACC j ACC[i] ACC i by_loop ans = max(ans, ans_tmp) ans max(ans, ans_tmp) max ans ans_tmp print(ans) print ans next = P[i] P[i] next div = k // num k // num div tmp = [i] [i] tmp ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop) max(ans_tmp, ACC[j] - ACC[i] + by_loop) ans_tmp loops = [] [] loops r = k % num k % num r check = [False] * n [False] * n check next = P[next] P[next] next ans = max(ans, ans_tmp) max(ans, ans_tmp) ans P = list(map(int, input().split())) list(map(int, input().split())) P C = list(map(int, input().split())) list(map(int, input().split())) C r = num num r ans_tmp = int(-1e9) int(-1e9) ans_tmp P = [p - 1 for p in P] [p - 1 for p in P] P sumL = sum(L) sum(L) sumL k = map(int, input().split()) map(int, input().split()) k ans = int(-1e9) int(-1e9) ans r = num num r by_loop = max(sumL, 0) * div max(sumL, 0) * div by_loop L += L[:r] L[:r] L n, k = map(int, input().split()) map(int, input().split()) n div -= 1 1 div ACC = [0] + list(accumulate(L)) [0] + list(accumulate(L)) ACC num = len(loop) len(loop) num L = [C[p] for p in loop] [C[p] for p in loop] L
from itertools import accumulate

n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

check = [False] * n
loops = []

for i in range(n):
    if check[i]:
        continue
    check[i] = True
    next = P[i]
    tmp = [i]
    while next != i:
        check[next] = True
        tmp.append(next)
        next = P[next]
    loops.append(tmp)

ans = int(-1e9)

for loop in loops:

    num = len(loop)
    div = k // num
    r = k % num
    if r == 0:
        r = num
        div -= 1
    L = [C[p] for p in loop]
    ans_tmp = int(-1e9)
    sumL = sum(L)
    by_loop = max(sumL, 0) * div
    if k > num and sumL <= 0:
        r = num
    L += L[:r]
    ACC = [0] + list(accumulate(L))

    for i in range(num):
        for j in range(i + 1, i + r + 1):
            ans_tmp = max(ans_tmp, ACC[j] - ACC[i] + by_loop)

    ans = max(ans, ans_tmp)
print(ans)
