import sys
input = sys.stdin.readline

# D - Moving Piece
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

# 問題文とインデックスの位置を揃える
P.insert(0, 0)
C.insert(0, 0)

# マス目を周期ごとに分割する
groups = []
appeared = set()

for i in range(1, N + 1):
	if not i in appeared:
		group = []
		s = set()
		j = i

		while not j in s:
			group.append(j)
			s.add(j)
			appeared.add(j)
			j = P[j]

		groups.append(group)

minus_max = -sys.maxsize
ans = minus_max

# 周期ごとにハイスコアを算出
for group in groups:
	n = len(group)

	# スタート位置を1マスずつズラす
	for g in group:
		score = 0
		max_score = minus_max
		scores = []
		scores.append(minus_max)
		Pi = P[g]

		for j in range(n):
			score += C[Pi]
			max_score = max(max_score, score)
			scores.append(max_score)
			Pi = P[Pi]

		if K > n and score > 0:
			s = scores[K % n] + score * (K // n)
			ans = max(ans, s)

			s = scores[n] + score * (K // n - 1)
			ans = max(ans, s)
		elif K > n:
			ans = max(ans, scores[n])
		else:
			ans = max(ans, scores[K])

print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split P.insert(0, 0) P.insert P insert 0 0 C.insert(0, 0) C.insert C insert 0 0 groups = [] groups [] appeared = set() appeared set() set for i in range(1, N + 1):
	if not i in appeared:
		group = []
		s = set()
		j = i

		while not j in s:
			group.append(j)
			s.add(j)
			appeared.add(j)
			j = P[j]

		groups.append(group) i range(1, N + 1) range 1 N + 1 N 1 if not i in appeared:
		group = []
		s = set()
		j = i

		while not j in s:
			group.append(j)
			s.add(j)
			appeared.add(j)
			j = P[j]

		groups.append(group) not i in appeared i in appeared i appeared group = [] group [] s = set() s set() set j = i j i while not j in s:
			group.append(j)
			s.add(j)
			appeared.add(j)
			j = P[j]

		 not j in s j in s j s group.append(j) group.append group append j s.add(j) s.add s add j appeared.add(j) appeared.add appeared add j j = P[j] j P[j] P j groups.append(group) groups.append groups append group minus_max = -sys.maxsize minus_max -sys.maxsize sys.maxsize sys maxsize ans = minus_max ans minus_max for group in groups:
	n = len(group)

	# スタート位置を1マスずつズラす
	for g in group:
		score = 0
		max_score = minus_max
		scores = []
		scores.append(minus_max)
		Pi = P[g]

		for j in range(n):
			score += C[Pi]
			max_score = max(max_score, score)
			scores.append(max_score)
			Pi = P[Pi]

		if K > n and score > 0:
			s = scores[K % n] + score * (K // n)
			ans = max(ans, s)

			s = scores[n] + score * (K // n - 1)
			ans = max(ans, s)
		elif K > n:
			ans = max(ans, scores[n])
		else:
			ans = max(ans, scores[K]) group groups n = len(group) n len(group) len group for g in group:
		score = 0
		max_score = minus_max
		scores = []
		scores.append(minus_max)
		Pi = P[g]

		for j in range(n):
			score += C[Pi]
			max_score = max(max_score, score)
			scores.append(max_score)
			Pi = P[Pi]

		if K > n and score > 0:
			s = scores[K % n] + score * (K // n)
			ans = max(ans, s)

			s = scores[n] + score * (K // n - 1)
			ans = max(ans, s)
		elif K > n:
			ans = max(ans, scores[n])
		else:
			ans = max(ans, scores[K]) g group score = 0 score 0 max_score = minus_max max_score minus_max scores = [] scores [] scores.append(minus_max) scores.append scores append minus_max Pi = P[g] Pi P[g] P g for j in range(n):
			score += C[Pi]
			max_score = max(max_score, score)
			scores.append(max_score)
			Pi = P[Pi]

		 j range(n) range n score += C[Pi] score C[Pi] C Pi max_score = max(max_score, score) max_score max(max_score, score) max max_score score scores.append(max_score) scores.append scores append max_score Pi = P[Pi] Pi P[Pi] P Pi if K > n and score > 0:
			s = scores[K % n] + score * (K // n)
			ans = max(ans, s)

			s = scores[n] + score * (K // n - 1)
			ans = max(ans, s)
		elif K > n:
			ans = max(ans, scores[n])
		else:
			ans = max(ans, scores[K]) K > n and score > 0 K > n K n score > 0 score 0 s = scores[K % n] + score * (K // n) s scores[K % n] + score * (K // n) scores[K % n] scores K % n K n score * (K // n) score K // n K n ans = max(ans, s) ans max(ans, s) max ans s s = scores[n] + score * (K // n - 1) s scores[n] + score * (K // n - 1) scores[n] scores n score * (K // n - 1) score K // n - 1 K // n K n 1 ans = max(ans, s) ans max(ans, s) max ans s elif K > n:
			ans = max(ans, scores[n])
		 K > n K n ans = max(ans, scores[n]) ans max(ans, scores[n]) max ans scores[n] scores n ans = max(ans, scores[K]) ans max(ans, scores[K]) max ans scores[K] scores K print(ans) print ans groups = [] [] groups C = list(map(int, input().split())) list(map(int, input().split())) C s = scores[K % n] + score * (K // n) scores[K % n] + score * (K // n) s ans = max(ans, scores[K]) max(ans, scores[K]) ans scores = [] [] scores minus_max = -sys.maxsize -sys.maxsize minus_max ans = max(ans, scores[n]) max(ans, scores[n]) ans Pi = P[Pi] P[Pi] Pi group = [] [] group P = list(map(int, input().split())) list(map(int, input().split())) P ans = minus_max minus_max ans max_score = minus_max minus_max max_score input = sys.stdin.readline sys.stdin.readline input ans = max(ans, s) max(ans, s) ans n = len(group) len(group) n appeared = set() set() appeared K = map(int, input().split()) map(int, input().split()) K s = scores[n] + score * (K // n - 1) scores[n] + score * (K // n - 1) s N, K = map(int, input().split()) map(int, input().split()) N score = 0 0 score j = P[j] P[j] j max_score = max(max_score, score) max(max_score, score) max_score score += C[Pi] C[Pi] score Pi = P[g] P[g] Pi s = set() set() s j = i i j ans = max(ans, s) max(ans, s) ans
import sys
input = sys.stdin.readline

# D - Moving Piece
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

# 問題文とインデックスの位置を揃える
P.insert(0, 0)
C.insert(0, 0)

# マス目を周期ごとに分割する
groups = []
appeared = set()

for i in range(1, N + 1):
	if not i in appeared:
		group = []
		s = set()
		j = i

		while not j in s:
			group.append(j)
			s.add(j)
			appeared.add(j)
			j = P[j]

		groups.append(group)

minus_max = -sys.maxsize
ans = minus_max

# 周期ごとにハイスコアを算出
for group in groups:
	n = len(group)

	# スタート位置を1マスずつズラす
	for g in group:
		score = 0
		max_score = minus_max
		scores = []
		scores.append(minus_max)
		Pi = P[g]

		for j in range(n):
			score += C[Pi]
			max_score = max(max_score, score)
			scores.append(max_score)
			Pi = P[Pi]

		if K > n and score > 0:
			s = scores[K % n] + score * (K // n)
			ans = max(ans, s)

			s = scores[n] + score * (K // n - 1)
			ans = max(ans, s)
		elif K > n:
			ans = max(ans, scores[n])
		else:
			ans = max(ans, scores[K])

print(ans)