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

for i in range(N):
    tmp_max = [-10**9]*(N+1)
    total = 0
    current_block = P[i]
    for j in range(1, N + 1):
        total += C[current_block]
        tmp_max[j] = max(tmp_max[j-1], total)
        if current_block == i:
            break
        current_block = P[current_block]
    if total <= 0:
        ans = max(ans, tmp_max[j])
    else:
        n = K // j
        r = K % j
        if r == 0:
            ans = max(ans, total*(n-1) + tmp_max[j])
        else:
            ans = max(ans, total*n + tmp_max[r])

print(ans) N, K = map(int, input().split()) N 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 ans -10**9 -10 9 for i in range(N):
    tmp_max = [-10**9]*(N+1)
    total = 0
    current_block = P[i]
    for j in range(1, N + 1):
        total += C[current_block]
        tmp_max[j] = max(tmp_max[j-1], total)
        if current_block == i:
            break
        current_block = P[current_block]
    if total <= 0:
        ans = max(ans, tmp_max[j])
    else:
        n = K // j
        r = K % j
        if r == 0:
            ans = max(ans, total*(n-1) + tmp_max[j])
        else:
            ans = max(ans, total*n + tmp_max[r]) i range(N) range N tmp_max = [-10**9]*(N+1) tmp_max [-10**9]*(N+1) [-10**9] -10**9 -10 9 N+1 N 1 total = 0 total 0 current_block = P[i] current_block P[i] P i for j in range(1, N + 1):
        total += C[current_block]
        tmp_max[j] = max(tmp_max[j-1], total)
        if current_block == i:
            break
        current_block = P[current_block]
     j range(1, N + 1) range 1 N + 1 N 1 total += C[current_block] total C[current_block] C current_block tmp_max[j] = max(tmp_max[j-1], total) tmp_max[j] tmp_max j max(tmp_max[j-1], total) max tmp_max[j-1] tmp_max j-1 j 1 total if current_block == i:
            break
         current_block == i current_block i break current_block = P[current_block] current_block P[current_block] P current_block if total <= 0:
        ans = max(ans, tmp_max[j])
    else:
        n = K // j
        r = K % j
        if r == 0:
            ans = max(ans, total*(n-1) + tmp_max[j])
        else:
            ans = max(ans, total*n + tmp_max[r]) total <= 0 total 0 ans = max(ans, tmp_max[j]) ans max(ans, tmp_max[j]) max ans tmp_max[j] tmp_max j n = K // j n K // j K j r = K % j r K % j K j if r == 0:
            ans = max(ans, total*(n-1) + tmp_max[j])
        else:
            ans = max(ans, total*n + tmp_max[r]) r == 0 r 0 ans = max(ans, total*(n-1) + tmp_max[j]) ans max(ans, total*(n-1) + tmp_max[j]) max ans total*(n-1) + tmp_max[j] total*(n-1) total n-1 n 1 tmp_max[j] tmp_max j ans = max(ans, total*n + tmp_max[r]) ans max(ans, total*n + tmp_max[r]) max ans total*n + tmp_max[r] total*n total n tmp_max[r] tmp_max r print(ans) print ans n = K // j K // j n tmp_max = [-10**9]*(N+1) [-10**9]*(N+1) tmp_max N, K = map(int, input().split()) map(int, input().split()) N P = list(map(lambda x:int(x) - 1,input().split())) list(map(lambda x:int(x) - 1,input().split())) P total += C[current_block] C[current_block] total ans = max(ans, tmp_max[j]) max(ans, tmp_max[j]) ans current_block = P[current_block] P[current_block] current_block r = K % j K % j r C = list(map(int, input().split())) list(map(int, input().split())) C ans = -10**9 -10**9 ans K = map(int, input().split()) map(int, input().split()) K total = 0 0 total ans = max(ans, total*(n-1) + tmp_max[j]) max(ans, total*(n-1) + tmp_max[j]) ans current_block = P[i] P[i] current_block ans = max(ans, total*n + tmp_max[r]) max(ans, total*n + tmp_max[r]) ans
N, K = map(int, input().split())
P = list(map(lambda x:int(x) - 1,input().split()))
C = list(map(int, input().split()))
ans = -10**9

for i in range(N):
    tmp_max = [-10**9]*(N+1)
    total = 0
    current_block = P[i]
    for j in range(1, N + 1):
        total += C[current_block]
        tmp_max[j] = max(tmp_max[j-1], total)
        if current_block == i:
            break
        current_block = P[current_block]
    if total <= 0:
        ans = max(ans, tmp_max[j])
    else:
        n = K // j
        r = K % j
        if r == 0:
            ans = max(ans, total*(n-1) + tmp_max[j])
        else:
            ans = max(ans, total*n + tmp_max[r])

print(ans)