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

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

    sum_sc = 0
    k_cn = 0
    while True:
        k_cn += 1
        if k < k_cn: break
        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 INF = 10**18 INF 10**18 10 18 score = -INF score -INF INF for st in range(n):
    lap_cn = 0
    lap_sc = 0
    nx = st
    while True:
        lap_cn += 1
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
    lap_sc = max(0, lap_sc)

    sum_sc = 0
    k_cn = 0
    while True:
        k_cn += 1
        if k < k_cn: break
        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_cn = 0 lap_cn 0 lap_sc = 0 lap_sc 0 nx = st nx st while True:
        lap_cn += 1
        lap_sc += C[nx]
        nx = P[nx]
        if nx == st: break
     True lap_cn += 1 lap_cn 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 sum_sc = 0 sum_sc 0 k_cn = 0 k_cn 0 while True:
        k_cn += 1
        if k < k_cn: break
        sum_sc += C[nx]
        score = max(score, sum_sc + lap_sc * ((k - k_cn) // lap_cn))
        nx = P[nx]
        if nx == st: break True k_cn += 1 k_cn 1 if k < k_cn: break
         k < k_cn k k_cn 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 INF = 10**18 10**18 INF lap_sc = max(0, lap_sc) max(0, lap_sc) 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 sum_sc += C[nx] C[nx] sum_sc n, k = map(int, input().split()) map(int, input().split()) n k = map(int, input().split()) map(int, input().split()) k nx = st st nx lap_cn += 1 1 lap_cn lap_sc = 0 0 lap_sc sum_sc = 0 0 sum_sc k_cn = 0 0 k_cn k_cn += 1 1 k_cn lap_cn = 0 0 lap_cn nx = P[nx] P[nx] nx score = -INF -INF score lap_sc += C[nx] C[nx] lap_sc nx = P[nx] P[nx] nx C = [*map(int, input().split())] [*map(int, input().split())] C 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())]

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

    sum_sc = 0
    k_cn = 0
    while True:
        k_cn += 1
        if k < k_cn: break
        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)
