import sys
import numpy as np
# from numba import njit

# @njit(cache=True)
def calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID

def upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal):

	# procCnt = 0
	currentCycleSumTmp = 0

	for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	return ans





def main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):

	ans = -1e18


	for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	print(int(ans))

if sys.argv[-1]=='ONLINE_JUDGE':
	# from numba import njit
	from numba.pycc import CC
	cc=CC('my_module')
	cc.export('calcCycle','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(calcCycle)
	# cc.export('main','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(main)
	cc.export('upd','i8(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# cc.export('upd','UniTuple(i8,3)(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# main=njit(main, cache=True)
	cc.compile()
	exit(0)
from my_module import upd,calcCycle
# from my_module import main,calcCycle
# from my_module import calcCycle



if __name__ == "__main__":
	N,K = map(int,input().split())
	P = np.array( input().split(), np.int64 )
	P -= 1
	C = np.array( input().split(), np.int64 )

	# 一度計算したサイクル情報を一応キャッシュしておく。。。
	# あんまり意味なさそう
	cycleIDs = np.full( N, -1, dtype=np.int64 )
	cycleItemCnts = np.full( N, -1, dtype=np.int64 )
	cycleTotalScores = np.full( N, -1, dtype=np.int64 )

	calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)

	# print(cycleItemCnts)


	main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)
	#print(ans)

 import sys sys import numpy as np numpy def calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID calcCycle cycleID = 0 cycleID 0 for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID n range(N) range N v = n v n currentCycleItemCnt = 0 currentCycleItemCnt 0 currentCycleTotal = 0 currentCycleTotal 0 if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID cycleIDs[v] != -1 cycleIDs[v] cycleIDs v -1 continue while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID True currentCycleItemCnt += 1 currentCycleItemCnt 1 currentCycleTotal += C[v] currentCycleTotal C[v] C v v = P[v] v P[v] P v if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				 v == n v n cycleIDs[v] = cycleID cycleIDs[v] cycleIDs v cycleID cycleItemCnts[cycleID] = currentCycleItemCnt cycleItemCnts[cycleID] cycleItemCnts cycleID currentCycleItemCnt cycleTotalScores[cycleID] = currentCycleTotal cycleTotalScores[cycleID] cycleTotalScores cycleID currentCycleTotal cycleID += 1 cycleID 1 break cycleIDs[v] = cycleID cycleIDs[v] cycleIDs v cycleID N N K K P P C C cycleIDs cycleIDs cycleItemCnts cycleItemCnts cycleTotalScores cycleTotalScores def upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal):

	# procCnt = 0
	currentCycleSumTmp = 0

	for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	return ans upd currentCycleSumTmp = 0 currentCycleSumTmp 0 for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	 k range(min(K,currentCycleItemCnt)) range min(K,currentCycleItemCnt) min K currentCycleItemCnt procCnt = k+1 procCnt k+1 k 1 currentCycleSumTmp += C[v] currentCycleSumTmp C[v] C v cycleLoopCnt = 0 cycleLoopCnt 0 if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		 0 < currentCycleTotal 0 currentCycleTotal cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt cycleLoopCnt ( K - procCnt ) // currentCycleItemCnt K - procCnt K procCnt currentCycleItemCnt ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal ) ans max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal ) max ans currentCycleSumTmp + cycleLoopCnt * currentCycleTotal currentCycleSumTmp cycleLoopCnt * currentCycleTotal cycleLoopCnt currentCycleTotal v = P[v] v P[v] P v return ans ans P P C C K K v v ans ans currentCycleItemCnt currentCycleItemCnt currentCycleTotal currentCycleTotal def main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):

	ans = -1e18


	for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	print(int(ans)) main ans = -1e18 ans -1e18 for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	 n range(N) range N v = n v n currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ] currentCycleItemCnt cycleItemCnts[ cycleIDs[v] ] cycleItemCnts cycleIDs[v] cycleIDs v currentCycleTotal cycleTotalScores[ cycleIDs[v] ] cycleTotalScores cycleIDs[v] cycleIDs v ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal) ans upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal) upd P C K v ans currentCycleItemCnt currentCycleTotal print(int(ans)) print int(ans) int ans N N K K P P C C cycleIDs cycleIDs cycleItemCnts cycleItemCnts cycleTotalScores cycleTotalScores if sys.argv[-1]=='ONLINE_JUDGE':
	# from numba import njit
	from numba.pycc import CC
	cc=CC('my_module')
	cc.export('calcCycle','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(calcCycle)
	# cc.export('main','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(main)
	cc.export('upd','i8(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# cc.export('upd','UniTuple(i8,3)(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# main=njit(main, cache=True)
	cc.compile()
	exit(0) sys.argv[-1]=='ONLINE_JUDGE' sys.argv[-1] sys.argv sys argv -1 'ONLINE_JUDGE' from numba.pycc import CC cc=CC('my_module') cc CC('my_module') CC 'my_module' cc.export('calcCycle','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(calcCycle) cc.export('calcCycle','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])') cc.export cc export 'calcCycle' 'void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])' calcCycle cc.export('upd','i8(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd) cc.export('upd','i8(i8[:],i8[:],i8,i8,i8,i8,i8)') cc.export cc export 'upd' 'i8(i8[:],i8[:],i8,i8,i8,i8,i8)' upd cc.compile() cc.compile cc compile exit(0) exit 0 from my_module import upd,calcCycle if __name__ == "__main__":
	N,K = map(int,input().split())
	P = np.array( input().split(), np.int64 )
	P -= 1
	C = np.array( input().split(), np.int64 )

	# 一度計算したサイクル情報を一応キャッシュしておく。。。
	# あんまり意味なさそう
	cycleIDs = np.full( N, -1, dtype=np.int64 )
	cycleItemCnts = np.full( N, -1, dtype=np.int64 )
	cycleTotalScores = np.full( N, -1, dtype=np.int64 )

	calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)

	# print(cycleItemCnts)


	main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)
	#print(ans) __name__ == "__main__" __name__ "__main__" N,K = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K P = np.array( input().split(), np.int64 ) P np.array( input().split(), np.int64 ) np.array np array input().split() ().split () input split np.int64 np int64 P -= 1 P 1 C = np.array( input().split(), np.int64 ) C np.array( input().split(), np.int64 ) np.array np array input().split() ().split () input split np.int64 np int64 cycleIDs = np.full( N, -1, dtype=np.int64 ) cycleIDs np.full( N, -1, dtype=np.int64 ) np.full np full N -1 dtype=np.int64 dtype np.int64 np int64 cycleItemCnts = np.full( N, -1, dtype=np.int64 ) cycleItemCnts np.full( N, -1, dtype=np.int64 ) np.full np full N -1 dtype=np.int64 dtype np.int64 np int64 cycleTotalScores = np.full( N, -1, dtype=np.int64 ) cycleTotalScores np.full( N, -1, dtype=np.int64 ) np.full np full N -1 dtype=np.int64 dtype np.int64 np int64 calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores) calcCycle N K P C cycleIDs cycleItemCnts cycleTotalScores main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores) main N K P C cycleIDs cycleItemCnts cycleTotalScores cc=CC('my_module') CC('my_module') cc cycleTotalScores = np.full( N, -1, dtype=np.int64 ) np.full( N, -1, dtype=np.int64 ) cycleTotalScores K = map(int,input().split()) map(int,input().split()) K P -= 1 1 P def upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal):

	# procCnt = 0
	currentCycleSumTmp = 0

	for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	return ans def upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal):

	# procCnt = 0
	currentCycleSumTmp = 0

	for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	return ans upd def main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):

	ans = -1e18


	for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	print(int(ans)) def main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):

	ans = -1e18


	for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	print(int(ans)) main N,K = map(int,input().split()) map(int,input().split()) N C = np.array( input().split(), np.int64 ) np.array( input().split(), np.int64 ) C def calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID def calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID calcCycle cycleIDs = np.full( N, -1, dtype=np.int64 ) np.full( N, -1, dtype=np.int64 ) cycleIDs cycleItemCnts = np.full( N, -1, dtype=np.int64 ) np.full( N, -1, dtype=np.int64 ) cycleItemCnts P = np.array( input().split(), np.int64 ) np.array( input().split(), np.int64 ) P
import sys
import numpy as np
# from numba import njit

# @njit(cache=True)
def calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			continue
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleIDs[v] = cycleID
					cycleItemCnts[cycleID] = currentCycleItemCnt
					cycleTotalScores[cycleID] = currentCycleTotal
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID

def upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal):

	# procCnt = 0
	currentCycleSumTmp = 0

	for k in range(min(K,currentCycleItemCnt)):

		# 頂点vにコマが置かれた時の最高スコアを計算し、
		# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する

		procCnt = k+1
		currentCycleSumTmp += C[v]

		cycleLoopCnt = 0
		if 0 < currentCycleTotal:
			cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		v = P[v]

	return ans





def main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores):

	ans = -1e18


	for n in range(N):

		v = n
		currentCycleItemCnt, currentCycleTotal = cycleItemCnts[ cycleIDs[v] ], cycleTotalScores[ cycleIDs[v] ]

		ans=upd(P,C,K,v,ans,currentCycleItemCnt,currentCycleTotal)
		# currentCycleSumTmp = 0
		# for k in range(min(K,currentCycleItemCnt)):

		# 	procCnt = k+1
		# 	currentCycleSumTmp += C[v]

		# 	cycleLoopCnt = 0
		# 	if 0 < currentCycleTotal:
		# 		cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

		# 	# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, "procCnt, cycleLoopCnt, currentCycleTotal=", procCnt, cycleLoopCnt, currentCycleTotal)
		# 	ans = max( ans, currentCycleSumTmp + cycleLoopCnt * currentCycleTotal )
		# 	v = P[v]


	print(int(ans))

if sys.argv[-1]=='ONLINE_JUDGE':
	# from numba import njit
	from numba.pycc import CC
	cc=CC('my_module')
	cc.export('calcCycle','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(calcCycle)
	# cc.export('main','void(i8,i8,i8[:],i8[:],i8[:],i8[:],i8[:])')(main)
	cc.export('upd','i8(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# cc.export('upd','UniTuple(i8,3)(i8[:],i8[:],i8,i8,i8,i8,i8)')(upd)
	# main=njit(main, cache=True)
	cc.compile()
	exit(0)
from my_module import upd,calcCycle
# from my_module import main,calcCycle
# from my_module import calcCycle



if __name__ == "__main__":
	N,K = map(int,input().split())
	P = np.array( input().split(), np.int64 )
	P -= 1
	C = np.array( input().split(), np.int64 )

	# 一度計算したサイクル情報を一応キャッシュしておく。。。
	# あんまり意味なさそう
	cycleIDs = np.full( N, -1, dtype=np.int64 )
	cycleItemCnts = np.full( N, -1, dtype=np.int64 )
	cycleTotalScores = np.full( N, -1, dtype=np.int64 )

	calcCycle(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)

	# print(cycleItemCnts)


	main(N,K,P,C,cycleIDs,cycleItemCnts,cycleTotalScores)
	#print(ans)


