#ABC175-D
"""
1周未満で終わる時の最大値をまずもとめる
(i)k<一周なら
最大値を出力して終了
(ii)sum(lst) <= 0
1周以上する価値はないので、最大値を出力して終了
(iii)sum(lst) > 0
N%kの端数だけ愚直にもう一度最大値を求めて出力
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())
p = list(map(int,readline().split()))
c = list(map(int,readline().split()))
p = [i-1 for i in p]

ans = -10**18

for i in range(n):
    lst1 = [c[i]]
    idx = p[i]
    while idx != i:
        lst1.append(c[idx])
        idx = p[idx]
    sm = sum(lst1)
    acc = 0
    for i in range(min(len(lst1),k+2)):
        acc += lst1[i]
        a = acc
        if sm > 0:
            a += sm*((k-(i+1))//len(lst1))
        ans = max(a,ans)
print(ans) """
1周未満で終わる時の最大値をまずもとめる
(i)k<一周なら
最大値を出力して終了
(ii)sum(lst) <= 0
1周以上する価値はないので、最大値を出力して終了
(iii)sum(lst) > 0
N%kの端数だけ愚直にもう一度最大値を求めて出力
""" import sys sys readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline def even(n): return 1 if n%2==0 else 0 even n n n,k = map(int,readline().split()) n map(int,readline().split()) map int readline().split() ().split () readline split k p = list(map(int,readline().split())) p list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split c = list(map(int,readline().split())) c list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split i-1 for i in p for i in p i p for i in p i-1 i 1 p = [i-1 for i in p] p [i-1 for i in p] ans = -10**18 ans -10**18 -10 18 for i in range(n):
    lst1 = [c[i]]
    idx = p[i]
    while idx != i:
        lst1.append(c[idx])
        idx = p[idx]
    sm = sum(lst1)
    acc = 0
    for i in range(min(len(lst1),k+2)):
        acc += lst1[i]
        a = acc
        if sm > 0:
            a += sm*((k-(i+1))//len(lst1))
        ans = max(a,ans) i range(n) range n lst1 = [c[i]] lst1 [c[i]] c[i] c i idx = p[i] idx p[i] p i while idx != i:
        lst1.append(c[idx])
        idx = p[idx]
     idx != i idx i lst1.append(c[idx]) lst1.append lst1 append c[idx] c idx idx = p[idx] idx p[idx] p idx sm = sum(lst1) sm sum(lst1) sum lst1 acc = 0 acc 0 for i in range(min(len(lst1),k+2)):
        acc += lst1[i]
        a = acc
        if sm > 0:
            a += sm*((k-(i+1))//len(lst1))
        ans = max(a,ans) i range(min(len(lst1),k+2)) range min(len(lst1),k+2) min len(lst1) len lst1 k+2 k 2 acc += lst1[i] acc lst1[i] lst1 i a = acc a acc if sm > 0:
            a += sm*((k-(i+1))//len(lst1))
         sm > 0 sm 0 a += sm*((k-(i+1))//len(lst1)) a sm*((k-(i+1))//len(lst1)) sm (k-(i+1))//len(lst1) k-(i+1) k i+1 i 1 len(lst1) len lst1 ans = max(a,ans) ans max(a,ans) max a ans print(ans) print ans k = map(int,readline().split()) map(int,readline().split()) k def even(n): return 1 if n%2==0 else 0 def even(n): return 1 if n%2==0 else 0 even ans = max(a,ans) max(a,ans) ans idx = p[i] p[i] idx p = list(map(int,readline().split())) list(map(int,readline().split())) p p = [i-1 for i in p] [i-1 for i in p] p lst1 = [c[i]] [c[i]] lst1 c = list(map(int,readline().split())) list(map(int,readline().split())) c readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline a = acc acc a sm = sum(lst1) sum(lst1) sm n,k = map(int,readline().split()) map(int,readline().split()) n acc = 0 0 acc acc += lst1[i] lst1[i] acc ans = -10**18 -10**18 ans idx = p[idx] p[idx] idx a += sm*((k-(i+1))//len(lst1)) sm*((k-(i+1))//len(lst1)) a
#ABC175-D
"""
1周未満で終わる時の最大値をまずもとめる
(i)k<一周なら
最大値を出力して終了
(ii)sum(lst) <= 0
1周以上する価値はないので、最大値を出力して終了
(iii)sum(lst) > 0
N%kの端数だけ愚直にもう一度最大値を求めて出力
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())
p = list(map(int,readline().split()))
c = list(map(int,readline().split()))
p = [i-1 for i in p]

ans = -10**18

for i in range(n):
    lst1 = [c[i]]
    idx = p[i]
    while idx != i:
        lst1.append(c[idx])
        idx = p[idx]
    sm = sum(lst1)
    acc = 0
    for i in range(min(len(lst1),k+2)):
        acc += lst1[i]
        a = acc
        if sm > 0:
            a += sm*((k-(i+1))//len(lst1))
        ans = max(a,ans)
print(ans)
