from itertools import accumulate 
#acumulate [a[0], a[0]+a[1], a[0]+...]
 
n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = list(map(int, input().split()))
ans = -(10 ** 18)
 
for i in range(n):
    pos = i
    scores = []
    while True:
        pos = p[pos]
        scores.append(c[pos])
        if pos == i:
            break
    m = len(scores)
    s = list(accumulate(scores))
    for j in range(min(m, k)):
        x = (k - j - 1) // m
        ans = max(ans, s[j], s[j] + s[-1] * x)
 
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 int(x) - 1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) - 1 int(x) int x 1 p = [int(x) - 1 for x in input().split()] p [int(x) - 1 for x in 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 ans = -(10 ** 18) ans -(10 ** 18) 10 ** 18 10 18 for i in range(n):
    pos = i
    scores = []
    while True:
        pos = p[pos]
        scores.append(c[pos])
        if pos == i:
            break
    m = len(scores)
    s = list(accumulate(scores))
    for j in range(min(m, k)):
        x = (k - j - 1) // m
        ans = max(ans, s[j], s[j] + s[-1] * x)
  i range(n) range n pos = i pos i scores = [] scores [] while True:
        pos = p[pos]
        scores.append(c[pos])
        if pos == i:
            break
     True pos = p[pos] pos p[pos] p pos scores.append(c[pos]) scores.append scores append c[pos] c pos if pos == i:
            break
     pos == i pos i break m = len(scores) m len(scores) len scores s = list(accumulate(scores)) s list(accumulate(scores)) list accumulate(scores) accumulate scores for j in range(min(m, k)):
        x = (k - j - 1) // m
        ans = max(ans, s[j], s[j] + s[-1] * x)
  j range(min(m, k)) range min(m, k) min m k x = (k - j - 1) // m x (k - j - 1) // m k - j - 1 k - j k j 1 m ans = max(ans, s[j], s[j] + s[-1] * x) ans max(ans, s[j], s[j] + s[-1] * x) max ans s[j] s j s[j] + s[-1] * x s[j] s j s[-1] * x s[-1] s -1 x print(ans) print ans c = list(map(int, input().split())) list(map(int, input().split())) c pos = p[pos] p[pos] pos scores = [] [] scores m = len(scores) len(scores) m s = list(accumulate(scores)) list(accumulate(scores)) s k = map(int, input().split()) map(int, input().split()) k p = [int(x) - 1 for x in input().split()] [int(x) - 1 for x in input().split()] p n, k = map(int, input().split()) map(int, input().split()) n pos = i i pos x = (k - j - 1) // m (k - j - 1) // m x ans = -(10 ** 18) -(10 ** 18) ans ans = max(ans, s[j], s[j] + s[-1] * x) max(ans, s[j], s[j] + s[-1] * x) ans
from itertools import accumulate 
#acumulate [a[0], a[0]+a[1], a[0]+...]
 
n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = list(map(int, input().split()))
ans = -(10 ** 18)
 
for i in range(n):
    pos = i
    scores = []
    while True:
        pos = p[pos]
        scores.append(c[pos])
        if pos == i:
            break
    m = len(scores)
    s = list(accumulate(scores))
    for j in range(min(m, k)):
        x = (k - j - 1) // m
        ans = max(ans, s[j], s[j] + s[-1] * x)
 
print(ans)