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

move_db = [[0] * N for _ in range(65)]
point_db = [[0] * N for _ in range(65)]
maxpoint_db = [[0] * N for _ in range(65)]

for i, (p, c) in enumerate(zip(P, C)):
    move_db[0][i] = p
    point_db[0][i] = c
    maxpoint_db[0][i] = c

for i in range(40):
    for j in range(N):
        move_db[i+1][j] = move_db[i][move_db[i][j]]
        point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j]
        maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]])

res = -10 ** 19
for i in range(N):
    tmp, now = 0, i
    for d in range(32):
        if K&(1<<d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now]

print(res) 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 [0] * N for _ in range(65) for _ in range(65) _ range(65) range 65 for _ in range(65) [0] * N [0] 0 N move_db = [[0] * N for _ in range(65)] move_db [[0] * N for _ in range(65)] [0] * N for _ in range(65) for _ in range(65) _ range(65) range 65 for _ in range(65) [0] * N [0] 0 N point_db = [[0] * N for _ in range(65)] point_db [[0] * N for _ in range(65)] [0] * N for _ in range(65) for _ in range(65) _ range(65) range 65 for _ in range(65) [0] * N [0] 0 N maxpoint_db = [[0] * N for _ in range(65)] maxpoint_db [[0] * N for _ in range(65)] for i, (p, c) in enumerate(zip(P, C)):
    move_db[0][i] = p
    point_db[0][i] = c
    maxpoint_db[0][i] = c i (p, c) p c enumerate(zip(P, C)) enumerate zip(P, C) zip P C move_db[0][i] = p move_db[0][i] [0] move_db 0 i p point_db[0][i] = c point_db[0][i] [0] point_db 0 i c maxpoint_db[0][i] = c maxpoint_db[0][i] [0] maxpoint_db 0 i c for i in range(40):
    for j in range(N):
        move_db[i+1][j] = move_db[i][move_db[i][j]]
        point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j]
        maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]]) i range(40) range 40 for j in range(N):
        move_db[i+1][j] = move_db[i][move_db[i][j]]
        point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j]
        maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]]) j range(N) range N move_db[i+1][j] = move_db[i][move_db[i][j]] move_db[i+1][j] [i+1] move_db i+1 i 1 j move_db[i][move_db[i][j]] [i] move_db i move_db[i][j] [i] move_db i j point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j] point_db[i+1][j] [i+1] point_db i+1 i 1 j point_db[i][move_db[i][j]] + point_db[i][j] point_db[i][move_db[i][j]] [i] point_db i move_db[i][j] [i] move_db i j point_db[i][j] [i] point_db i j maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]]) maxpoint_db[i+1][j] [i+1] maxpoint_db i+1 i 1 j max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]]) max maxpoint_db[i][j] [i] maxpoint_db i j point_db[i][j] + maxpoint_db[i][move_db[i][j]] point_db[i][j] [i] point_db i j maxpoint_db[i][move_db[i][j]] [i] maxpoint_db i move_db[i][j] [i] move_db i j res = -10 ** 19 res -10 ** 19 -10 19 for i in range(N):
    tmp, now = 0, i
    for d in range(32):
        if K&(1<<d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now] i range(N) range N tmp, now = 0, i tmp 0 now i for d in range(32):
        if K&(1<<d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now] d range(32) range 32 if K&(1<<d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now] K&(1<<d) K 1<<d 1 d res = max(res, tmp + maxpoint_db[d][now]) res max(res, tmp + maxpoint_db[d][now]) max res tmp + maxpoint_db[d][now] tmp maxpoint_db[d][now] [d] maxpoint_db d now tmp += point_db[d][now] tmp point_db[d][now] [d] point_db d now now = move_db[d][now] now move_db[d][now] [d] move_db d now print(res) print res now = 0, i i now res = max(res, tmp + maxpoint_db[d][now]) max(res, tmp + maxpoint_db[d][now]) res tmp += point_db[d][now] point_db[d][now] tmp maxpoint_db = [[0] * N for _ in range(65)] [[0] * N for _ in range(65)] maxpoint_db C = list(map(int, input().split())) list(map(int, input().split())) C tmp, now = 0, i 0 tmp K = map(int, input().split()) 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 point_db = [[0] * N for _ in range(65)] [[0] * N for _ in range(65)] point_db N, K = map(int, input().split()) map(int, input().split()) N res = -10 ** 19 -10 ** 19 res now = move_db[d][now] move_db[d][now] now move_db = [[0] * N for _ in range(65)] [[0] * N for _ in range(65)] move_db
N, K = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))

move_db = [[0] * N for _ in range(65)]
point_db = [[0] * N for _ in range(65)]
maxpoint_db = [[0] * N for _ in range(65)]

for i, (p, c) in enumerate(zip(P, C)):
    move_db[0][i] = p
    point_db[0][i] = c
    maxpoint_db[0][i] = c

for i in range(40):
    for j in range(N):
        move_db[i+1][j] = move_db[i][move_db[i][j]]
        point_db[i+1][j] = point_db[i][move_db[i][j]] + point_db[i][j]
        maxpoint_db[i+1][j] = max(maxpoint_db[i][j], point_db[i][j] + maxpoint_db[i][move_db[i][j]])

res = -10 ** 19
for i in range(N):
    tmp, now = 0, i
    for d in range(32):
        if K&(1<<d):
            res = max(res, tmp + maxpoint_db[d][now])
            tmp += point_db[d][now]
            now = move_db[d][now]

print(res)
