#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left
from itertools import product
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def LF(): return list(map(float, input().split()))
def LC(): return [c for c in input().split()]
def LLI(n): return [LI() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]

def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)]

def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret


INF = float("inf")
MOD = 10**9 + 7


def main():
    N, K = MAP()
    P = LI_()
    C = LI()

    # サイクルとは閉路のことである。順列の性質として、有向グラフで記述した時、必
    # ずサイクルの集合となる。
    ans = -INF
    for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

    print(ans)
    return


if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10**8) sys.setrecursionlimit sys setrecursionlimit 10**8 10 8 from bisect import bisect_left from itertools import product def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LI(): return list(map(int, input().split())) LI def LI_(): return list(map(lambda x: int(x)-1, input().split())) LI_ def LF(): return list(map(float, input().split())) LF c for c in input().split() for c in input().split() c input().split() ().split () input split for c in input().split() c def LC(): return [c for c in input().split()] LC LI() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI() LI def LLI(n): return [LI() for _ in range(n)] LLI n n input() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) input() input def NSTR(n): return [input() for _ in range(n)] NSTR n n def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)] array2d [initial]*M for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [initial]*M [initial] initial M return [[initial]*M for _ in range(N)] [[initial]*M for _ in range(N)] N N M M initial=0 initial 0 def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret copy2d ret = array2d(N, M) ret array2d(N, M) array2d N M for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
     i range(N) range N for j in range(M):
            ret[i][j] = orig[i][j]
     j range(M) range M ret[i][j] = orig[i][j] ret[i][j] [i] ret i j orig[i][j] [i] orig i j return ret ret orig orig N N M M INF = float("inf") INF float("inf") float "inf" MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 def main():
    N, K = MAP()
    P = LI_()
    C = LI()

    # サイクルとは閉路のことである。順列の性質として、有向グラフで記述した時、必
    # ずサイクルの集合となる。
    ans = -INF
    for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

    print(ans)
    return main N, K = MAP() N MAP() MAP K P = LI_() P LI_() LI_ C = LI() C LI() LI ans = -INF ans -INF INF for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

     i range(N) range N v = i v i cycle_sum = 0 cycle_sum 0 cycle_cnt = 0 cycle_cnt 0 while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

         True cycle_cnt += 1 cycle_cnt 1 cycle_sum += C[v] cycle_sum C[v] C v v = P[v] v P[v] P v if v == i:
                break

         v == i v i break path = 0 path 0 cnt = 0 cnt 0 while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

     True cnt += 1 cnt 1 path += C[v] path C[v] C v if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
             cnt > K cnt K break num = (K-cnt)//cycle_cnt num (K-cnt)//cycle_cnt K-cnt K cnt cycle_cnt score = path + max(0, cycle_sum)*num score path + max(0, cycle_sum)*num path max(0, cycle_sum)*num max(0, cycle_sum) max 0 cycle_sum num ans = max(ans, score) ans max(ans, score) max ans score v = P[v] v P[v] P v if v == i:
                break

     v == i v i break print(ans) print ans return if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def LLI(n): return [LI() for _ in range(n)] def LLI(n): return [LI() for _ in range(n)] LLI def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)] def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)] array2d def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret copy2d def LF(): return list(map(float, input().split())) def LF(): return list(map(float, input().split())) LF def NSTR(n): return [input() for _ in range(n)] def NSTR(n): return [input() for _ in range(n)] NSTR def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP def LI_(): return list(map(lambda x: int(x)-1, input().split())) def LI_(): return list(map(lambda x: int(x)-1, input().split())) LI_ INF = float("inf") float("inf") INF def LC(): return [c for c in input().split()] def LC(): return [c for c in input().split()] LC def main():
    N, K = MAP()
    P = LI_()
    C = LI()

    # サイクルとは閉路のことである。順列の性質として、有向グラフで記述した時、必
    # ずサイクルの集合となる。
    ans = -INF
    for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

    print(ans)
    return def main():
    N, K = MAP()
    P = LI_()
    C = LI()

    # サイクルとは閉路のことである。順列の性質として、有向グラフで記述した時、必
    # ずサイクルの集合となる。
    ans = -INF
    for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

    print(ans)
    return main MOD = 10**9 + 7 10**9 + 7 MOD def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI def INT(): return int(input()) def INT(): return int(input()) INT
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from bisect import bisect_left
from itertools import product
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def LF(): return list(map(float, input().split()))
def LC(): return [c for c in input().split()]
def LLI(n): return [LI() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]

def array2d(N, M, initial=0):
    return [[initial]*M for _ in range(N)]

def copy2d(orig, N, M):
    ret = array2d(N, M)
    for i in range(N):
        for j in range(M):
            ret[i][j] = orig[i][j]
    return ret


INF = float("inf")
MOD = 10**9 + 7


def main():
    N, K = MAP()
    P = LI_()
    C = LI()

    # サイクルとは閉路のことである。順列の性質として、有向グラフで記述した時、必
    # ずサイクルの集合となる。
    ans = -INF
    for i in range(N):
        v = i
        cycle_sum = 0
        cycle_cnt = 0

        # iを始点とし、閉路の和と長さを求める
        while True:
            cycle_cnt += 1
            cycle_sum += C[v]
            v = P[v]
            if v == i:
                break

        path = 0
        cnt = 0

        while True:
            cnt += 1
            path += C[v]
            if cnt > K:
                break

            # サイクルを可能な限りまわる。サイクルの総和が負の場合は回らない
            num = (K-cnt)//cycle_cnt
            score = path + max(0, cycle_sum)*num
            ans = max(ans, score)

            v = P[v]
            if v == i:
                break

    print(ans)
    return


if __name__ == '__main__':
    main()
