n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**10
for i in range(n):
	loop=[0]
	now=i
	while True:
		now=p[now]-1
		loop.append(loop[-1]+c[now])
		if now==i:
			break
	L=len(loop)-1
	if k<=L:
		score=max(loop[1:k+1])
		ans=max(score,ans)
	else:
		score=max(loop[-1],0)*(k//L-1)
		ans=max(ans,score)
		for j in range(k%L+L):
			score+=loop[j%L+1]-loop[j%L]
			ans=max(score,ans)
print(ans) 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 ans=-10**10 ans -10**10 -10 10 for i in range(n):
	loop=[0]
	now=i
	while True:
		now=p[now]-1
		loop.append(loop[-1]+c[now])
		if now==i:
			break
	L=len(loop)-1
	if k<=L:
		score=max(loop[1:k+1])
		ans=max(score,ans)
	else:
		score=max(loop[-1],0)*(k//L-1)
		ans=max(ans,score)
		for j in range(k%L+L):
			score+=loop[j%L+1]-loop[j%L]
			ans=max(score,ans) i range(n) range n loop=[0] loop [0] 0 now=i now i while True:
		now=p[now]-1
		loop.append(loop[-1]+c[now])
		if now==i:
			break
	 True now=p[now]-1 now p[now]-1 p[now] p now 1 loop.append(loop[-1]+c[now]) loop.append loop append loop[-1]+c[now] loop[-1] loop -1 c[now] c now if now==i:
			break
	 now==i now i break L=len(loop)-1 L len(loop)-1 len(loop) len loop 1 if k<=L:
		score=max(loop[1:k+1])
		ans=max(score,ans)
	else:
		score=max(loop[-1],0)*(k//L-1)
		ans=max(ans,score)
		for j in range(k%L+L):
			score+=loop[j%L+1]-loop[j%L]
			ans=max(score,ans) k<=L k L score=max(loop[1:k+1]) score max(loop[1:k+1]) max loop[1:k+1] loop 1:k+1 1 k+1 k 1 ans=max(score,ans) ans max(score,ans) max score ans score=max(loop[-1],0)*(k//L-1) score max(loop[-1],0)*(k//L-1) max(loop[-1],0) max loop[-1] loop -1 0 k//L-1 k//L k L 1 ans=max(ans,score) ans max(ans,score) max ans score for j in range(k%L+L):
			score+=loop[j%L+1]-loop[j%L]
			ans=max(score,ans) j range(k%L+L) range k%L+L k%L k L L score+=loop[j%L+1]-loop[j%L] score loop[j%L+1]-loop[j%L] loop[j%L+1] loop j%L+1 j%L j L 1 loop[j%L] loop j%L j L ans=max(score,ans) ans max(score,ans) max score ans print(ans) print ans L=len(loop)-1 len(loop)-1 L now=p[now]-1 p[now]-1 now p=list(map(int,input().split())) list(map(int,input().split())) p ans=-10**10 -10**10 ans score+=loop[j%L+1]-loop[j%L] loop[j%L+1]-loop[j%L] score n,k=map(int,input().split()) map(int,input().split()) n now=i i now score=max(loop[-1],0)*(k//L-1) max(loop[-1],0)*(k//L-1) score ans=max(ans,score) max(ans,score) ans loop=[0] [0] loop k=map(int,input().split()) map(int,input().split()) k c=list(map(int,input().split())) list(map(int,input().split())) c score=max(loop[1:k+1]) max(loop[1:k+1]) score ans=max(score,ans) max(score,ans) ans ans=max(score,ans) max(score,ans) ans
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**10
for i in range(n):
	loop=[0]
	now=i
	while True:
		now=p[now]-1
		loop.append(loop[-1]+c[now])
		if now==i:
			break
	L=len(loop)-1
	if k<=L:
		score=max(loop[1:k+1])
		ans=max(score,ans)
	else:
		score=max(loop[-1],0)*(k//L-1)
		ans=max(ans,score)
		for j in range(k%L+L):
			score+=loop[j%L+1]-loop[j%L]
			ans=max(score,ans)
print(ans)