import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C


def main():
    N, K = NMI()
    P = NLI()
    P = [p-1 for p in P]
    C = NLI()

    seen = [0] * N
    loops = []
    for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

    ans = -float("inf")
    for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

    print(ans)



if __name__ == "__main__":
    main() import sys sys import math math from collections import defaultdict from collections import deque sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 input = lambda: sys.stdin.readline().strip() input lambda: sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip NI = lambda: int(input()) NI lambda: int(input()) int(input()) int input() input NMI = lambda: map(int, input().split()) NMI lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split NLI = lambda: list(NMI()) NLI lambda: list(NMI()) list(NMI()) list NMI() NMI SI = lambda: input() SI lambda: input() input() input def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C make_cumulative C = [0] * (len(A) + 1) C [0] * (len(A) + 1) [0] 0 len(A) + 1 len(A) len A 1 for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
     i a enumerate(A) enumerate A i += 1 i 1 C[i] = C[i - 1] + a C[i] C i C[i - 1] + a C[i - 1] C i - 1 i 1 a return C C A A def main():
    N, K = NMI()
    P = NLI()
    P = [p-1 for p in P]
    C = NLI()

    seen = [0] * N
    loops = []
    for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

    ans = -float("inf")
    for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

    print(ans) main N, K = NMI() N NMI() NMI K P = NLI() P NLI() NLI p-1 for p in P for p in P p P for p in P p-1 p 1 P = [p-1 for p in P] P [p-1 for p in P] C = NLI() C NLI() NLI seen = [0] * N seen [0] * N [0] 0 N loops = [] loops [] for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

     i range(N) range N if seen[i] == 1:
            continue
         seen[i] == 1 seen[i] seen i 1 continue now = i now i now_loop = [now] now_loop [now] now now_cost = C[now] now_cost C[now] C now while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
         True seen[now] = 1 seen[now] seen now 1 goto = P[now] goto P[now] P now if seen[goto] == 1:
                break
             seen[goto] == 1 seen[goto] seen goto 1 break now_loop.append(goto) now_loop.append now_loop append goto now_cost += C[goto] now_cost C[goto] C goto now = goto now goto loops.append([now_loop, now_cost]) loops.append loops append [now_loop, now_cost] now_loop now_cost ans = -float("inf") ans -float("inf") float("inf") float "inf" for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

     loop cost loops C[p] for p in loop for p in loop p loop for p in loop C[p] C p C_cum = make_cumulative([C[p] for p in loop]) C_cum make_cumulative([C[p] for p in loop]) make_cumulative [C[p] for p in loop] for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

     i start enumerate(loop) enumerate loop for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

     j end enumerate(loop) enumerate loop if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                 i == j i j base = cost base cost moved = len(loop) moved len(loop) len loop elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                 i < j i j base = C_cum[j] - C_cum[i] base C_cum[j] - C_cum[i] C_cum[j] C_cum j C_cum[i] C_cum i moved = j - i moved j - i j i base = cost - (C_cum[i] - C_cum[j]) base cost - (C_cum[i] - C_cum[j]) cost C_cum[i] - C_cum[j] C_cum[i] C_cum i C_cum[j] C_cum j moved = len(loop) + j - i moved len(loop) + j - i len(loop) + j len(loop) len loop j i if moved > K:
                    continue

                 moved > K moved K continue if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                 cost > 0 cost 0 tmp_K = K - moved tmp_K K - moved K moved num_loop = tmp_K // len(loop) num_loop tmp_K // len(loop) tmp_K len(loop) len loop tmp_ans = num_loop * cost + base tmp_ans num_loop * cost + base num_loop * cost num_loop cost base tmp_ans = base tmp_ans base ans = max(ans, tmp_ans) ans max(ans, tmp_ans) max ans tmp_ans print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main NI = lambda: int(input()) lambda: int(input()) NI SI = lambda: input() lambda: input() SI NMI = lambda: map(int, input().split()) lambda: map(int, input().split()) NMI NLI = lambda: list(NMI()) lambda: list(NMI()) NLI def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C make_cumulative input = lambda: sys.stdin.readline().strip() lambda: sys.stdin.readline().strip() input def main():
    N, K = NMI()
    P = NLI()
    P = [p-1 for p in P]
    C = NLI()

    seen = [0] * N
    loops = []
    for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

    ans = -float("inf")
    for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

    print(ans) def main():
    N, K = NMI()
    P = NLI()
    P = [p-1 for p in P]
    C = NLI()

    seen = [0] * N
    loops = []
    for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

    ans = -float("inf")
    for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

    print(ans) main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C


def main():
    N, K = NMI()
    P = NLI()
    P = [p-1 for p in P]
    C = NLI()

    seen = [0] * N
    loops = []
    for i in range(N):
        if seen[i] == 1:
            continue
        now = i
        now_loop = [now]
        now_cost = C[now]
        while True:
            seen[now] = 1
            goto = P[now]
            if seen[goto] == 1:
                break
            now_loop.append(goto)
            now_cost += C[goto]
            now = goto
        loops.append([now_loop, now_cost])

    ans = -float("inf")
    for loop, cost in loops:
        C_cum = make_cumulative([C[p] for p in loop])

        for i, start in enumerate(loop):
            for j, end in enumerate(loop):
                if i == j:
                    base = cost
                    moved = len(loop)
                elif i < j:
                    base = C_cum[j] - C_cum[i]
                    moved = j - i
                else:
                    base = cost - (C_cum[i] - C_cum[j])
                    moved = len(loop) + j - i

                if moved > K:
                    continue

                if cost > 0:
                    tmp_K = K - moved
                    num_loop = tmp_K // len(loop)
                    tmp_ans = num_loop * cost + base
                else:
                    tmp_ans = base

                ans = max(ans, tmp_ans)

    print(ans)



if __name__ == "__main__":
    main()