import numpy as np

if __name__ == "__main__":
	N,K = map(int,input().split())
	P = [ int(p)-1 for p in input().split() ]
	C = list(map(int,input().split()))
	cycleIDs = np.full( N, -1, np.int64 )
	cycleInfs = []
	cycleID = 0
	procCnt = 0
	for n in range(N):
		v = n
		if cycleIDs[v] != -1:
			continue
		else:
			currentCycleCosts = []
			while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	ans = -10 ** 9
	for procCnt, currentCycleSize, currentCycleCosts in cycleInfs:
		loopScore = 0
		if np.sum(currentCycleCosts) > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleSize
			loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize])

		for i in range(currentCycleSize):
			ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore )

	print(ans) import numpy as np numpy if __name__ == "__main__":
	N,K = map(int,input().split())
	P = [ int(p)-1 for p in input().split() ]
	C = list(map(int,input().split()))
	cycleIDs = np.full( N, -1, np.int64 )
	cycleInfs = []
	cycleID = 0
	procCnt = 0
	for n in range(N):
		v = n
		if cycleIDs[v] != -1:
			continue
		else:
			currentCycleCosts = []
			while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	ans = -10 ** 9
	for procCnt, currentCycleSize, currentCycleCosts in cycleInfs:
		loopScore = 0
		if np.sum(currentCycleCosts) > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleSize
			loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize])

		for i in range(currentCycleSize):
			ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore )

	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 int(p)-1 for p in input().split() for p in input().split() p input().split() ().split () input split for p in input().split() int(p)-1 int(p) int p 1 P = [ int(p)-1 for p in input().split() ] P [ int(p)-1 for p in 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 cycleIDs = np.full( N, -1, np.int64 ) cycleIDs np.full( N, -1, np.int64 ) np.full np full N -1 np.int64 np int64 cycleInfs = [] cycleInfs [] cycleID = 0 cycleID 0 procCnt = 0 procCnt 0 for n in range(N):
		v = n
		if cycleIDs[v] != -1:
			continue
		else:
			currentCycleCosts = []
			while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	 n range(N) range N v = n v n if cycleIDs[v] != -1:
			continue
		else:
			currentCycleCosts = []
			while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	 cycleIDs[v] != -1 cycleIDs[v] cycleIDs v -1 continue currentCycleCosts = [] currentCycleCosts [] while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	 True currentCycleCosts.append( C[v] ) currentCycleCosts.append currentCycleCosts append C[v] C v cycleIDs[v] = cycleID cycleIDs[v] cycleIDs v cycleID v = P[v] v P[v] P v if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	 cycleIDs[v] != -1 cycleIDs[v] cycleIDs v -1 procCnt = K % len( currentCycleCosts ) procCnt K % len( currentCycleCosts ) K len( currentCycleCosts ) len currentCycleCosts if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					 len( currentCycleCosts ) + procCnt <= K len( currentCycleCosts ) + procCnt len( currentCycleCosts ) len currentCycleCosts procCnt K procCnt += len( currentCycleCosts ) procCnt len( currentCycleCosts ) len currentCycleCosts cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) ) cycleInfs.append cycleInfs append ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) procCnt len(currentCycleCosts) len currentCycleCosts np.array( currentCycleCosts + currentCycleCosts ) np.array np array currentCycleCosts + currentCycleCosts currentCycleCosts currentCycleCosts cycleID += 1 cycleID 1 break ans = -10 ** 9 ans -10 ** 9 -10 9 for procCnt, currentCycleSize, currentCycleCosts in cycleInfs:
		loopScore = 0
		if np.sum(currentCycleCosts) > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleSize
			loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize])

		for i in range(currentCycleSize):
			ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore )

	 procCnt currentCycleSize currentCycleCosts cycleInfs loopScore = 0 loopScore 0 if np.sum(currentCycleCosts) > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleSize
			loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize])

		 np.sum(currentCycleCosts) > 0 np.sum(currentCycleCosts) np.sum np sum currentCycleCosts 0 cycleLoopCnt =  ( K - procCnt ) // currentCycleSize cycleLoopCnt ( K - procCnt ) // currentCycleSize K - procCnt K procCnt currentCycleSize loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize]) loopScore cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize]) cycleLoopCnt np.sum(currentCycleCosts[:currentCycleSize]) np.sum np sum currentCycleCosts[:currentCycleSize] currentCycleCosts :currentCycleSize currentCycleSize for i in range(currentCycleSize):
			ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore )

	 i range(currentCycleSize) range currentCycleSize ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore ) ans max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore ) max ans np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() [:procCnt].max [:procCnt] np.roll( currentCycleCosts, i ).cumsum() np.roll( currentCycleCosts, i ).cumsum np.roll( currentCycleCosts, i ) np.roll np roll currentCycleCosts i cumsum :procCnt procCnt max loopScore print(ans) print ans v = n n v loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize]) cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize]) loopScore cycleID = 0 0 cycleID P = [ int(p)-1 for p in input().split() ] [ int(p)-1 for p in input().split() ] P ans = -10 ** 9 -10 ** 9 ans procCnt = 0 0 procCnt ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore ) max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore ) ans currentCycleCosts = [] [] currentCycleCosts cycleInfs = [] [] cycleInfs procCnt += len( currentCycleCosts ) len( currentCycleCosts ) procCnt cycleLoopCnt =  ( K - procCnt ) // currentCycleSize ( K - procCnt ) // currentCycleSize cycleLoopCnt v = P[v] P[v] v C = list(map(int,input().split())) list(map(int,input().split())) C K = map(int,input().split()) map(int,input().split()) K cycleIDs = np.full( N, -1, np.int64 ) np.full( N, -1, np.int64 ) cycleIDs cycleID += 1 1 cycleID loopScore = 0 0 loopScore procCnt = K % len( currentCycleCosts ) K % len( currentCycleCosts ) procCnt N,K = map(int,input().split()) map(int,input().split()) N
import numpy as np

if __name__ == "__main__":
	N,K = map(int,input().split())
	P = [ int(p)-1 for p in input().split() ]
	C = list(map(int,input().split()))
	cycleIDs = np.full( N, -1, np.int64 )
	cycleInfs = []
	cycleID = 0
	procCnt = 0
	for n in range(N):
		v = n
		if cycleIDs[v] != -1:
			continue
		else:
			currentCycleCosts = []
			while True:
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

				v = P[v]
				if cycleIDs[v] != -1:
					procCnt = K % len( currentCycleCosts )
					if len( currentCycleCosts ) + procCnt <= K:
						procCnt += len( currentCycleCosts )
					cycleInfs.append( ( procCnt, len(currentCycleCosts), np.array( currentCycleCosts + currentCycleCosts ) ) )
					cycleID += 1
					break
	ans = -10 ** 9
	for procCnt, currentCycleSize, currentCycleCosts in cycleInfs:
		loopScore = 0
		if np.sum(currentCycleCosts) > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleSize
			loopScore = cycleLoopCnt * np.sum(currentCycleCosts[:currentCycleSize])

		for i in range(currentCycleSize):
			ans = max( ans, np.roll( currentCycleCosts, i ).cumsum()[:procCnt].max() + loopScore )

	print(ans)
