def solve():
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    ans = -float("inf")

    for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

    print(ans)

solve() def solve():
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    ans = -float("inf")

    for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

    print(ans) solve N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K *P, = map(int, input().split()) *P P map(int, input().split()) map int input().split() ().split () input split *C, = map(int, input().split()) *C C map(int, input().split()) map int input().split() ().split () input split ans = -float("inf") ans -float("inf") float("inf") float "inf" for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

     pos range(N) range N used = [-1] * N used [-1] * N [-1] -1 N cost = [0] * (N+1) cost [0] * (N+1) [0] 0 N+1 N 1 for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
         k range(N+1) range N+1 N 1 if k:
                cost[k] = cost[k-1] + C[pos]
             k cost[k] = cost[k-1] + C[pos] cost[k] cost k cost[k-1] + C[pos] cost[k-1] cost k-1 k 1 C[pos] C pos if used[pos] >= 0:
                loop_size = k-used[pos]
                break
             used[pos] >= 0 used[pos] used pos 0 loop_size = k-used[pos] loop_size k-used[pos] k used[pos] used pos break used[pos] = k used[pos] used pos k pos = P[pos] - 1 pos P[pos] - 1 P[pos] P pos 1 if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

     cost[loop_size] <= 0 cost[loop_size] cost loop_size 0 ans = max(ans, max(cost[1:K+1])) ans max(ans, max(cost[1:K+1])) max ans max(cost[1:K+1]) max cost[1:K+1] cost 1:K+1 1 K+1 K 1 a, b = divmod(K, loop_size) a divmod(K, loop_size) divmod K loop_size b v1 = cost[loop_size] * (a-1) + max(cost[:K+1]) v1 cost[loop_size] * (a-1) + max(cost[:K+1]) cost[loop_size] * (a-1) cost[loop_size] cost loop_size a-1 a 1 max(cost[:K+1]) max cost[:K+1] cost :K+1 K+1 K 1 v2 = cost[loop_size] * a + max(cost[:b+1]) v2 cost[loop_size] * a + max(cost[:b+1]) cost[loop_size] * a cost[loop_size] cost loop_size a max(cost[:b+1]) max cost[:b+1] cost :b+1 b+1 b 1 ans = max(ans, max(v1, v2) if a else v2) ans max(ans, max(v1, v2) if a else v2) max ans max(v1, v2) if a else v2 a max(v1, v2) max v1 v2 v2 print(ans) print ans solve() solve def solve():
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    ans = -float("inf")

    for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

    print(ans) def solve():
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    ans = -float("inf")

    for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

    print(ans) solve
def solve():
    N, K = map(int, input().split())
    *P, = map(int, input().split())
    *C, = map(int, input().split())
    ans = -float("inf")

    for pos in range(N):
        used = [-1] * N
        cost = [0] * (N+1)

        for k in range(N+1):
            if k:
                cost[k] = cost[k-1] + C[pos]
            if used[pos] >= 0:
                loop_size = k-used[pos]
                break
            used[pos] = k
            pos = P[pos] - 1
        if cost[loop_size] <= 0:
            ans = max(ans, max(cost[1:K+1]))
        else:
            a, b = divmod(K, loop_size)
            v1 = cost[loop_size] * (a-1) + max(cost[:K+1])
            v2 = cost[loop_size] * a + max(cost[:b+1])
            ans = max(ans, max(v1, v2) if a else v2)

    print(ans)

solve()
