n, k = (int(x) for x in input().split())
P = list(int(x) - 1 for x in input().split())
C = list(int(x) for x in input().split())

ans = -1e18
for si in range(n):
    S = [C[si]]
    x = P[si]
    while x != si:
        S.append(S[-1] + C[x])
        x = P[x]
    m = len(S)
    if k <= m:
        tmp = max(S[:k])
    else:
        loop = k // m
        r = k % m
        tmp = max(S[-1] * (loop - 1) + max(S), max(S))
        if r != 0:
            tmp = max(tmp, S[-1] * loop + max(S[:r]))
    ans = max(ans, tmp)
print(ans) int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x n, k = (int(x) for x in input().split()) n (int(x) for x in input().split()) k P = list(int(x) - 1 for x in input().split()) P list(int(x) - 1 for x in input().split()) list int(x) - 1 int(x) int x 1 C = list(int(x) for x in input().split()) C list(int(x) for x in input().split()) list int(x) int x ans = -1e18 ans -1e18 for si in range(n):
    S = [C[si]]
    x = P[si]
    while x != si:
        S.append(S[-1] + C[x])
        x = P[x]
    m = len(S)
    if k <= m:
        tmp = max(S[:k])
    else:
        loop = k // m
        r = k % m
        tmp = max(S[-1] * (loop - 1) + max(S), max(S))
        if r != 0:
            tmp = max(tmp, S[-1] * loop + max(S[:r]))
    ans = max(ans, tmp) si range(n) range n S = [C[si]] S [C[si]] C[si] C si x = P[si] x P[si] P si while x != si:
        S.append(S[-1] + C[x])
        x = P[x]
     x != si x si S.append(S[-1] + C[x]) S.append S append S[-1] + C[x] S[-1] S -1 C[x] C x x = P[x] x P[x] P x m = len(S) m len(S) len S if k <= m:
        tmp = max(S[:k])
    else:
        loop = k // m
        r = k % m
        tmp = max(S[-1] * (loop - 1) + max(S), max(S))
        if r != 0:
            tmp = max(tmp, S[-1] * loop + max(S[:r]))
     k <= m k m tmp = max(S[:k]) tmp max(S[:k]) max S[:k] S :k k loop = k // m loop k // m k m r = k % m r k % m k m tmp = max(S[-1] * (loop - 1) + max(S), max(S)) tmp max(S[-1] * (loop - 1) + max(S), max(S)) max S[-1] * (loop - 1) + max(S) S[-1] * (loop - 1) S[-1] S -1 loop - 1 loop 1 max(S) max S max(S) max S if r != 0:
            tmp = max(tmp, S[-1] * loop + max(S[:r]))
     r != 0 r 0 tmp = max(tmp, S[-1] * loop + max(S[:r])) tmp max(tmp, S[-1] * loop + max(S[:r])) max tmp S[-1] * loop + max(S[:r]) S[-1] * loop S[-1] S -1 loop max(S[:r]) max S[:r] S :r r ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans x = P[x] P[x] x m = len(S) len(S) m P = list(int(x) - 1 for x in input().split()) list(int(x) - 1 for x in input().split()) P S = [C[si]] [C[si]] S tmp = max(S[:k]) max(S[:k]) tmp x = P[si] P[si] x r = k % m k % m r ans = max(ans, tmp) max(ans, tmp) ans ans = -1e18 -1e18 ans tmp = max(S[-1] * (loop - 1) + max(S), max(S)) max(S[-1] * (loop - 1) + max(S), max(S)) tmp k = (int(x) for x in input().split()) (int(x) for x in input().split()) k tmp = max(tmp, S[-1] * loop + max(S[:r])) max(tmp, S[-1] * loop + max(S[:r])) tmp loop = k // m k // m loop n, k = (int(x) for x in input().split()) (int(x) for x in input().split()) n C = list(int(x) for x in input().split()) list(int(x) for x in input().split()) C
n, k = (int(x) for x in input().split())
P = list(int(x) - 1 for x in input().split())
C = list(int(x) for x in input().split())

ans = -1e18
for si in range(n):
    S = [C[si]]
    x = P[si]
    while x != si:
        S.append(S[-1] + C[x])
        x = P[x]
    m = len(S)
    if k <= m:
        tmp = max(S[:k])
    else:
        loop = k // m
        r = k % m
        tmp = max(S[-1] * (loop - 1) + max(S), max(S))
        if r != 0:
            tmp = max(tmp, S[-1] * loop + max(S[:r]))
    ans = max(ans, tmp)
print(ans)