n, k = map(int, input().split())
P = [*map(lambda x: int(x)-1, input().split())]
C = [*map(int, input().split())]

score = -10**18
for st in range(n):
    lap_sc = sum_sc = 0
    nx = st
    for lap_cn in range(1, n+1):
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
    lap_sc = max(0, lap_sc)

    for k_cn in range(1, k+1):
        sum_sc += C[nx]
        score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn))
        nx = P[nx]
        if nx == st: break

print(score) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k P = [*map(lambda x: int(x)-1, input().split())] P [*map(lambda x: int(x)-1, input().split())] *map(lambda x: int(x)-1, input().split()) 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 = [*map(int, input().split())] C [*map(int, input().split())] *map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split score = -10**18 score -10**18 -10 18 for st in range(n):
    lap_sc = sum_sc = 0
    nx = st
    for lap_cn in range(1, n+1):
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
    lap_sc = max(0, lap_sc)

    for k_cn in range(1, k+1):
        sum_sc += C[nx]
        score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn))
        nx = P[nx]
        if nx == st: break st range(n) range n lap_sc = sum_sc = 0 lap_sc 0 = sum_sc = 0 sum_sc 0 nx = st nx st for lap_cn in range(1, n+1):
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
     lap_cn range(1, n+1) range 1 n+1 n 1 lap_sc += C[nx] lap_sc C[nx] C nx nx = P[nx] nx P[nx] P nx if nx == st: break
     nx == st nx st lap_sc = max(0, lap_sc) lap_sc max(0, lap_sc) max 0 lap_sc for k_cn in range(1, k+1):
        sum_sc += C[nx]
        score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn))
        nx = P[nx]
        if nx == st: break k_cn range(1, k+1) range 1 k+1 k 1 sum_sc += C[nx] sum_sc C[nx] C nx score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn)) score max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn)) max score sum_sc + lap_sc * ((k - k_cn) // lap_cn) sum_sc lap_sc * ((k - k_cn) // lap_cn) lap_sc (k - k_cn) // lap_cn k - k_cn k k_cn lap_cn nx = P[nx] nx P[nx] P nx if nx == s nx == st nx st print(score) print score nx = P[nx] P[nx] nx sum_sc += C[nx] C[nx] sum_sc C = [*map(int, input().split())] [*map(int, input().split())] C lap_sc = sum_sc = 0 0 lap_sc sum_sc = 0 0 sum_sc nx = P[nx] P[nx] nx lap_sc = max(0, lap_sc) max(0, lap_sc) lap_sc lap_sc += C[nx] C[nx] lap_sc score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn)) max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn)) score n, k = map(int, input().split()) map(int, input().split()) n nx = st st nx k = map(int, input().split()) map(int, input().split()) k score = -10**18 -10**18 score P = [*map(lambda x: int(x)-1, input().split())] [*map(lambda x: int(x)-1, input().split())] P
n, k = map(int, input().split())
P = [*map(lambda x: int(x)-1, input().split())]
C = [*map(int, input().split())]

score = -10**18
for st in range(n):
    lap_sc = sum_sc = 0
    nx = st
    for lap_cn in range(1, n+1):
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
    lap_sc = max(0, lap_sc)

    for k_cn in range(1, k+1):
        sum_sc += C[nx]
        score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn))
        nx = P[nx]
        if nx == st: break

print(score)
