import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,K=mi()
    P=list(mi())
    C=list(mi())

    ans = -INF
    for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
    print(ans)





if __name__ == "__main__":
    main() import sys sys sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 def mi(): return map(int,input().split()) mi def ii(): return int(input()) ii def isp(): return input().split() isp def deb(text): print("-------\n{}\n-------".format(text)) deb text text INF=10**20 INF 10**20 10 20 def main():
    N,K=mi()
    P=list(mi())
    C=list(mi())

    ans = -INF
    for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
    print(ans) main N,K=mi() N mi() mi K P=list(mi()) P list(mi()) list mi() mi C=list(mi()) C list(mi()) list mi() mi ans = -INF ans -INF INF for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
     i range(N) range N start = i + 1 start i + 1 i 1 stack = [start] stack [start] start score_his = [0] score_his [0] 0 scores = 0 scores 0 depth = 1 depth 1 seen = {} seen {} while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
         stack if depth > K: break
             depth > K depth K current = stack.pop() current stack.pop() stack.pop stack pop if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
             current in seen current seen loop_start_depth = seen[current] loop_start_depth seen[current] seen current loop_end_depth = depth - 1 loop_end_depth depth - 1 depth 1 loop_scores = score_his[-1] - score_his[loop_start_depth-1] loop_scores score_his[-1] - score_his[loop_start_depth-1] score_his[-1] score_his -1 score_his[loop_start_depth-1] score_his loop_start_depth-1 loop_start_depth 1 loop_len = loop_end_depth - loop_start_depth + 1 loop_len loop_end_depth - loop_start_depth + 1 loop_end_depth - loop_start_depth loop_end_depth loop_start_depth 1 if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                 loop_scores < 0 loop_scores 0 rest_count = K - depth + 1 rest_count K - depth + 1 K - depth K depth 1 available_loop_count = rest_count // loop_len available_loop_count rest_count // loop_len rest_count loop_len res1 = (available_loop_count-1) * loop_scores + scores res1 (available_loop_count-1) * loop_scores + scores (available_loop_count-1) * loop_scores available_loop_count-1 available_loop_count 1 loop_scores scores res2 = res1 res2 res1 rest_loop_len = rest_count % loop_len + loop_len rest_loop_len rest_count % loop_len + loop_len rest_count % loop_len rest_count loop_len loop_len ress = [res1,res2] ress [res1,res2] res1 res2 for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                 j range(rest_loop_len) range rest_loop_len score = C[P[current-1]-1] score C[P[current-1]-1] C P[current-1]-1 P[current-1] P current-1 current 1 1 res2 += score res2 score ress.append(res2) ress.append ress append res2 current = P[current-1] current P[current-1] P current-1 current 1 score_his.append(max(ress)) score_his.append score_his append max(ress) max ress break score = C[P[current-1]-1] score C[P[current-1]-1] C P[current-1]-1 P[current-1] P current-1 current 1 1 scores += score scores score score_his.append(scores) score_his.append score_his append scores seen[current] = depth seen[current] seen current depth stack.append(P[current-1]) stack.append stack append P[current-1] P current-1 current 1 depth += 1 depth 1 ans = max(ans,max(score_his[1:])) ans max(ans,max(score_his[1:])) max ans max(score_his[1:]) max score_his[1:] score_his 1: 1 print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def isp(): return input().split() def isp(): return input().split() isp INF=10**20 10**20 INF def main():
    N,K=mi()
    P=list(mi())
    C=list(mi())

    ans = -INF
    for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
    print(ans) def main():
    N,K=mi()
    P=list(mi())
    C=list(mi())

    ans = -INF
    for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
    print(ans) main def deb(text): print("-------\n{}\n-------".format(text)) def deb(text): print("-------\n{}\n-------".format(text)) deb def mi(): return map(int,input().split()) def mi(): return map(int,input().split()) mi def ii(): return int(input()) def ii(): return int(input()) ii
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,K=mi()
    P=list(mi())
    C=list(mi())

    ans = -INF
    for i in range(N):
        start = i + 1 
        stack = [start]
        score_his = [0]
        scores = 0
        depth = 1
        seen = {}
        while stack:
            if depth > K: break
            current = stack.pop()
            
            if current in seen:
                loop_start_depth = seen[current]
                loop_end_depth = depth - 1 
                loop_scores = score_his[-1] - score_his[loop_start_depth-1]
                loop_len = loop_end_depth - loop_start_depth + 1
                if loop_scores < 0: break

                # print(start,score_his)
                # print(loop_start_depth,loop_end_depth,loop_scores,loop_len)
                
                rest_count = K - depth + 1
                available_loop_count = rest_count // loop_len
                res1 = (available_loop_count-1) * loop_scores + scores

                # print("a",rest_count,available_loop_count,res1)
                
                res2 = res1
                rest_loop_len = rest_count % loop_len + loop_len

                # print("b",rest_loop_len)
                ress = [res1,res2]
                for j in range(rest_loop_len):
                    score = C[P[current-1]-1]
                    res2 += score
                    ress.append(res2)
                    current = P[current-1]

                # print("ress",ress)
                score_his.append(max(ress))

                break

            
            score = C[P[current-1]-1]
            scores += score
            score_his.append(scores)

            seen[current] = depth
            stack.append(P[current-1])

            depth += 1
        
        ans = max(ans,max(score_his[1:]))
    
    print(ans)





if __name__ == "__main__":
    main()