# https://atcoder.jp/contests/abc175/tasks/abc175_d
# ダブリングで解く
# K>ループ長の場合、累積の最後の値が負ならmaxを取ればいいし、正ならば 累積の最後の値*周数をかけてから計算
import sys
sys.setrecursionlimit(1 << 25)
readline = sys.stdin.buffer.readline
read = sys.stdin.readline  # 文字列読み込む時はこっち
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(readline())


def ints(): return list(map(int, readline().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations, accumulate
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b


N, K = ints()
P = mina(*ints())
C = ints()

next = [[-1] * N for _ in range(32)]  # next[k][i] ノードiから2^k回移動するときのノード
score = [[0] * N for _ in range(32)]  # score[k][i] ノードiから2^k回移動するときに加算されるスコア
# nextの構築
next[0] = P
for k in range(31):
    for i in range(N):
        next[k + 1][i] = next[k][next[k][i]]
# scoreの構築
for i in range(N):
    score[0][i] = C[P[i]]
for k in range(31):
    for i in range(N):
        score[k + 1][i] = score[k][i] + score[k][next[k][i]]


# これじゃ道中がわからない...
# 一点しか求まらない
def get_score(i, k):  # 途中の1点のscoreを算出
    now = i
    ret = 0
    for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
    return ret, now


# 各ノードからK回移動したときのscoreを計算する
ans = -10**10
for i in range(N):
    # print(i, 'について')
    if K <= N:
        # 素直に最初からK回シミュレーションする
        tmp = 0
        now = i
        for _ in range(K):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)
    else:
        # 最初からN回シミュレーション + K-NからN回シミュレーション
        tmp = 0
        now = i
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)

        tmp, now = get_score(i, K - N)
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)


print(ans) import sys sys sys.setrecursionlimit(1 << 25) sys.setrecursionlimit sys setrecursionlimit 1 << 25 1 25 readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline read = sys.stdin.readline read sys.stdin.readline sys.stdin sys stdin readline ra = range ra range enu = enumerate enu enumerate def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit() exit print(*argv, **kwarg) print *argv argv **kwarg kwarg sys.exit() sys.exit sys exit *argv **kwarg def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと mina sub=1 sub 1 *argv def a_int(): return int(readline()) a_int def ints(): return list(map(int, readline().split())) ints def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret))) read_col '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合''' ret = [] ret [] for _ in range(H):
        ret.append(list(map(int, readline().split())))
     _ range(H) range H ret.append(list(map(int, readline().split()))) ret.append ret append list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split return tuple(map(list, zip(*ret))) tuple(map(list, zip(*ret))) tuple map(list, zip(*ret)) map list zip(*ret) zip *ret ret H H def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret read_tuple '''H is number of rows''' ret = [] ret [] for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
     _ range(H) range H ret.append(tuple(map(int, readline().split()))) ret.append ret append tuple(map(int, readline().split())) tuple map(int, readline().split()) map int readline().split() ().split () readline split return ret ret H H def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため read_matrix '''H is number of rows''' ret = [] ret [] for _ in range(H):
        ret.append(list(map(int, readline().split())))
     _ range(H) range H ret.append(list(map(int, readline().split()))) ret.append ret append list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split return ret ret H H MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 INF = 2**31 INF 2**31 2 31 from collections import defaultdict, Counter, deque from operator import itemgetter, xor, add from itertools import product, permutations, combinations, accumulate from bisect import bisect_left, bisect_right from functools import reduce from math import gcd def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b lcm g = gcd(a, b) g gcd(a, b) gcd a b return a // g * b a // g * b a // g a g b a a b b N, K = ints() N ints() ints K P = mina(*ints()) P mina(*ints()) mina *ints() ints() ints C = ints() C ints() ints [-1] * N for _ in range(32) for _ in range(32) _ range(32) range 32 for _ in range(32) [-1] * N [-1] -1 N next = [[-1] * N for _ in range(32)] next [[-1] * N for _ in range(32)] [0] * N for _ in range(32) for _ in range(32) _ range(32) range 32 for _ in range(32) [0] * N [0] 0 N score = [[0] * N for _ in range(32)] score [[0] * N for _ in range(32)] next[0] = P next[0] next 0 P for k in range(31):
    for i in range(N):
        next[k + 1][i] = next[k][next[k][i]]
# scoreの構築 k range(31) range 31 for i in range(N):
        next[k + 1][i] = next[k][next[k][i]]
# scoreの構築 i range(N) range N next[k + 1][i] = next[k][next[k][i]] next[k + 1][i] [k + 1] next k + 1 k 1 i next[k][next[k][i]] [k] next k next[k][i] [k] next k i for i in range(N):
    score[0][i] = C[P[i]] i range(N) range N score[0][i] = C[P[i]] score[0][i] [0] score 0 i C[P[i]] C P[i] P i for k in range(31):
    for i in range(N):
        score[k + 1][i] = score[k][i] + score[k][next[k][i]]


# これじゃ道中がわからない...
# 一点しか求まらない k range(31) range 31 for i in range(N):
        score[k + 1][i] = score[k][i] + score[k][next[k][i]]


# これじゃ道中がわからない...
# 一点しか求まらない i range(N) range N score[k + 1][i] = score[k][i] + score[k][next[k][i]] score[k + 1][i] [k + 1] score k + 1 k 1 i score[k][i] + score[k][next[k][i]] score[k][i] [k] score k i score[k][next[k][i]] [k] score k next[k][i] [k] next k i def get_score(i, k):  # 途中の1点のscoreを算出
    now = i
    ret = 0
    for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
    return ret, now


# 各ノードからK回移動したときのscoreを計算する get_score now = i now i ret = 0 ret 0 for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
     j range(k.bit_length()) range k.bit_length() k.bit_length k bit_length if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
     (k >> j) & 1 k >> j k j 1 ret += score[j][now] ret score[j][now] [j] score j now now = next[j][now] now next[j][now] [j] next j now return ret, now return ret, now ret now i i k k ans = -10**10 ans -10**10 -10 10 for i in range(N):
    # print(i, 'について')
    if K <= N:
        # 素直に最初からK回シミュレーションする
        tmp = 0
        now = i
        for _ in range(K):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)
    else:
        # 最初からN回シミュレーション + K-NからN回シミュレーション
        tmp = 0
        now = i
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)

        tmp, now = get_score(i, K - N)
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp) i range(N) range N if K <= N:
        # 素直に最初からK回シミュレーションする
        tmp = 0
        now = i
        for _ in range(K):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)
    else:
        # 最初からN回シミュレーション + K-NからN回シミュレーション
        tmp = 0
        now = i
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)

        tmp, now = get_score(i, K - N)
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp) K <= N K N tmp = 0 tmp 0 now = i now i for _ in range(K):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)
     _ range(K) range K tmp += score[0][now] tmp score[0][now] [0] score 0 now now = next[0][now] now next[0][now] [0] next 0 now ans = max(tmp, ans) ans max(tmp, ans) max tmp ans tmp = 0 tmp 0 now = i now i for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)

         j range(N) range N tmp += score[0][now] tmp score[0][now] [0] score 0 now now = next[0][now] now next[0][now] [0] next 0 now ans = max(tmp, ans) ans max(tmp, ans) max tmp ans tmp, now = get_score(i, K - N) tmp get_score(i, K - N) get_score i K - N K N now for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp) j range(N) range N tmp += score[0][now] tmp score[0][now] [0] score 0 now now = next[0][now] now next[0][now] [0] next 0 now ans = max(tmp, ans) ans max(tmp, ans) max tmp ans print(ans) print ans read = sys.stdin.readline sys.stdin.readline read N, K = ints() ints() N K = ints() ints() K def get_score(i, k):  # 途中の1点のscoreを算出
    now = i
    ret = 0
    for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
    return ret, now


# 各ノードからK回移動したときのscoreを計算する def get_score(i, k):  # 途中の1点のscoreを算出
    now = i
    ret = 0
    for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
    return ret, now


# 各ノードからK回移動したときのscoreを計算する get_score ans = max(tmp, ans) max(tmp, ans) ans now = get_score(i, K - N) get_score(i, K - N) now now = next[0][now] next[0][now] now def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため read_matrix now = next[0][now] next[0][now] now ans = max(tmp, ans) max(tmp, ans) ans now = i i now tmp += score[0][now] score[0][now] tmp def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと mina tmp, now = get_score(i, K - N) get_score(i, K - N) tmp ans = -10**10 -10**10 ans def a_int(): return int(readline()) def a_int(): return int(readline()) a_int now = i i now tmp += score[0][now] score[0][now] tmp def ints(): return list(map(int, readline().split())) def ints(): return list(map(int, readline().split())) ints ra = range range ra enu = enumerate enumerate enu def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret))) def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret))) read_col next = [[-1] * N for _ in range(32)] [[-1] * N for _ in range(32)] next P = mina(*ints()) mina(*ints()) P tmp = 0 0 tmp ans = max(tmp, ans) max(tmp, ans) ans MOD = 10**9 + 7 10**9 + 7 MOD def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b lcm C = ints() ints() C readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit() def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit() exit def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret read_tuple tmp = 0 0 tmp score = [[0] * N for _ in range(32)] [[0] * N for _ in range(32)] score now = next[0][now] next[0][now] now INF = 2**31 2**31 INF tmp += score[0][now] score[0][now] tmp
# https://atcoder.jp/contests/abc175/tasks/abc175_d
# ダブリングで解く
# K>ループ長の場合、累積の最後の値が負ならmaxを取ればいいし、正ならば 累積の最後の値*周数をかけてから計算
import sys
sys.setrecursionlimit(1 << 25)
readline = sys.stdin.buffer.readline
read = sys.stdin.readline  # 文字列読み込む時はこっち
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(readline())


def ints(): return list(map(int, readline().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, readline().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, readline().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations, accumulate
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b


N, K = ints()
P = mina(*ints())
C = ints()

next = [[-1] * N for _ in range(32)]  # next[k][i] ノードiから2^k回移動するときのノード
score = [[0] * N for _ in range(32)]  # score[k][i] ノードiから2^k回移動するときに加算されるスコア
# nextの構築
next[0] = P
for k in range(31):
    for i in range(N):
        next[k + 1][i] = next[k][next[k][i]]
# scoreの構築
for i in range(N):
    score[0][i] = C[P[i]]
for k in range(31):
    for i in range(N):
        score[k + 1][i] = score[k][i] + score[k][next[k][i]]


# これじゃ道中がわからない...
# 一点しか求まらない
def get_score(i, k):  # 途中の1点のscoreを算出
    now = i
    ret = 0
    for j in range(k.bit_length()):
        if (k >> j) & 1:
            ret += score[j][now]
            now = next[j][now]
    return ret, now


# 各ノードからK回移動したときのscoreを計算する
ans = -10**10
for i in range(N):
    # print(i, 'について')
    if K <= N:
        # 素直に最初からK回シミュレーションする
        tmp = 0
        now = i
        for _ in range(K):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)
    else:
        # 最初からN回シミュレーション + K-NからN回シミュレーション
        tmp = 0
        now = i
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)

        tmp, now = get_score(i, K - N)
        for j in range(N):
            tmp += score[0][now]
            now = next[0][now]
            ans = max(tmp, ans)
            # print(tmp)


print(ans)
