import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
P = [0] + inpl()
C = [0] + inpl()

ans = 1000000010 * -1
for start in range(1,n+1):
    l = [0] * (n+1)
    l[start] = 1
    now = start
    score = []
    while True:
        l[now] = 1
        score.append(C[P[now]])
        if l[P[now]] == 1:
            break
        now = P[now]
    
    ruiseki = []
    for i in range(len(score)):
        if i == 0:
            ruiseki.append(score[i])
        else:
            ruiseki.append(ruiseki[i-1] + score[i])

    a = ruiseki[-1] #1ループでのスコア
    b = max(ruiseki) #1ループ中の最大スコア
    c = k // len(ruiseki) #ループ可能回数
    d = k % len(ruiseki) #ループ抜けてからのあまり回数

    tmp = 0
    if c == 0:
        tmp = max(ruiseki[:d])
    elif c > 0:
        if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
    ans = max(ans,tmp)

print(ans) import sys, bisect, math, itertools, string, queue, copy sys bisect math itertools string queue copy def inp(): return int(input()) inp def inpm(): return map(int,input().split()) inpm def inpl(): return list(map(int, input().split())) inpl def inpls(): return list(input().split()) inpls def inplm(n): return list(int(input()) for _ in range(n)) inplm n n list(input()) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(input()) list input() input def inplL(n): return [list(input()) for _ in range(n)] inplL n n tuple(input()) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) tuple(input()) tuple input() input def inplT(n): return [tuple(input()) for _ in range(n)] inplT n n list(map(int, input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split def inpll(n): return [list(map(int, input().split())) for _ in range(n)] inpll n n list(map(int, input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) inplls n n n,k = inpm() n inpm() inpm k P = [0] + inpl() P [0] + inpl() [0] 0 inpl() inpl C = [0] + inpl() C [0] + inpl() [0] 0 inpl() inpl ans = 1000000010 * -1 ans 1000000010 * -1 1000000010 -1 for start in range(1,n+1):
    l = [0] * (n+1)
    l[start] = 1
    now = start
    score = []
    while True:
        l[now] = 1
        score.append(C[P[now]])
        if l[P[now]] == 1:
            break
        now = P[now]
    
    ruiseki = []
    for i in range(len(score)):
        if i == 0:
            ruiseki.append(score[i])
        else:
            ruiseki.append(ruiseki[i-1] + score[i])

    a = ruiseki[-1] #1ループでのスコア
    b = max(ruiseki) #1ループ中の最大スコア
    c = k // len(ruiseki) #ループ可能回数
    d = k % len(ruiseki) #ループ抜けてからのあまり回数

    tmp = 0
    if c == 0:
        tmp = max(ruiseki[:d])
    elif c > 0:
        if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
    ans = max(ans,tmp) start range(1,n+1) range 1 n+1 n 1 l = [0] * (n+1) l [0] * (n+1) [0] 0 n+1 n 1 l[start] = 1 l[start] l start 1 now = start now start score = [] score [] while True:
        l[now] = 1
        score.append(C[P[now]])
        if l[P[now]] == 1:
            break
        now = P[now]
    
     True l[now] = 1 l[now] l now 1 score.append(C[P[now]]) score.append score append C[P[now]] C P[now] P now if l[P[now]] == 1:
            break
         l[P[now]] == 1 l[P[now]] l P[now] P now 1 break now = P[now] now P[now] P now ruiseki = [] ruiseki [] for i in range(len(score)):
        if i == 0:
            ruiseki.append(score[i])
        else:
            ruiseki.append(ruiseki[i-1] + score[i])

     i range(len(score)) range len(score) len score if i == 0:
            ruiseki.append(score[i])
        else:
            ruiseki.append(ruiseki[i-1] + score[i])

     i == 0 i 0 ruiseki.append(score[i]) ruiseki.append ruiseki append score[i] score i ruiseki.append(ruiseki[i-1] + score[i]) ruiseki.append ruiseki append ruiseki[i-1] + score[i] ruiseki[i-1] ruiseki i-1 i 1 score[i] score i a = ruiseki[-1] a ruiseki[-1] ruiseki -1 b = max(ruiseki) b max(ruiseki) max ruiseki c = k // len(ruiseki) c k // len(ruiseki) k len(ruiseki) len ruiseki d = k % len(ruiseki) d k % len(ruiseki) k len(ruiseki) len ruiseki tmp = 0 tmp 0 if c == 0:
        tmp = max(ruiseki[:d])
    elif c > 0:
        if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
     c == 0 c 0 tmp = max(ruiseki[:d]) tmp max(ruiseki[:d]) max ruiseki[:d] ruiseki :d d elif c > 0:
        if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
     c > 0 c 0 if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
     a <= 0 a 0 tmp = b tmp b elif d == 0:
            tmp = a*(c-1) + b
         d == 0 d 0 tmp = a*(c-1) + b tmp a*(c-1) + b a*(c-1) a c-1 c 1 b tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b) tmp max(a*c + max(ruiseki[:d]),a*(c-1) + b) max a*c + max(ruiseki[:d]) a*c a c max(ruiseki[:d]) max ruiseki[:d] ruiseki :d d a*(c-1) + b a*(c-1) a c-1 c 1 b ans = max(ans,tmp) ans max(ans,tmp) max ans tmp print(ans) print ans tmp = b b tmp n,k = inpm() inpm() n tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b) max(a*c + max(ruiseki[:d]),a*(c-1) + b) tmp c = k // len(ruiseki) k // len(ruiseki) c def inplL(n): return [list(input()) for _ in range(n)] def inplL(n): return [list(input()) for _ in range(n)] inplL ans = 1000000010 * -1 1000000010 * -1 ans def inpls(): return list(input().split()) def inpls(): return list(input().split()) inpls C = [0] + inpl() [0] + inpl() C ans = max(ans,tmp) max(ans,tmp) ans d = k % len(ruiseki) k % len(ruiseki) d k = inpm() inpm() k tmp = a*(c-1) + b a*(c-1) + b tmp def inpl(): return list(map(int, input().split())) def inpl(): return list(map(int, input().split())) inpl b = max(ruiseki) max(ruiseki) b score = [] [] score a = ruiseki[-1] ruiseki[-1] a def inp(): return int(input()) def inp(): return int(input()) inp tmp = max(ruiseki[:d]) max(ruiseki[:d]) tmp def inplm(n): return list(int(input()) for _ in range(n)) def inplm(n): return list(int(input()) for _ in range(n)) inplm now = start start now def inplT(n): return [tuple(input()) for _ in range(n)] def inplT(n): return [tuple(input()) for _ in range(n)] inplT def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) inplls ruiseki = [] [] ruiseki def inpll(n): return [list(map(int, input().split())) for _ in range(n)] def inpll(n): return [list(map(int, input().split())) for _ in range(n)] inpll l = [0] * (n+1) [0] * (n+1) l now = P[now] P[now] now def inpm(): return map(int,input().split()) def inpm(): return map(int,input().split()) inpm tmp = 0 0 tmp P = [0] + inpl() [0] + inpl() P
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,k = inpm()
P = [0] + inpl()
C = [0] + inpl()

ans = 1000000010 * -1
for start in range(1,n+1):
    l = [0] * (n+1)
    l[start] = 1
    now = start
    score = []
    while True:
        l[now] = 1
        score.append(C[P[now]])
        if l[P[now]] == 1:
            break
        now = P[now]
    
    ruiseki = []
    for i in range(len(score)):
        if i == 0:
            ruiseki.append(score[i])
        else:
            ruiseki.append(ruiseki[i-1] + score[i])

    a = ruiseki[-1] #1ループでのスコア
    b = max(ruiseki) #1ループ中の最大スコア
    c = k // len(ruiseki) #ループ可能回数
    d = k % len(ruiseki) #ループ抜けてからのあまり回数

    tmp = 0
    if c == 0:
        tmp = max(ruiseki[:d])
    elif c > 0:
        if a <= 0:
            tmp = b
        elif d == 0:
            tmp = a*(c-1) + b
        else:
            tmp = max(a*c + max(ruiseki[:d]),a*(c-1) + b)
    ans = max(ans,tmp)

print(ans)