N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
G = [-1 for i in range(N+1)]

for i, v in enumerate(P):
    G[i+1] = (v, C[v-1])

ans = -float("inf")

for i in range(1, N+1):
    loop = []
    S = i
    visited = [False for i in range(N+1)]
    while visited[S] == False:
        loop.append(G[S][-1])
        visited[S] = True
        S = G[S][0]

    circle_sum = sum(loop)
    this_case_ans = -float("inf")

    if circle_sum < 0:
        pass_sum = 0
        for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
        ans = max(ans, this_case_ans)
        continue

    else:
        L = len(loop)
        for i in range(1, L):
            loop[i] += loop[i-1]
        loop.insert(0, -float("inf"))
        for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
        loop[0] = 0
        if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
        ans = max(ans, this_case_ans)

print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split -1 for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) -1 G = [-1 for i in range(N+1)] G [-1 for i in range(N+1)] for i, v in enumerate(P):
    G[i+1] = (v, C[v-1]) i v enumerate(P) enumerate P G[i+1] = (v, C[v-1]) G[i+1] G i+1 i 1 (v, C[v-1]) v C[v-1] C v-1 v 1 ans = -float("inf") ans -float("inf") float("inf") float "inf" for i in range(1, N+1):
    loop = []
    S = i
    visited = [False for i in range(N+1)]
    while visited[S] == False:
        loop.append(G[S][-1])
        visited[S] = True
        S = G[S][0]

    circle_sum = sum(loop)
    this_case_ans = -float("inf")

    if circle_sum < 0:
        pass_sum = 0
        for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
        ans = max(ans, this_case_ans)
        continue

    else:
        L = len(loop)
        for i in range(1, L):
            loop[i] += loop[i-1]
        loop.insert(0, -float("inf"))
        for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
        loop[0] = 0
        if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
        ans = max(ans, this_case_ans) i range(1, N+1) range 1 N+1 N 1 loop = [] loop [] S = i S i False for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) False visited = [False for i in range(N+1)] visited [False for i in range(N+1)] while visited[S] == False:
        loop.append(G[S][-1])
        visited[S] = True
        S = G[S][0]

     visited[S] == False visited[S] visited S False loop.append(G[S][-1]) loop.append loop append G[S][-1] [S] G S -1 visited[S] = True visited[S] visited S True S = G[S][0] S G[S][0] [S] G S 0 circle_sum = sum(loop) circle_sum sum(loop) sum loop this_case_ans = -float("inf") this_case_ans -float("inf") float("inf") float "inf" if circle_sum < 0:
        pass_sum = 0
        for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
        ans = max(ans, this_case_ans)
        continue

    else:
        L = len(loop)
        for i in range(1, L):
            loop[i] += loop[i-1]
        loop.insert(0, -float("inf"))
        for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
        loop[0] = 0
        if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
        ans = max(ans, this_case_ans) circle_sum < 0 circle_sum 0 pass_sum = 0 pass_sum 0 for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
         i range(len(loop)) range len(loop) len loop pass_sum += loop[i] pass_sum loop[i] loop i this_case_ans = max(this_case_ans, pass_sum) this_case_ans max(this_case_ans, pass_sum) max this_case_ans pass_sum ans = max(ans, this_case_ans) ans max(ans, this_case_ans) max ans this_case_ans continue L = len(loop) L len(loop) len loop for i in range(1, L):
            loop[i] += loop[i-1]
         i range(1, L) range 1 L loop[i] += loop[i-1] loop[i] loop i loop[i-1] loop i-1 i 1 loop.insert(0, -float("inf")) loop.insert loop insert 0 -float("inf") float("inf") float "inf" for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
         i range(1, L+1) range 1 L+1 L 1 loop[i] = max(loop[i], loop[i-1]) loop[i] loop i max(loop[i], loop[i-1]) max loop[i] loop i loop[i-1] loop i-1 i 1 loop[0] = 0 loop[0] loop 0 0 if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
         K//L > 0 K//L K L 0 this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1]) this_case_ans max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1]) max K//L * circle_sum +
                                loop[K % L] K//L * circle_sum K//L K L circle_sum loop[K % L] loop K % L K L (K//L-1)*circle_sum + loop[-1] (K//L-1)*circle_sum K//L-1 K//L K L 1 circle_sum loop[-1] loop -1 for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
         i range(1, K+1) range 1 K+1 K 1 this_case_ans = max(this_case_ans, loop[i]) this_case_ans max(this_case_ans, loop[i]) max this_case_ans loop[i] loop i ans = max(ans, this_case_ans) ans max(ans, this_case_ans) max ans this_case_ans print(ans) print ans ans = -float("inf") -float("inf") ans ans = max(ans, this_case_ans) max(ans, this_case_ans) ans this_case_ans = max(this_case_ans, loop[i]) max(this_case_ans, loop[i]) this_case_ans K = map(int, input().split()) map(int, input().split()) K pass_sum += loop[i] loop[i] pass_sum this_case_ans = max(this_case_ans, pass_sum) max(this_case_ans, pass_sum) this_case_ans S = G[S][0] G[S][0] S visited = [False for i in range(N+1)] [False for i in range(N+1)] visited L = len(loop) len(loop) L loop = [] [] loop this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1]) max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1]) this_case_ans ans = max(ans, this_case_ans) max(ans, this_case_ans) ans pass_sum = 0 0 pass_sum G = [-1 for i in range(N+1)] [-1 for i in range(N+1)] G C = list(map(int, input().split())) list(map(int, input().split())) C P = list(map(int, input().split())) list(map(int, input().split())) P this_case_ans = -float("inf") -float("inf") this_case_ans S = i i S N, K = map(int, input().split()) map(int, input().split()) N circle_sum = sum(loop) sum(loop) circle_sum
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
G = [-1 for i in range(N+1)]

for i, v in enumerate(P):
    G[i+1] = (v, C[v-1])

ans = -float("inf")

for i in range(1, N+1):
    loop = []
    S = i
    visited = [False for i in range(N+1)]
    while visited[S] == False:
        loop.append(G[S][-1])
        visited[S] = True
        S = G[S][0]

    circle_sum = sum(loop)
    this_case_ans = -float("inf")

    if circle_sum < 0:
        pass_sum = 0
        for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
        ans = max(ans, this_case_ans)
        continue

    else:
        L = len(loop)
        for i in range(1, L):
            loop[i] += loop[i-1]
        loop.insert(0, -float("inf"))
        for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
        loop[0] = 0
        if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            for i in range(1, K+1):
                this_case_ans = max(this_case_ans, loop[i])
        ans = max(ans, this_case_ans)

print(ans)