N, K = map(int, input().split())
*P, = map(int, input().split())
*C, = map(int, input().split())

def get(pos):
    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:
        return 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])
        return max(v1, v2) if a else v2

ans = max([get(i) for i in range(N)])
print(ans) 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 def get(pos):
    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:
        return 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])
        return max(v1, v2) if a else v2 get 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:
        return 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])
        return max(v1, v2) if a else v2 cost[loop_size] <= 0 cost[loop_size] cost loop_size 0 return max(cost[1:K+1]) 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 return max(v1, v2) if a else v2 max(v1, v2) if a else v2 a max(v1, v2) max v1 v2 v2 pos pos get(i) for i in range(N) for i in range(N) i range(N) range N for i in range(N) get(i) get i ans = max([get(i) for i in range(N)]) ans max([get(i) for i in range(N)]) max [get(i) for i in range(N)] print(ans) print ans N, K = map(int, input().split()) map(int, input().split()) N def get(pos):
    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:
        return 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])
        return max(v1, v2) if a else v2 def get(pos):
    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:
        return 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])
        return max(v1, v2) if a else v2 get K = map(int, input().split()) map(int, input().split()) K ans = max([get(i) for i in range(N)]) max([get(i) for i in range(N)]) ans
N, K = map(int, input().split())
*P, = map(int, input().split())
*C, = map(int, input().split())

def get(pos):
    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:
        return 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])
        return max(v1, v2) if a else v2

ans = max([get(i) for i in range(N)])
print(ans)
