N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
C = [int(i) for i in input().split()]

points = list()
for i in range(N):
    sum = 0
    now = P[i]
    result = list()
    flag = True
    for j in range(N):
        sum += C[now]
        result.append(sum)
        if now == i:
            break
        if j == N-1 and now != i:
            flag = False
        now = P[now]

    if flag:
        points.append(result)
    else:
        # iスタートでiに戻るループが存在しない
        points.append([-int(1e19)])

ans = -int(1e19)

for i in range(len(points)):
    now = i
    tmpp = 0
    k = K
    while points[now][0] == -int(1e19):
        now = P[now]
        tmpp += C[now]
        k -= 1
        ans = max(ans, tmpp)

    cir = len(points[now])
    cir_point = points[now][cir-1]

    if k <= cir:
        for j in range(k):
            ans = max(ans, points[now][j] + tmpp)
    else:
        if cir_point > 0:
            lm = k // cir - 1
            for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
            for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
        else:
            for j in range(cir):
                ans = max(ans, points[now][j] + tmpp)

print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(i) - 1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) - 1 int(i) int i 1 P = [int(i) - 1 for i in input().split()] P [int(i) - 1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = [int(i) for i in input().split()] C [int(i) for i in input().split()] points = list() points list() list for i in range(N):
    sum = 0
    now = P[i]
    result = list()
    flag = True
    for j in range(N):
        sum += C[now]
        result.append(sum)
        if now == i:
            break
        if j == N-1 and now != i:
            flag = False
        now = P[now]

    if flag:
        points.append(result)
    else:
        # iスタートでiに戻るループが存在しない
        points.append([-int(1e19)]) i range(N) range N sum = 0 sum 0 now = P[i] now P[i] P i result = list() result list() list flag = True flag True for j in range(N):
        sum += C[now]
        result.append(sum)
        if now == i:
            break
        if j == N-1 and now != i:
            flag = False
        now = P[now]

     j range(N) range N sum += C[now] sum C[now] C now result.append(sum) result.append result append sum if now == i:
            break
         now == i now i break if j == N-1 and now != i:
            flag = False
         j == N-1 and now != i j == N-1 j N-1 N 1 now != i now i flag = False flag False now = P[now] now P[now] P now if flag:
        points.append(result)
    else:
        # iスタートでiに戻るループが存在しない
        points.append([-int(1e19)]) flag points.append(result) points.append points append result points.append([-int(1e19)]) points.append points append [-int(1e19)] -int(1e19) int(1e19) int 1e19 ans = -int(1e19) ans -int(1e19) int(1e19) int 1e19 for i in range(len(points)):
    now = i
    tmpp = 0
    k = K
    while points[now][0] == -int(1e19):
        now = P[now]
        tmpp += C[now]
        k -= 1
        ans = max(ans, tmpp)

    cir = len(points[now])
    cir_point = points[now][cir-1]

    if k <= cir:
        for j in range(k):
            ans = max(ans, points[now][j] + tmpp)
    else:
        if cir_point > 0:
            lm = k // cir - 1
            for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
            for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
        else:
            for j in range(cir):
                ans = max(ans, points[now][j] + tmpp) i range(len(points)) range len(points) len points now = i now i tmpp = 0 tmpp 0 k = K k K while points[now][0] == -int(1e19):
        now = P[now]
        tmpp += C[now]
        k -= 1
        ans = max(ans, tmpp)

     points[now][0] == -int(1e19) points[now][0] [now] points now 0 -int(1e19) int(1e19) int 1e19 now = P[now] now P[now] P now tmpp += C[now] tmpp C[now] C now k -= 1 k 1 ans = max(ans, tmpp) ans max(ans, tmpp) max ans tmpp cir = len(points[now]) cir len(points[now]) len points[now] points now cir_point = points[now][cir-1] cir_point points[now][cir-1] [now] points now cir-1 cir 1 if k <= cir:
        for j in range(k):
            ans = max(ans, points[now][j] + tmpp)
    else:
        if cir_point > 0:
            lm = k // cir - 1
            for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
            for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
        else:
            for j in range(cir):
                ans = max(ans, points[now][j] + tmpp) k <= cir k cir for j in range(k):
            ans = max(ans, points[now][j] + tmpp)
     j range(k) range k ans = max(ans, points[now][j] + tmpp) ans max(ans, points[now][j] + tmpp) max ans points[now][j] + tmpp points[now][j] [now] points now j tmpp if cir_point > 0:
            lm = k // cir - 1
            for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
            for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
        else:
            for j in range(cir):
                ans = max(ans, points[now][j] + tmpp) cir_point > 0 cir_point 0 lm = k // cir - 1 lm k // cir - 1 k // cir k cir 1 for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
             j range(cir) range cir ans = max(ans, points[now][j] + lm * cir_point + tmpp) ans max(ans, points[now][j] + lm * cir_point + tmpp) max ans points[now][j] + lm * cir_point + tmpp points[now][j] + lm * cir_point points[now][j] [now] points now j lm * cir_point lm cir_point tmpp for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
         j range(k % cir) range k % cir k cir ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp) ans max(ans, points[now][j] + (lm + 1) * cir_point + tmpp) max ans points[now][j] + (lm + 1) * cir_point + tmpp points[now][j] + (lm + 1) * cir_point points[now][j] [now] points now j (lm + 1) * cir_point lm + 1 lm 1 cir_point tmpp for j in range(cir):
                ans = max(ans, points[now][j] + tmpp) j range(cir) range cir ans = max(ans, points[now][j] + tmpp) ans max(ans, points[now][j] + tmpp) max ans points[now][j] + tmpp points[now][j] [now] points now j tmpp print(ans) print ans sum = 0 0 sum ans = max(ans, points[now][j] + tmpp) max(ans, points[now][j] + tmpp) ans K = map(int, input().split()) map(int, input().split()) K k -= 1 1 k k = K K k points = list() list() points ans = max(ans, points[now][j] + tmpp) max(ans, points[now][j] + tmpp) ans now = P[i] P[i] now sum += C[now] C[now] sum tmpp = 0 0 tmpp now = P[now] P[now] now P = [int(i) - 1 for i in input().split()] [int(i) - 1 for i in input().split()] P cir_point = points[now][cir-1] points[now][cir-1] cir_point ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp) max(ans, points[now][j] + (lm + 1) * cir_point + tmpp) ans now = i i now tmpp += C[now] C[now] tmpp lm = k // cir - 1 k // cir - 1 lm now = P[now] P[now] now C = [int(i) for i in input().split()] [int(i) for i in input().split()] C N, K = map(int, input().split()) map(int, input().split()) N flag = True True flag ans = -int(1e19) -int(1e19) ans cir = len(points[now]) len(points[now]) cir ans = max(ans, tmpp) max(ans, tmpp) ans result = list() list() result ans = max(ans, points[now][j] + lm * cir_point + tmpp) max(ans, points[now][j] + lm * cir_point + tmpp) ans flag = False False flag
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
C = [int(i) for i in input().split()]

points = list()
for i in range(N):
    sum = 0
    now = P[i]
    result = list()
    flag = True
    for j in range(N):
        sum += C[now]
        result.append(sum)
        if now == i:
            break
        if j == N-1 and now != i:
            flag = False
        now = P[now]

    if flag:
        points.append(result)
    else:
        # iスタートでiに戻るループが存在しない
        points.append([-int(1e19)])

ans = -int(1e19)

for i in range(len(points)):
    now = i
    tmpp = 0
    k = K
    while points[now][0] == -int(1e19):
        now = P[now]
        tmpp += C[now]
        k -= 1
        ans = max(ans, tmpp)

    cir = len(points[now])
    cir_point = points[now][cir-1]

    if k <= cir:
        for j in range(k):
            ans = max(ans, points[now][j] + tmpp)
    else:
        if cir_point > 0:
            lm = k // cir - 1
            for j in range(cir):
                ans = max(ans, points[now][j] + lm * cir_point + tmpp)
            for j in range(k % cir):
                ans = max(ans, points[now][j] + (lm + 1) * cir_point + tmpp)
        else:
            for j in range(cir):
                ans = max(ans, points[now][j] + tmpp)

print(ans)