from itertools import accumulate

def max_from_acc(A, b):
    r = -10**9-1
    acc = [0] + list(accumulate(A + A[:b]))
    for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
    return r

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]

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

# print(loops)

ans = -10**9-1
for loop in loops:
    t =len(loop)
    div, mod = divmod(k, t)
    if mod == 0:
        mod = t
        div -= 1
    C_loop = [C[p] for p in loop]
    loop_score = sum(C_loop)

    if t >=k:
        ans = max(ans, max_from_acc(C_loop, k))
    else:
        if loop_score > 0:
            ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div)
        else:
            ans = max(ans, max_from_acc(C_loop, t))

print(ans) from itertools import accumulate def max_from_acc(A, b):
    r = -10**9-1
    acc = [0] + list(accumulate(A + A[:b]))
    for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
    return r max_from_acc r = -10**9-1 r -10**9-1 -10**9 -10 9 1 acc = [0] + list(accumulate(A + A[:b])) acc [0] + list(accumulate(A + A[:b])) [0] 0 list(accumulate(A + A[:b])) list accumulate(A + A[:b]) accumulate A + A[:b] A A[:b] A :b b for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
     i range(len(A)) range len(A) len A for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
     j range(1, b+1) range 1 b+1 b 1 r = max(r, acc[i+j]-acc[i]) r max(r, acc[i+j]-acc[i]) max r acc[i+j]-acc[i] acc[i+j] acc i+j i j acc[i] acc i return r r A A b b 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] checked = [False] * n checked [False] * n [False] False n loops = [] loops [] for i in range(n):
    if checked[i]:
        continue
    checked[i] = True
    tmp = [i]
    next = P[i]
    while next != i:
        tmp.append(next)
        checked[next] = True
        next = P[next]
    loops.append(tmp)

# print(loops) i range(n) range n if checked[i]:
        continue
     checked[i] checked i continue checked[i] = True checked[i] checked i True tmp = [i] tmp [i] i next = P[i] next P[i] P i while next != i:
        tmp.append(next)
        checked[next] = True
        next = P[next]
     next != i next i tmp.append(next) tmp.append tmp append next checked[next] = True checked[next] checked next True next = P[next] next P[next] P next loops.append(tmp) loops.append loops append tmp ans = -10**9-1 ans -10**9-1 -10**9 -10 9 1 for loop in loops:
    t =len(loop)
    div, mod = divmod(k, t)
    if mod == 0:
        mod = t
        div -= 1
    C_loop = [C[p] for p in loop]
    loop_score = sum(C_loop)

    if t >=k:
        ans = max(ans, max_from_acc(C_loop, k))
    else:
        if loop_score > 0:
            ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div)
        else:
            ans = max(ans, max_from_acc(C_loop, t)) loop loops t =len(loop) t len(loop) len loop div, mod = divmod(k, t) div divmod(k, t) divmod k t mod if mod == 0:
        mod = t
        div -= 1
     mod == 0 mod 0 mod = t mod t div -= 1 div 1 C[p] for p in loop for p in loop p loop for p in loop C[p] C p C_loop = [C[p] for p in loop] C_loop [C[p] for p in loop] loop_score = sum(C_loop) loop_score sum(C_loop) sum C_loop if t >=k:
        ans = max(ans, max_from_acc(C_loop, k))
    else:
        if loop_score > 0:
            ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div)
        else:
            ans = max(ans, max_from_acc(C_loop, t)) t >=k t k ans = max(ans, max_from_acc(C_loop, k)) ans max(ans, max_from_acc(C_loop, k)) max ans max_from_acc(C_loop, k) max_from_acc C_loop k if loop_score > 0:
            ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div)
        else:
            ans = max(ans, max_from_acc(C_loop, t)) loop_score > 0 loop_score 0 ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div) ans max(ans, max_from_acc(C_loop, mod)+loop_score*div) max ans max_from_acc(C_loop, mod)+loop_score*div max_from_acc(C_loop, mod) max_from_acc C_loop mod loop_score*div loop_score div ans = max(ans, max_from_acc(C_loop, t)) ans max(ans, max_from_acc(C_loop, t)) max ans max_from_acc(C_loop, t) max_from_acc C_loop t print(ans) print ans next = P[i] P[i] next def max_from_acc(A, b):
    r = -10**9-1
    acc = [0] + list(accumulate(A + A[:b]))
    for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
    return r def max_from_acc(A, b):
    r = -10**9-1
    acc = [0] + list(accumulate(A + A[:b]))
    for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
    return r max_from_acc k = map(int, input().split()) map(int, input().split()) k ans = max(ans, max_from_acc(C_loop, k)) max(ans, max_from_acc(C_loop, k)) ans mod = divmod(k, t) divmod(k, t) mod tmp = [i] [i] tmp t =len(loop) len(loop) t div, mod = divmod(k, t) divmod(k, t) div P = list(map(int, input().split())) list(map(int, input().split())) P C = list(map(int, input().split())) list(map(int, input().split())) C next = P[next] P[next] next ans = max(ans, max_from_acc(C_loop, t)) max(ans, max_from_acc(C_loop, t)) ans loop_score = sum(C_loop) sum(C_loop) loop_score ans = -10**9-1 -10**9-1 ans checked = [False] * n [False] * n checked loops = [] [] loops ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div) max(ans, max_from_acc(C_loop, mod)+loop_score*div) ans mod = t t mod C_loop = [C[p] for p in loop] [C[p] for p in loop] C_loop P = [p - 1 for p in P] [p - 1 for p in P] P n, k = map(int, input().split()) map(int, input().split()) n div -= 1 1 div
from itertools import accumulate

def max_from_acc(A, b):
    r = -10**9-1
    acc = [0] + list(accumulate(A + A[:b]))
    for i in range(len(A)):
        for j in range(1, b+1):
            r = max(r, acc[i+j]-acc[i])
    return r

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]

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

# print(loops)

ans = -10**9-1
for loop in loops:
    t =len(loop)
    div, mod = divmod(k, t)
    if mod == 0:
        mod = t
        div -= 1
    C_loop = [C[p] for p in loop]
    loop_score = sum(C_loop)

    if t >=k:
        ans = max(ans, max_from_acc(C_loop, k))
    else:
        if loop_score > 0:
            ans = max(ans, max_from_acc(C_loop, mod)+loop_score*div)
        else:
            ans = max(ans, max_from_acc(C_loop, t))

print(ans)