N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

maxscore = -(10 ** 18)

for st in range(N):
	cumsum = [0]
	now = st
	while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	cumsum.append(cumsum[-1] + C[st])
	
	m = len(cumsum) - 1
	loopsum = cumsum[-1]
	
	if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	score = (K // m - 1) * loopsum
	extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
	score += max(extended_cumsum)
	maxscore = max(maxscore, score)
print(maxscore) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x: int(x) - 1, input().split())) P list(map(lambda x: int(x) - 1, input().split())) list map(lambda x: int(x) - 1, input().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x 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 maxscore = -(10 ** 18) maxscore -(10 ** 18) 10 ** 18 10 18 for st in range(N):
	cumsum = [0]
	now = st
	while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	cumsum.append(cumsum[-1] + C[st])
	
	m = len(cumsum) - 1
	loopsum = cumsum[-1]
	
	if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	score = (K // m - 1) * loopsum
	extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
	score += max(extended_cumsum)
	maxscore = max(maxscore, score) st range(N) range N cumsum = [0] cumsum [0] 0 now = st now st while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	 P[now] != st P[now] P now st now = P[now] now P[now] P now cumsum.append(cumsum[-1] + C[now]) cumsum.append cumsum append cumsum[-1] + C[now] cumsum[-1] cumsum -1 C[now] C now cumsum.append(cumsum[-1] + C[st]) cumsum.append cumsum append cumsum[-1] + C[st] cumsum[-1] cumsum -1 C[st] C st m = len(cumsum) - 1 m len(cumsum) - 1 len(cumsum) len cumsum 1 loopsum = cumsum[-1] loopsum cumsum[-1] cumsum -1 if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	 loopsum < 0 loopsum 0 maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)])) maxscore max(maxscore, max(cumsum[1:min(K + 1, m + 1)])) max maxscore max(cumsum[1:min(K + 1, m + 1)]) max cumsum[1:min(K + 1, m + 1)] cumsum 1:min(K + 1, m + 1) 1 min(K + 1, m + 1) min K + 1 K 1 m + 1 m 1 continue score = (K // m - 1) * loopsum score (K // m - 1) * loopsum K // m - 1 K // m K m 1 loopsum cumsum[i] + loopsum for i in range(1, K % m + 1) for i in range(1, K % m + 1) i range(1, K % m + 1) range 1 K % m + 1 K % m K m 1 for i in range(1, K % m + 1) cumsum[i] + loopsum cumsum[i] cumsum i loopsum extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)] extended_cumsum cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)] cumsum [cumsum[i] + loopsum for i in range(1, K % m + 1)] score += max(extended_cumsum) score max(extended_cumsum) max extended_cumsum maxscore = max(maxscore, score) maxscore max(maxscore, score) max maxscore score print(maxscore) print maxscore cumsum = [0] [0] cumsum score += max(extended_cumsum) max(extended_cumsum) score loopsum = cumsum[-1] cumsum[-1] loopsum P = list(map(lambda x: int(x) - 1, input().split())) list(map(lambda x: int(x) - 1, input().split())) P C = list(map(int, input().split())) list(map(int, input().split())) C now = P[now] P[now] now extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)] cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)] extended_cumsum score = (K // m - 1) * loopsum (K // m - 1) * loopsum score now = st st now maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)])) max(maxscore, max(cumsum[1:min(K + 1, m + 1)])) maxscore maxscore = -(10 ** 18) -(10 ** 18) maxscore N, K = map(int, input().split()) map(int, input().split()) N K = map(int, input().split()) map(int, input().split()) K m = len(cumsum) - 1 len(cumsum) - 1 m maxscore = max(maxscore, score) max(maxscore, score) maxscore
N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

maxscore = -(10 ** 18)

for st in range(N):
	cumsum = [0]
	now = st
	while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	cumsum.append(cumsum[-1] + C[st])
	
	m = len(cumsum) - 1
	loopsum = cumsum[-1]
	
	if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	score = (K // m - 1) * loopsum
	extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
	score += max(extended_cumsum)
	maxscore = max(maxscore, score)
print(maxscore)