N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

ans = -10**9-1
for i in range(N):
    seen = [-1]*N
    cumsum = [0]*(N+1)
    now = i
    j = 0
    while seen[now] == -1:
        seen[now] = 1
        now = P[now]
        cumsum[j+1] = cumsum[j] + C[now]
        j += 1
    loop_size = j
    if K <= loop_size:
        ans = max([ans] + cumsum[1:K+1])
    else:
        if cumsum[loop_size] > 0:
            loop_n = K//loop_size - 1
            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]
            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1:loop_size+1])
print(ans) N, K = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x: int(x) - 1, input().split())) P list(map(lambda x: int(x) - 1, input().split())) list map(lambda x: int(x) - 1, input().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x 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 ans = -10**9-1 ans -10**9-1 -10**9 -10 9 1 for i in range(N):
    seen = [-1]*N
    cumsum = [0]*(N+1)
    now = i
    j = 0
    while seen[now] == -1:
        seen[now] = 1
        now = P[now]
        cumsum[j+1] = cumsum[j] + C[now]
        j += 1
    loop_size = j
    if K <= loop_size:
        ans = max([ans] + cumsum[1:K+1])
    else:
        if cumsum[loop_size] > 0:
            loop_n = K//loop_size - 1
            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]
            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1:loop_size+1]) i range(N) range N seen = [-1]*N seen [-1]*N [-1] -1 N cumsum = [0]*(N+1) cumsum [0]*(N+1) [0] 0 N+1 N 1 now = i now i j = 0 j 0 while seen[now] == -1:
        seen[now] = 1
        now = P[now]
        cumsum[j+1] = cumsum[j] + C[now]
        j += 1
     seen[now] == -1 seen[now] seen now -1 seen[now] = 1 seen[now] seen now 1 now = P[now] now P[now] P now cumsum[j+1] = cumsum[j] + C[now] cumsum[j+1] cumsum j+1 j 1 cumsum[j] + C[now] cumsum[j] cumsum j C[now] C now j += 1 j 1 loop_size = j loop_size j if K <= loop_size:
        ans = max([ans] + cumsum[1:K+1])
    else:
        if cumsum[loop_size] > 0:
            loop_n = K//loop_size - 1
            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]
            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1:loop_size+1]) K <= loop_size K loop_size ans = max([ans] + cumsum[1:K+1]) ans max([ans] + cumsum[1:K+1]) max [ans] + cumsum[1:K+1] [ans] ans cumsum[1:K+1] cumsum 1:K+1 1 K+1 K 1 if cumsum[loop_size] > 0:
            loop_n = K//loop_size - 1
            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]
            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1:loop_size+1]) cumsum[loop_size] > 0 cumsum[loop_size] cumsum loop_size 0 loop_n = K//loop_size - 1 loop_n K//loop_size - 1 K//loop_size K loop_size 1 x + cumsum[loop_size] for x in cumsum[1:loop_size+1] for x in cumsum[1:loop_size+1] x cumsum[1:loop_size+1] cumsum 1:loop_size+1 1 loop_size+1 loop_size 1 for x in cumsum[1:loop_size+1] x + cumsum[loop_size] x cumsum[loop_size] cumsum loop_size cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]] cumsum2 cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]] cumsum[0:loop_size+1] cumsum 0:loop_size+1 0 loop_size+1 loop_size 1 [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]] score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1]) score cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1]) cumsum[loop_size]*loop_n cumsum[loop_size] cumsum loop_size loop_n max(cumsum2[0:loop_size+K%loop_size+1]) max cumsum2[0:loop_size+K%loop_size+1] cumsum2 0:loop_size+K%loop_size+1 0 loop_size+K%loop_size+1 loop_size+K%loop_size loop_size K%loop_size K loop_size 1 ans = max(ans, score) ans max(ans, score) max ans score ans = max([ans] + cumsum[1:loop_size+1]) ans max([ans] + cumsum[1:loop_size+1]) max [ans] + cumsum[1:loop_size+1] [ans] ans cumsum[1:loop_size+1] cumsum 1:loop_size+1 1 loop_size+1 loop_size 1 print(ans) print ans seen = [-1]*N [-1]*N seen cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]] cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]] cumsum2 score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1]) cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1]) score C = list(map(int, input().split())) list(map(int, input().split())) C now = P[now] P[now] now loop_n = K//loop_size - 1 K//loop_size - 1 loop_n ans = -10**9-1 -10**9-1 ans K = list(map(int, input().split())) list(map(int, input().split())) K P = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) P cumsum = [0]*(N+1) [0]*(N+1) cumsum ans = max([ans] + cumsum[1:K+1]) max([ans] + cumsum[1:K+1]) ans ans = max([ans] + cumsum[1:loop_size+1]) max([ans] + cumsum[1:loop_size+1]) ans N, K = list(map(int, input().split())) list(map(int, input().split())) N loop_size = j j loop_size ans = max(ans, score) max(ans, score) ans j = 0 0 j now = i i now j += 1 1 j
N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

ans = -10**9-1
for i in range(N):
    seen = [-1]*N
    cumsum = [0]*(N+1)
    now = i
    j = 0
    while seen[now] == -1:
        seen[now] = 1
        now = P[now]
        cumsum[j+1] = cumsum[j] + C[now]
        j += 1
    loop_size = j
    if K <= loop_size:
        ans = max([ans] + cumsum[1:K+1])
    else:
        if cumsum[loop_size] > 0:
            loop_n = K//loop_size - 1
            cumsum2 = cumsum[0:loop_size+1] + [x + cumsum[loop_size] for x in cumsum[1:loop_size+1]]
            score = cumsum[loop_size]*loop_n + max(cumsum2[0:loop_size+K%loop_size+1])
            ans = max(ans, score)
        else:
            ans = max([ans] + cumsum[1:loop_size+1])
print(ans)