N, K = [int(_) for _ in input().split()]
P = [int(_) - 1 for _ in input().split()]
C = [int(_) for _ in input().split()]


X = [False for _ in range(N)]

V = []
for i in range(N):
    if X[i]:
        continue
    t = i
    v = []
    while X[t] is False:
        X[t] = True
        v.append(C[t])
        t = P[t]
    V.append(tuple(v))

def f(v, K):
    if K == 0:
        return 0
    if max(v) < 0:
        return max(v)
    n = len(v)

    X = [0]
    for i in range(n):
        X.append(X[-1] + v[i])
    ans = -100000000000
    for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
    return(ans)

ans = -100000000000
for v in V:
    n = len(v)
    if K > n:
        s = sum(v)
        if s > 0:
            x = s * (K // n) + f(v * 2, K % n)
            y = s * (K // n - 1) + f(v * 2, n)
            a = max(x, y)
        else:
            a = f(v * 2, n)
    else:
        a = f(v * 2, K)
    ans = max(a, ans)

print(ans) int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ N, K = [int(_) for _ in input().split()] N [int(_) for _ in input().split()] K int(_) - 1 for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) - 1 int(_) int _ 1 P = [int(_) - 1 for _ in input().split()] P [int(_) - 1 for _ in input().split()] int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ C = [int(_) for _ in input().split()] C [int(_) for _ in input().split()] False for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) False X = [False for _ in range(N)] X [False for _ in range(N)] V = [] V [] for i in range(N):
    if X[i]:
        continue
    t = i
    v = []
    while X[t] is False:
        X[t] = True
        v.append(C[t])
        t = P[t]
    V.append(tuple(v)) i range(N) range N if X[i]:
        continue
     X[i] X i continue t = i t i v = [] v [] while X[t] is False:
        X[t] = True
        v.append(C[t])
        t = P[t]
     X[t] is False X[t] X t False X[t] = True X[t] X t True v.append(C[t]) v.append v append C[t] C t t = P[t] t P[t] P t V.append(tuple(v)) V.append V append tuple(v) tuple v def f(v, K):
    if K == 0:
        return 0
    if max(v) < 0:
        return max(v)
    n = len(v)

    X = [0]
    for i in range(n):
        X.append(X[-1] + v[i])
    ans = -100000000000
    for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
    return(ans) f if K == 0:
        return 0
     K == 0 K 0 return 0 0 if max(v) < 0:
        return max(v)
     max(v) < 0 max(v) max v 0 return max(v) max(v) max v n = len(v) n len(v) len v X = [0] X [0] 0 for i in range(n):
        X.append(X[-1] + v[i])
     i range(n) range n X.append(X[-1] + v[i]) X.append X append X[-1] + v[i] X[-1] X -1 v[i] v i ans = -100000000000 ans -100000000000 for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
     i range(n + 1) range n + 1 n 1 for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
     j range(i) range i if i - j > K:
                continue
             i - j > K i - j i j K continue ans = max(ans, X[i] - X[j]) ans max(ans, X[i] - X[j]) max ans X[i] - X[j] X[i] X i X[j] X j return(ans) ans v v K K ans = -100000000000 ans -100000000000 for v in V:
    n = len(v)
    if K > n:
        s = sum(v)
        if s > 0:
            x = s * (K // n) + f(v * 2, K % n)
            y = s * (K // n - 1) + f(v * 2, n)
            a = max(x, y)
        else:
            a = f(v * 2, n)
    else:
        a = f(v * 2, K)
    ans = max(a, ans) v V n = len(v) n len(v) len v if K > n:
        s = sum(v)
        if s > 0:
            x = s * (K // n) + f(v * 2, K % n)
            y = s * (K // n - 1) + f(v * 2, n)
            a = max(x, y)
        else:
            a = f(v * 2, n)
    else:
        a = f(v * 2, K)
     K > n K n s = sum(v) s sum(v) sum v if s > 0:
            x = s * (K // n) + f(v * 2, K % n)
            y = s * (K // n - 1) + f(v * 2, n)
            a = max(x, y)
        else:
            a = f(v * 2, n)
     s > 0 s 0 x = s * (K // n) + f(v * 2, K % n) x s * (K // n) + f(v * 2, K % n) s * (K // n) s K // n K n f(v * 2, K % n) f v * 2 v 2 K % n K n y = s * (K // n - 1) + f(v * 2, n) y s * (K // n - 1) + f(v * 2, n) s * (K // n - 1) s K // n - 1 K // n K n 1 f(v * 2, n) f v * 2 v 2 n a = max(x, y) a max(x, y) max x y a = f(v * 2, n) a f(v * 2, n) f v * 2 v 2 n a = f(v * 2, K) a f(v * 2, K) f v * 2 v 2 K ans = max(a, ans) ans max(a, ans) max a ans print(ans) print ans t = i i t n = len(v) len(v) n s = sum(v) sum(v) s a = f(v * 2, n) f(v * 2, n) a C = [int(_) for _ in input().split()] [int(_) for _ in input().split()] C V = [] [] V a = f(v * 2, K) f(v * 2, K) a N, K = [int(_) for _ in input().split()] [int(_) for _ in input().split()] N ans = max(a, ans) max(a, ans) ans K = [int(_) for _ in input().split()] [int(_) for _ in input().split()] K P = [int(_) - 1 for _ in input().split()] [int(_) - 1 for _ in input().split()] P t = P[t] P[t] t X = [False for _ in range(N)] [False for _ in range(N)] X ans = -100000000000 -100000000000 ans x = s * (K // n) + f(v * 2, K % n) s * (K // n) + f(v * 2, K % n) x y = s * (K // n - 1) + f(v * 2, n) s * (K // n - 1) + f(v * 2, n) y v = [] [] v def f(v, K):
    if K == 0:
        return 0
    if max(v) < 0:
        return max(v)
    n = len(v)

    X = [0]
    for i in range(n):
        X.append(X[-1] + v[i])
    ans = -100000000000
    for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
    return(ans) def f(v, K):
    if K == 0:
        return 0
    if max(v) < 0:
        return max(v)
    n = len(v)

    X = [0]
    for i in range(n):
        X.append(X[-1] + v[i])
    ans = -100000000000
    for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
    return(ans) f a = max(x, y) max(x, y) a
N, K = [int(_) for _ in input().split()]
P = [int(_) - 1 for _ in input().split()]
C = [int(_) for _ in input().split()]


X = [False for _ in range(N)]

V = []
for i in range(N):
    if X[i]:
        continue
    t = i
    v = []
    while X[t] is False:
        X[t] = True
        v.append(C[t])
        t = P[t]
    V.append(tuple(v))

def f(v, K):
    if K == 0:
        return 0
    if max(v) < 0:
        return max(v)
    n = len(v)

    X = [0]
    for i in range(n):
        X.append(X[-1] + v[i])
    ans = -100000000000
    for i in range(n + 1):
        for j in range(i):
            if i - j > K:
                continue
            ans = max(ans, X[i] - X[j])
    return(ans)

ans = -100000000000
for v in V:
    n = len(v)
    if K > n:
        s = sum(v)
        if s > 0:
            x = s * (K // n) + f(v * 2, K % n)
            y = s * (K // n - 1) + f(v * 2, n)
            a = max(x, y)
        else:
            a = f(v * 2, n)
    else:
        a = f(v * 2, K)
    ans = max(a, ans)

print(ans)
