import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    P = LI_()
    C = LI()
    G = [-1 for _ in range(N)]
    for i, p in enumerate(P):
        G[i] = p
    seen = set()

    loop_score = []
    loop_size = []
    loop_id2node = collections.defaultdict(list)

    _id = 0
    def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

    for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
    ans = -inf
    for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
    print(ans)

main()
 import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools math string itertools fractions heapq collections re array bisect sys random time copy functools from collections import deque sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 inf = 10**20 inf 10**20 10 20 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 DR = [1, -1, 0, 0] DR [1, -1, 0, 0] 1 -1 0 0 DC = [0, 0, 1, -1] DC [0, 0, 1, -1] 0 0 1 -1 int(x) for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x) int x def LI(): return [int(x) for x in sys.stdin.readline().split()] LI int(x)-1 for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x)-1 int(x) int x 1 def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ float(x) for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() float(x) float x def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def LS(): return sys.stdin.readline().split() LS def I(): return int(sys.stdin.readline()) I def F(): return float(sys.stdin.readline()) F def S(): return input()
      S def main():
    N, K = LI()
    P = LI_()
    C = LI()
    G = [-1 for _ in range(N)]
    for i, p in enumerate(P):
        G[i] = p
    seen = set()

    loop_score = []
    loop_size = []
    loop_id2node = collections.defaultdict(list)

    _id = 0
    def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

    for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
    ans = -inf
    for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
    print(ans) main N, K = LI() N LI() LI K P = LI_() P LI_() LI_ C = LI() C LI() LI -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 G = [-1 for _ in range(N)] G [-1 for _ in range(N)] for i, p in enumerate(P):
        G[i] = p
     i p enumerate(P) enumerate P G[i] = p G[i] G i p seen = set() seen set() set loop_score = [] loop_score [] loop_size = [] loop_size [] loop_id2node = collections.defaultdict(list) loop_id2node collections.defaultdict(list) collections.defaultdict collections defaultdict list _id = 0 _id 0 def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

     calc_one_loop nodes = set() nodes set() set if G[v] == -1:
            return -1, -1
        # calc loop, and return score
         G[v] == -1 G[v] G v -1 return -1, -1 return -1, -1 -1 -1 score = 0 score 0 size = 0 size 0 while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
         G[v] not in seen G[v] G v seen v = G[v] v G[v] G v seen.add(v) seen.add seen add v nodes.add(v) nodes.add nodes add v score += C[v] score C[v] C v size += 1 size 1 return score, size, nodes return score, size, nodes score size nodes v v for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
     i range(N) range N if i in seen:
            continue
         i in seen i seen continue score, size, nodes = calc_one_loop(i) score calc_one_loop(i) calc_one_loop i size nodes if score == -1 and size == -1:
            continue
         score == -1 and size == -1 score == -1 score -1 size == -1 size -1 continue loop_score.append(score) loop_score.append loop_score append score loop_size.append(size) loop_size.append loop_size append size loop_id2node[_id].extend(list(nodes)) [_id].extend [_id] loop_id2node _id extend list(nodes) list nodes seen.add(i) seen.add seen add i _id += 1 _id 1 ans = -inf ans -inf inf for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
     i range(_id) range _id n_loop = K // loop_size[i] n_loop K // loop_size[i] K loop_size[i] loop_size i [0] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [0] 0 score_after = [[0] for _ in range(N)] score_after [[0] for _ in range(N)] for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
         v loop_id2node[i] loop_id2node i max_move_cnt = loop_size[i] + 1 max_move_cnt loop_size[i] + 1 loop_size[i] loop_size i 1 move_cnt = 0 move_cnt 0 cum_score = 0 cum_score 0 cur_v = v cur_v v while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
         move_cnt < max_move_cnt move_cnt max_move_cnt cur_v = G[cur_v] cur_v G[cur_v] G cur_v cum_score += C[cur_v] cum_score C[cur_v] C cur_v score_after[v].append(cum_score) [v].append [v] score_after v append cum_score move_cnt += 1 move_cnt 1 if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
         loop_score[i] <= 0 loop_score[i] loop_score i 0 max_step = min(K, loop_size[i]) max_step min(K, loop_size[i]) min K loop_size[i] loop_size i score = 0 score 0 score = 0 score 0 max_step = loop_size[i] max_step loop_size[i] loop_size i for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
     l range(1, max_step + 1) range 1 max_step + 1 max_step 1 for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
     v loop_id2node[i] loop_id2node i cp_score = score cp_score score cnt = 0 cnt 0 cp_score += score_after[v][l] cp_score score_after[v][l] [v] score_after v l if l > K:
                    continue
                # 回った時も試してみる
                 l > K l K continue if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                 K - l >= loop_size[i] K - l K l loop_size[i] loop_size i cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i])) cp_score max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i])) max cp_score cp_score + loop_score[i] * ((K - l) // loop_size[i]) cp_score loop_score[i] * ((K - l) // loop_size[i]) loop_score[i] loop_score i (K - l) // loop_size[i] K - l K l loop_size[i] loop_size i ans = max(ans, cp_score) ans max(ans, cp_score) max ans cp_score print(ans) print ans main() main def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI(): return [int(x) for x in sys.stdin.readline().split()] LI def LF(): return [float(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def main():
    N, K = LI()
    P = LI_()
    C = LI()
    G = [-1 for _ in range(N)]
    for i, p in enumerate(P):
        G[i] = p
    seen = set()

    loop_score = []
    loop_size = []
    loop_id2node = collections.defaultdict(list)

    _id = 0
    def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

    for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
    ans = -inf
    for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
    print(ans) def main():
    N, K = LI()
    P = LI_()
    C = LI()
    G = [-1 for _ in range(N)]
    for i, p in enumerate(P):
        G[i] = p
    seen = set()

    loop_score = []
    loop_size = []
    loop_id2node = collections.defaultdict(list)

    _id = 0
    def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

    for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
    ans = -inf
    for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
    print(ans) main DR = [1, -1, 0, 0] [1, -1, 0, 0] DR def S(): return input()
      def S(): return input()
      S DC = [0, 0, 1, -1] [0, 0, 1, -1] DC inf = 10**20 10**20 inf mod = 10**9 + 7 10**9 + 7 mod def F(): return float(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) F def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ def LS(): return sys.stdin.readline().split() def LS(): return sys.stdin.readline().split() LS def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    P = LI_()
    C = LI()
    G = [-1 for _ in range(N)]
    for i, p in enumerate(P):
        G[i] = p
    seen = set()

    loop_score = []
    loop_size = []
    loop_id2node = collections.defaultdict(list)

    _id = 0
    def calc_one_loop(v):
        nodes = set()
        if G[v] == -1:
            return -1, -1
        # calc loop, and return score
        score = 0
        size = 0
        while G[v] not in seen:
            v = G[v]
            seen.add(v)
            nodes.add(v)
            score += C[v]
            size += 1
        return score, size, nodes

    for i in range(N):
        if i in seen:
            continue
        score, size, nodes = calc_one_loop(i)
        if score == -1 and size == -1:
            continue
        loop_score.append(score)
        loop_size.append(size)
        loop_id2node[_id].extend(list(nodes))
        seen.add(i)
        _id += 1

    # print('loop_size: ', loop_size)
    # print('loop_score: ', loop_score)
    ans = -inf
    for i in range(_id):
        # i: id
        n_loop = K // loop_size[i]

        # score_afterを作る
        score_after = [[0] for _ in range(N)]
        for v in loop_id2node[i]:
            max_move_cnt = loop_size[i] + 1
            move_cnt = 0
            cum_score = 0
            cur_v = v
            while move_cnt < max_move_cnt:
                cur_v = G[cur_v]
                cum_score += C[cur_v]
                score_after[v].append(cum_score)
                move_cnt += 1
        # print('score_after: ', score_after)
        if loop_score[i] <= 0:
            max_step = min(K, loop_size[i])
            score = 0
        else:
            # 先に回る
            # ちょうど1周だったら、回らない
            score = 0
            max_step = loop_size[i]

            # if K == loop_size[i]:
            #     max_step = K
            #     score = 0
            # else:
            #     K -= n_loop * loop_size[i]
            #     max_step = K
            #     score = n_loop * loop_score[i]

        # loop_size[i]より小さい数で全探索
        for l in range(1, max_step + 1):
            for v in loop_id2node[i]:
                cp_score = score
                cnt = 0
                cp_score += score_after[v][l]
                # 実現不可能なら飛ばす
                if l > K:
                    continue
                # 回った時も試してみる
                if K - l >= loop_size[i]:
                    cp_score = max(cp_score, cp_score + loop_score[i] * ((K - l) // loop_size[i]))
                ans = max(ans, cp_score)
    print(ans)

main()

