import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
P = LIST()
C = LIST()

loop_point = [0]*N
loop_count = [0]*N
searched = [0]*N

for i in range(N):
	if searched[i]:
		continue
	idx = i
	points = 0
	cnt = 0
	while not searched[P[idx]-1]:
		idx = P[idx]-1
		searched[idx] = 1
		cnt += 1
		points += C[idx]
	loop_count[i] = cnt
	loop_point[i] = points

	while not loop_point[P[idx]-1]:
		idx_next = P[idx]-1
		loop_count[idx_next] = loop_count[idx]
		loop_point[idx_next] = loop_point[idx]
		idx = idx_next

ans = -INF
for i in range(N):
	p = 0
	idx = i
	loop = loop_point[i] > 0 and K > loop_count[i]
	for j in range(1, min(K+1, loop_count[i]+1)):
		idx = P[idx] - 1
		p += C[idx]
		if loop:
			final_p = p + loop_point[i]*((K-j)//loop_count[i])
		else:
			final_p = p
		ans = ans if ans > final_p else final_p
print(ans) import sys, re sys re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left, insort, insort_left from heapq import heappush, heappop from functools import reduce, lru_cache def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LIST(): return list(map(int, input().split())) LIST def TUPLE(): return tuple(map(int, input().split())) TUPLE MAP() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) MAP() MAP def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP n n sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float('inf') INF float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 from decimal import * N, K = MAP() N MAP() MAP K P = LIST() P LIST() LIST C = LIST() C LIST() LIST loop_point = [0]*N loop_point [0]*N [0] 0 N loop_count = [0]*N loop_count [0]*N [0] 0 N searched = [0]*N searched [0]*N [0] 0 N for i in range(N):
	if searched[i]:
		continue
	idx = i
	points = 0
	cnt = 0
	while not searched[P[idx]-1]:
		idx = P[idx]-1
		searched[idx] = 1
		cnt += 1
		points += C[idx]
	loop_count[i] = cnt
	loop_point[i] = points

	while not loop_point[P[idx]-1]:
		idx_next = P[idx]-1
		loop_count[idx_next] = loop_count[idx]
		loop_point[idx_next] = loop_point[idx]
		idx = idx_next i range(N) range N if searched[i]:
		continue
	 searched[i] searched i continue idx = i idx i points = 0 points 0 cnt = 0 cnt 0 while not searched[P[idx]-1]:
		idx = P[idx]-1
		searched[idx] = 1
		cnt += 1
		points += C[idx]
	 not searched[P[idx]-1] searched[P[idx]-1] searched P[idx]-1 P[idx] P idx 1 idx = P[idx]-1 idx P[idx]-1 P[idx] P idx 1 searched[idx] = 1 searched[idx] searched idx 1 cnt += 1 cnt 1 points += C[idx] points C[idx] C idx loop_count[i] = cnt loop_count[i] loop_count i cnt loop_point[i] = points loop_point[i] loop_point i points while not loop_point[P[idx]-1]:
		idx_next = P[idx]-1
		loop_count[idx_next] = loop_count[idx]
		loop_point[idx_next] = loop_point[idx]
		idx = idx_next not loop_point[P[idx]-1] loop_point[P[idx]-1] loop_point P[idx]-1 P[idx] P idx 1 idx_next = P[idx]-1 idx_next P[idx]-1 P[idx] P idx 1 loop_count[idx_next] = loop_count[idx] loop_count[idx_next] loop_count idx_next loop_count[idx] loop_count idx loop_point[idx_next] = loop_point[idx] loop_point[idx_next] loop_point idx_next loop_point[idx] loop_point idx idx = idx_next idx idx_next ans = -INF ans -INF INF for i in range(N):
	p = 0
	idx = i
	loop = loop_point[i] > 0 and K > loop_count[i]
	for j in range(1, min(K+1, loop_count[i]+1)):
		idx = P[idx] - 1
		p += C[idx]
		if loop:
			final_p = p + loop_point[i]*((K-j)//loop_count[i])
		else:
			final_p = p
		ans = ans if ans > final_p else final_p i range(N) range N p = 0 p 0 idx = i idx i loop = loop_point[i] > 0 and K > loop_count[i] loop loop_point[i] > 0 and K > loop_count[i] loop_point[i] > 0 loop_point[i] loop_point i 0 K > loop_count[i] K loop_count[i] loop_count i for j in range(1, min(K+1, loop_count[i]+1)):
		idx = P[idx] - 1
		p += C[idx]
		if loop:
			final_p = p + loop_point[i]*((K-j)//loop_count[i])
		else:
			final_p = p
		ans = ans if ans > final_p else final_p j range(1, min(K+1, loop_count[i]+1)) range 1 min(K+1, loop_count[i]+1) min K+1 K 1 loop_count[i]+1 loop_count[i] loop_count i 1 idx = P[idx] - 1 idx P[idx] - 1 P[idx] P idx 1 p += C[idx] p C[idx] C idx if loop:
			final_p = p + loop_point[i]*((K-j)//loop_count[i])
		else:
			final_p = p
		 loop final_p = p + loop_point[i]*((K-j)//loop_count[i]) final_p p + loop_point[i]*((K-j)//loop_count[i]) p loop_point[i]*((K-j)//loop_count[i]) loop_point[i] loop_point i (K-j)//loop_count[i] K-j K j loop_count[i] loop_count i final_p = p final_p p ans = ans if ans > final_p else final_p ans ans if ans > final_p else final_p ans > final_p ans final_p ans final_p print(ans) print ans C = LIST() LIST() C N, K = MAP() MAP() N cnt += 1 1 cnt def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP final_p = p + loop_point[i]*((K-j)//loop_count[i]) p + loop_point[i]*((K-j)//loop_count[i]) final_p cnt = 0 0 cnt def INT(): return int(input()) def INT(): return int(input()) INT idx = i i idx points += C[idx] C[idx] points def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST idx_next = P[idx]-1 P[idx]-1 idx_next ans = ans if ans > final_p else final_p ans if ans > final_p else final_p ans idx = P[idx]-1 P[idx]-1 idx ans = -INF -INF ans points = 0 0 points loop_count = [0]*N [0]*N loop_count idx = idx_next idx_next idx INF = float('inf') float('inf') INF idx = i i idx final_p = p p final_p searched = [0]*N [0]*N searched p += C[idx] C[idx] p mod = 10 ** 9 + 7 10 ** 9 + 7 mod p = 0 0 p def ZIP(n): return zip(*(MAP() for _ in range(n))) def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP loop = loop_point[i] > 0 and K > loop_count[i] loop_point[i] > 0 and K > loop_count[i] loop P = LIST() LIST() P K = MAP() MAP() K loop_point = [0]*N [0]*N loop_point idx = P[idx] - 1 P[idx] - 1 idx def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def TUPLE(): return tuple(map(int, input().split())) def TUPLE(): return tuple(map(int, input().split())) TUPLE
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
P = LIST()
C = LIST()

loop_point = [0]*N
loop_count = [0]*N
searched = [0]*N

for i in range(N):
	if searched[i]:
		continue
	idx = i
	points = 0
	cnt = 0
	while not searched[P[idx]-1]:
		idx = P[idx]-1
		searched[idx] = 1
		cnt += 1
		points += C[idx]
	loop_count[i] = cnt
	loop_point[i] = points

	while not loop_point[P[idx]-1]:
		idx_next = P[idx]-1
		loop_count[idx_next] = loop_count[idx]
		loop_point[idx_next] = loop_point[idx]
		idx = idx_next

ans = -INF
for i in range(N):
	p = 0
	idx = i
	loop = loop_point[i] > 0 and K > loop_count[i]
	for j in range(1, min(K+1, loop_count[i]+1)):
		idx = P[idx] - 1
		p += C[idx]
		if loop:
			final_p = p + loop_point[i]*((K-j)//loop_count[i])
		else:
			final_p = p
		ans = ans if ans > final_p else final_p
print(ans)