n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

g = [v for v in range(n + 1)]
s = [0] * (n + 1)
r = [0] * (n + 1)
seen = set()
for i in range(1, n + 1):
    if i in seen:
        s[i] = s[g[i]]
        r[i] = r[g[i]]
        continue
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        g[ni] = i
        seen.add(ni)
        if ni == i:
            break
    s[i] = sm
    r[i] = cnt

ans = -(10 ** 9 + 1)
for i in range(1, n + 1):
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i])
        if ni == i or cnt == k:
            break
print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = [0] + list(map(int, input().split())) p [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c = [0] + list(map(int, input().split())) c [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split v for v in range(n + 1) for v in range(n + 1) v range(n + 1) range n + 1 n 1 for v in range(n + 1) v g = [v for v in range(n + 1)] g [v for v in range(n + 1)] s = [0] * (n + 1) s [0] * (n + 1) [0] 0 n + 1 n 1 r = [0] * (n + 1) r [0] * (n + 1) [0] 0 n + 1 n 1 seen = set() seen set() set for i in range(1, n + 1):
    if i in seen:
        s[i] = s[g[i]]
        r[i] = r[g[i]]
        continue
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        g[ni] = i
        seen.add(ni)
        if ni == i:
            break
    s[i] = sm
    r[i] = cnt i range(1, n + 1) range 1 n + 1 n 1 if i in seen:
        s[i] = s[g[i]]
        r[i] = r[g[i]]
        continue
     i in seen i seen s[i] = s[g[i]] s[i] s i s[g[i]] s g[i] g i r[i] = r[g[i]] r[i] r i r[g[i]] r g[i] g i continue ni = i ni i sm = 0 sm 0 cnt = 0 cnt 0 while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        g[ni] = i
        seen.add(ni)
        if ni == i:
            break
     True ni = p[ni] ni p[ni] p ni sm += c[ni] sm c[ni] c ni cnt += 1 cnt 1 g[ni] = i g[ni] g ni i seen.add(ni) seen.add seen add ni if ni == i:
            break
     ni == i ni i break s[i] = sm s[i] s i sm r[i] = cnt r[i] r i cnt ans = -(10 ** 9 + 1) ans -(10 ** 9 + 1) 10 ** 9 + 1 10 ** 9 10 9 1 for i in range(1, n + 1):
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i])
        if ni == i or cnt == k:
            break i range(1, n + 1) range 1 n + 1 n 1 ni = i ni i sm = 0 sm 0 cnt = 0 cnt 0 while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i])
        if ni == i or cnt == k:
            break True ni = p[ni] ni p[ni] p ni sm += c[ni] sm c[ni] c ni cnt += 1 cnt 1 ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i]) ans max(ans, sm, sm + (k - cnt) // r[i] * s[i]) max ans sm sm + (k - cnt) // r[i] * s[i] sm (k - cnt) // r[i] * s[i] (k - cnt) // r[i] k - cnt k cnt r[i] r i s[i] s i if ni == i or cnt == k:
            break ni == i or cnt == k ni == i ni i cnt == k cnt k break print(ans) print ans ni = i i ni g = [v for v in range(n + 1)] [v for v in range(n + 1)] g cnt += 1 1 cnt cnt = 0 0 cnt n, k = map(int, input().split()) map(int, input().split()) n ni = p[ni] p[ni] ni ni = i i ni r = [0] * (n + 1) [0] * (n + 1) r s = [0] * (n + 1) [0] * (n + 1) s p = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) p cnt = 0 0 cnt ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i]) max(ans, sm, sm + (k - cnt) // r[i] * s[i]) ans ni = p[ni] p[ni] ni sm = 0 0 sm k = map(int, input().split()) map(int, input().split()) k seen = set() set() seen sm = 0 0 sm ans = -(10 ** 9 + 1) -(10 ** 9 + 1) ans cnt += 1 1 cnt sm += c[ni] c[ni] sm c = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) c sm += c[ni] c[ni] sm
n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

g = [v for v in range(n + 1)]
s = [0] * (n + 1)
r = [0] * (n + 1)
seen = set()
for i in range(1, n + 1):
    if i in seen:
        s[i] = s[g[i]]
        r[i] = r[g[i]]
        continue
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        g[ni] = i
        seen.add(ni)
        if ni == i:
            break
    s[i] = sm
    r[i] = cnt

ans = -(10 ** 9 + 1)
for i in range(1, n + 1):
    ni = i
    sm = 0
    cnt = 0
    while True:
        ni = p[ni]
        sm += c[ni]
        cnt += 1
        ans = max(ans, sm, sm + (k - cnt) // r[i] * s[i])
        if ni == i or cnt == k:
            break
print(ans)
