import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = 'ONLINE_JUDGE' not in sys.argv

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    N, K = mis()
    P = list(map(lambda x: x-1, mis()))
    C = lmis()
    ans = -INF
    for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
    print(ans)








main()
 import sys sys sys.setrecursionlimit(1000000000) sys.setrecursionlimit sys setrecursionlimit 1000000000 import math math from math import gcd def lcm(a, b): return a * b // gcd(a, b) lcm a a b b from itertools import count, permutations, combinations, chain, product from functools import lru_cache from collections import deque, defaultdict from operator import itemgetter from pprint import pprint ii = lambda: int(input()) ii lambda: int(input()) int(input()) int input() input mis = lambda: map(int, input().split()) mis lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split lmis = lambda: list(mis()) lmis lambda: list(mis()) list(mis()) list mis() mis INF = float('inf') INF float('inf') float 'inf' N1097 = 10**9 + 7 N1097 10**9 + 7 10**9 10 9 7 DEBUG = 'ONLINE_JUDGE' not in sys.argv DEBUG 'ONLINE_JUDGE' not in sys.argv 'ONLINE_JUDGE' sys.argv sys argv def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok meg while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
     abs(ok-ng)>1 abs(ok-ng) abs ok-ng ok ng 1 mid = (ok+ng)//2 mid (ok+ng)//2 ok+ng ok ng 2 if f(mid):
            ok=mid
        else:
            ng=mid
     f(mid) f mid ok=mid ok mid ng=mid ng mid return ok ok f f ok ok ng ng def get_inv(n, modp):
    return pow(n, modp-2, modp) get_inv return pow(n, modp-2, modp) pow(n, modp-2, modp) pow n modp-2 modp 2 modp n n modp modp def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs factorials_list fs = [1] fs [1] 1 for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
     i range(1, n+1) range 1 n+1 n 1 fs.append(fs[-1] * i % modp) fs.append fs append fs[-1] * i % modp fs[-1] * i fs[-1] fs -1 i modp return fs fs n n modp modp def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs invs_list invs = [get_inv(fs[-1], modp)] invs [get_inv(fs[-1], modp)] get_inv(fs[-1], modp) get_inv fs[-1] fs -1 modp for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
     i range(n, 1-1, -1) range n 1-1 1 1 -1 invs.append(invs[-1] * i % modp) invs.append invs append invs[-1] * i % modp invs[-1] * i invs[-1] invs -1 i modp invs.reverse() invs.reverse invs reverse return invs invs n n fs fs modp modp def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp comb num = 1 num 1 for i in range(n, n-k, -1):
        num = num * i % modp
     i range(n, n-k, -1) range n n-k n k -1 num = num * i % modp num num * i % modp num * i num i modp den = 1 den 1 for i in range(2, k+1):
        den = den * i % modp
     i range(2, k+1) range 2 k+1 k 1 den = den * i % modp den den * i % modp den * i den i modp return num * get_inv(den, modp) % modp num * get_inv(den, modp) % modp num * get_inv(den, modp) num get_inv(den, modp) get_inv den modp modp n n k k modp modp def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

# comb_from_list return fs[n] * invs[n-k] * invs[k] % modp fs[n] * invs[n-k] * invs[k] % modp fs[n] * invs[n-k] * invs[k] fs[n] * invs[n-k] fs[n] fs n invs[n-k] invs n-k n k invs[k] invs k modp n n k k modp modp fs fs invs invs class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True UnionFindEx def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
     __init__ self.roots = [-1] * size self.roots self roots [-1] * size [-1] -1 size self self size size def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
     getRootID r = self.roots[i] r self.roots[i] self.roots self roots i if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
     r < 0 r 0 return i i r = self.getRootID(r) r self.getRootID(r) self.getRootID self getRootID r self.roots[i] = r self.roots[i] self.roots self roots i r return r r self self i i def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
     getGroupSize return -self.roots[self.getRootID(i)] -self.roots[self.getRootID(i)] self.roots[self.getRootID(i)] self.roots self roots self.getRootID(i) self.getRootID self getRootID i self self i i def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True connect r1, r2 = self.getRootID(i), self.getRootID(j) r1 self.getRootID(i) self.getRootID self getRootID i r2 self.getRootID(j) self.getRootID self getRootID j if r1 == r2:
            return False
         r1 == r2 r1 r2 return False False if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
         self.getGroupSize(r1) < self.getGroupSize(r2) self.getGroupSize(r1) self.getGroupSize self getGroupSize r1 self.getGroupSize(r2) self.getGroupSize self getGroupSize r2 r1, r2 = r2, r1 r1 r2 r2 r1 self.roots[r1] += self.roots[r2] self.roots[r1] self.roots self roots r1 self.roots[r2] self.roots self roots r2 self.roots[r2] = r1 self.roots[r2] self.roots self roots r2 r1 return True True self self i i j j Yes = 'Yes' Yes 'Yes' No = 'No' No 'No' def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs) dprint if DEBUG:
        print(*args, **kwargs) DEBUG print(*args, **kwargs) print *args args **kwargs kwargs *args **kwargs def main():
    N, K = mis()
    P = list(map(lambda x: x-1, mis()))
    C = lmis()
    ans = -INF
    for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
    print(ans) main N, K = mis() N mis() mis K P = list(map(lambda x: x-1, mis())) P list(map(lambda x: x-1, mis())) list map(lambda x: x-1, mis()) map lambda x: x-1 x-1 x 1 x mis() mis C = lmis() C lmis() lmis ans = -INF ans -INF INF for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
     i range(N) range N p = i p i tmp_score = 0 tmp_score 0 k = K k K cycle = 0 cycle 0 while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
         k p = P[p] p P[p] P p tmp_score += C[p] tmp_score C[p] C p ans = max(ans, tmp_score) ans max(ans, tmp_score) max ans tmp_score k -= 1 k 1 cycle += 1 cycle 1 if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
         p==i p i if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
         tmp_score < 0 tmp_score 0 break elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
         k > cycle*2 k cycle*2 cycle 2 skip_loop = (k - cycle)//cycle skip_loop (k - cycle)//cycle k - cycle k cycle cycle tmp_score *= skip_loop + 1 tmp_score skip_loop + 1 skip_loop 1 k -= skip_loop * cycle k skip_loop * cycle skip_loop cycle ans = max(ans, tmp_score) ans max(ans, tmp_score) max ans tmp_score print(ans) print ans main() main def lcm(a, b): return a * b // gcd(a, b) def lcm(a, b): return a * b // gcd(a, b) lcm No = 'No' 'No' No def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs) def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs) dprint def main():
    N, K = mis()
    P = list(map(lambda x: x-1, mis()))
    C = lmis()
    ans = -INF
    for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
    print(ans) def main():
    N, K = mis()
    P = list(map(lambda x: x-1, mis()))
    C = lmis()
    ans = -INF
    for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
    print(ans) main INF = float('inf') float('inf') INF class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True UnionFindEx def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs factorials_list lmis = lambda: list(mis()) lambda: list(mis()) lmis def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok meg N1097 = 10**9 + 7 10**9 + 7 N1097 ii = lambda: int(input()) lambda: int(input()) ii def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs invs_list DEBUG = 'ONLINE_JUDGE' not in sys.argv 'ONLINE_JUDGE' not in sys.argv DEBUG def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp comb mis = lambda: map(int, input().split()) lambda: map(int, input().split()) mis def get_inv(n, modp):
    return pow(n, modp-2, modp) def get_inv(n, modp):
    return pow(n, modp-2, modp) get_inv Yes = 'Yes' 'Yes' Yes def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

# def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

# comb_from_list
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, combinations, chain, product
from functools import lru_cache
from collections import deque, defaultdict
from operator import itemgetter
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
DEBUG = 'ONLINE_JUDGE' not in sys.argv

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'

def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

def main():
    N, K = mis()
    P = list(map(lambda x: x-1, mis()))
    C = lmis()
    ans = -INF
    for i in range(N):
        p = i
        tmp_score = 0
        k = K
        cycle = 0
        while k:
            p = P[p]
            tmp_score += C[p]
            ans = max(ans, tmp_score)
            k -= 1
            cycle += 1
            if p==i:
                if tmp_score < 0:
                    break
                elif k > cycle*2:
                    skip_loop = (k - cycle)//cycle
                    tmp_score *= skip_loop + 1
                    k -= skip_loop * cycle
        ans = max(ans, tmp_score)
    print(ans)








main()

