import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def end_of_loop():raise StopIteration

def main():
    N, K = MI()
    P = LI()
    C = LI()
    #1週下再に加算されるスコア並びにあるますから出発した際に１マス進んだ際のスコアがわかればよい。
    #初期化
    ans = []
    result = -float('inf')
    #開始点
    for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
    print(result)

if __name__ == "__main__":
    main() import sys sys import math math from collections import defaultdict, deque from copy import deepcopy input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def RD(): return input().rstrip() RD def F(): return float(input().rstrip()) F def I(): return int(input().rstrip()) I def MI(): return map(int, input().split()) MI def MF(): return map(float,input().split()) MF def LI(): return list(map(int, input().split())) LI def LF(): return list(map(float,input().split())) LF [num for i in range(W)] for j in range(H) for j in range(H) j range(H) range H for j in range(H) [num for i in range(W)] def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] Init H H W W num num def end_of_loop():raise StopIteration end_of_loop def main():
    N, K = MI()
    P = LI()
    C = LI()
    #1週下再に加算されるスコア並びにあるますから出発した際に１マス進んだ際のスコアがわかればよい。
    #初期化
    ans = []
    result = -float('inf')
    #開始点
    for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
    print(result) main N, K = MI() N MI() MI K P = LI() P LI() LI C = LI() C LI() LI ans = [] ans [] result = -float('inf') result -float('inf') float('inf') float 'inf' for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
     i range(N) range N result2= 0 result2 0 temp = i temp i d = [-float('inf')] * N d [-float('inf')] * N [-float('inf')] -float('inf') float('inf') float 'inf' N for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
     j range(N) range N temp = P[temp]-1 temp P[temp]-1 P[temp] P temp 1 result2+=C[temp] result2 C[temp] C temp d[j] = result2 d[j] d j result2 if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
     temp == i temp i index = j+1 index j+1 j 1 score = result2 score result2 if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
     index < K index K amari = K % index amari K % index K index syou = K // index syou K // index K index if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                     amari !=0 amari 0 result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1])) result max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1])) max result syou*score syou score syou*score+max(d[0:amari]) syou*score syou score max(d[0:amari]) max d[0:amari] d 0:amari 0 amari (syou-1)*score+max(d[0:index-1]) (syou-1)*score syou-1 syou 1 score max(d[0:index-1]) max d[0:index-1] d 0:index-1 0 index-1 index 1 max(d) max d (syou-1)*score+max(d[0:index-1]) (syou-1)*score syou-1 syou 1 score max(d[0:index-1]) max d[0:index-1] d 0:index-1 0 index-1 index 1 result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1])) result max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1])) max result syou*score syou score max(d) max d (syou-1)*score+max(d[0:index-1]) (syou-1)*score syou-1 syou 1 score max(d[0:index-1]) max d[0:index-1] d 0:index-1 0 index-1 index 1 break result = max(result, max(d[0:K])) result max(result, max(d[0:K])) max result max(d[0:K]) max d[0:K] d 0:K 0 K break print(result) print result if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI def MI(): return map(int, input().split()) def MI(): return map(int, input().split()) MI def MF(): return map(float,input().split()) def MF(): return map(float,input().split()) MF def end_of_loop():raise StopIteration def end_of_loop():raise StopIteration end_of_loop input = sys.stdin.readline sys.stdin.readline input def I(): return int(input().rstrip()) def I(): return int(input().rstrip()) I def RD(): return input().rstrip() def RD(): return input().rstrip() RD def main():
    N, K = MI()
    P = LI()
    C = LI()
    #1週下再に加算されるスコア並びにあるますから出発した際に１マス進んだ際のスコアがわかればよい。
    #初期化
    ans = []
    result = -float('inf')
    #開始点
    for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
    print(result) def main():
    N, K = MI()
    P = LI()
    C = LI()
    #1週下再に加算されるスコア並びにあるますから出発した際に１マス進んだ際のスコアがわかればよい。
    #初期化
    ans = []
    result = -float('inf')
    #開始点
    for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
    print(result) main def LF(): return list(map(float,input().split())) def LF(): return list(map(float,input().split())) LF def F(): return float(input().rstrip()) def F(): return float(input().rstrip()) F def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] Init
import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def end_of_loop():raise StopIteration

def main():
    N, K = MI()
    P = LI()
    C = LI()
    #1週下再に加算されるスコア並びにあるますから出発した際に１マス進んだ際のスコアがわかればよい。
    #初期化
    ans = []
    result = -float('inf')
    #開始点
    for i in range(N):
        result2= 0
        # start
        temp = i
        d = [-float('inf')] * N
        for j in range(N):
            temp = P[temp]-1
            result2+=C[temp]
            d[j] = result2
            if temp == i:
                index = j+1
                score = result2
                if index < K:
                    amari = K % index
                    syou = K // index
                    if amari !=0:
                        result = max(result, syou*score, syou*score+max(d[0:amari]), (syou-1)*score+max(d[0:index-1]), max(d), (syou-1)*score+max(d[0:index-1]))
                    else:
                        result = max(result, syou*score, max(d), (syou-1)*score+max(d[0:index-1]))
                    break
                else:
                    result = max(result, max(d[0:K]))
                    break
    print(result)

if __name__ == "__main__":
    main()