N,K = map(int, input().split())
P = [int(p) for p in input().split()]
C = [int(c) for c in input().split()]

ans = max(C)
for i in range(N):
    used = [0]*N
    used[i] = 1
    loop = [C[i]]
    j = P[i]-1
    while used[j] == 0:
        used[j] = 1
        loop.append(loop[-1]+C[j])
        j = P[j]-1
    M = len(loop)
    if K >= M:
        if K%M == 0:
            t = 0
        else:
            t = max(loop[:K%M])
            t = max(t, 0)
        num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop))
    else:
        num = max(loop[:K])
    ans = max(ans, num)
if max(C) <= 0:
    ans = max(C)
print(ans) N,K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(p) for p in input().split() for p in input().split() p input().split() ().split () input split for p in input().split() int(p) int p P = [int(p) for p in input().split()] P [int(p) for p in input().split()] int(c) for c in input().split() for c in input().split() c input().split() ().split () input split for c in input().split() int(c) int c C = [int(c) for c in input().split()] C [int(c) for c in input().split()] ans = max(C) ans max(C) max C for i in range(N):
    used = [0]*N
    used[i] = 1
    loop = [C[i]]
    j = P[i]-1
    while used[j] == 0:
        used[j] = 1
        loop.append(loop[-1]+C[j])
        j = P[j]-1
    M = len(loop)
    if K >= M:
        if K%M == 0:
            t = 0
        else:
            t = max(loop[:K%M])
            t = max(t, 0)
        num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop))
    else:
        num = max(loop[:K])
    ans = max(ans, num) i range(N) range N used = [0]*N used [0]*N [0] 0 N used[i] = 1 used[i] used i 1 loop = [C[i]] loop [C[i]] C[i] C i j = P[i]-1 j P[i]-1 P[i] P i 1 while used[j] == 0:
        used[j] = 1
        loop.append(loop[-1]+C[j])
        j = P[j]-1
     used[j] == 0 used[j] used j 0 used[j] = 1 used[j] used j 1 loop.append(loop[-1]+C[j]) loop.append loop append loop[-1]+C[j] loop[-1] loop -1 C[j] C j j = P[j]-1 j P[j]-1 P[j] P j 1 M = len(loop) M len(loop) len loop if K >= M:
        if K%M == 0:
            t = 0
        else:
            t = max(loop[:K%M])
            t = max(t, 0)
        num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop))
    else:
        num = max(loop[:K])
     K >= M K M if K%M == 0:
            t = 0
        else:
            t = max(loop[:K%M])
            t = max(t, 0)
         K%M == 0 K%M K M 0 t = 0 t 0 t = max(loop[:K%M]) t max(loop[:K%M]) max loop[:K%M] loop :K%M K%M K M t = max(t, 0) t max(t, 0) max t 0 num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop)) num max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop)) max loop[-1]*(K//M)+t loop[-1]*(K//M) loop[-1] loop -1 K//M K M t max(loop) max loop loop[-1]*(K//M-1)+max(loop) loop[-1]*(K//M-1) loop[-1] loop -1 K//M-1 K//M K M 1 max(loop) max loop num = max(loop[:K]) num max(loop[:K]) max loop[:K] loop :K K ans = max(ans, num) ans max(ans, num) max ans num if max(C) <= 0:
    ans = max(C) max(C) <= 0 max(C) max C 0 ans = max(C) ans max(C) max C print(ans) print ans M = len(loop) len(loop) M j = P[i]-1 P[i]-1 j num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop)) max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop)) num t = max(t, 0) max(t, 0) t ans = max(ans, num) max(ans, num) ans P = [int(p) for p in input().split()] [int(p) for p in input().split()] P K = map(int, input().split()) map(int, input().split()) K t = max(loop[:K%M]) max(loop[:K%M]) t used = [0]*N [0]*N used C = [int(c) for c in input().split()] [int(c) for c in input().split()] C j = P[j]-1 P[j]-1 j ans = max(C) max(C) ans ans = max(C) max(C) ans loop = [C[i]] [C[i]] loop num = max(loop[:K]) max(loop[:K]) num t = 0 0 t N,K = map(int, input().split()) map(int, input().split()) N
N,K = map(int, input().split())
P = [int(p) for p in input().split()]
C = [int(c) for c in input().split()]

ans = max(C)
for i in range(N):
    used = [0]*N
    used[i] = 1
    loop = [C[i]]
    j = P[i]-1
    while used[j] == 0:
        used[j] = 1
        loop.append(loop[-1]+C[j])
        j = P[j]-1
    M = len(loop)
    if K >= M:
        if K%M == 0:
            t = 0
        else:
            t = max(loop[:K%M])
            t = max(t, 0)
        num = max(loop[-1]*(K//M)+t, max(loop), loop[-1]*(K//M-1)+max(loop))
    else:
        num = max(loop[:K])
    ans = max(ans, num)
if max(C) <= 0:
    ans = max(C)
print(ans)