n, k = map(int, input().split())
P = [int(x)-1 for x in input().split()]
C = [int(x) for x in input().split()]

vis = [0 for i in range(n+1)]
	

res = -10000000000000000000000
for i in range(0,n):
	if vis[i] == 0:
		su = 0
		vis[i] = 1
		v = []
		v.append(C[i])
		su += C[i]
		x = P[i]
		while x != i:
			vis[x] = 1
			su += C[x]
			v.append(C[x])
			x = P[x]
		vs = len(v)
		for ii in range(vs):
			umm = 0
			for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm)


print(res) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(x)-1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x)-1 int(x) int x 1 P = [int(x)-1 for x in input().split()] P [int(x)-1 for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C = [int(x) for x in input().split()] C [int(x) for x in input().split()] 0 for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) 0 vis = [0 for i in range(n+1)] vis [0 for i in range(n+1)] res = -10000000000000000000000 res -10000000000000000000000 for i in range(0,n):
	if vis[i] == 0:
		su = 0
		vis[i] = 1
		v = []
		v.append(C[i])
		su += C[i]
		x = P[i]
		while x != i:
			vis[x] = 1
			su += C[x]
			v.append(C[x])
			x = P[x]
		vs = len(v)
		for ii in range(vs):
			umm = 0
			for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm) i range(0,n) range 0 n if vis[i] == 0:
		su = 0
		vis[i] = 1
		v = []
		v.append(C[i])
		su += C[i]
		x = P[i]
		while x != i:
			vis[x] = 1
			su += C[x]
			v.append(C[x])
			x = P[x]
		vs = len(v)
		for ii in range(vs):
			umm = 0
			for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm) vis[i] == 0 vis[i] vis i 0 su = 0 su 0 vis[i] = 1 vis[i] vis i 1 v = [] v [] v.append(C[i]) v.append v append C[i] C i su += C[i] su C[i] C i x = P[i] x P[i] P i while x != i:
			vis[x] = 1
			su += C[x]
			v.append(C[x])
			x = P[x]
		 x != i x i vis[x] = 1 vis[x] vis x 1 su += C[x] su C[x] C x v.append(C[x]) v.append v append C[x] C x x = P[x] x P[x] P x vs = len(v) vs len(v) len v for ii in range(vs):
			umm = 0
			for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm) ii range(vs) range vs umm = 0 umm 0 for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm) j range(vs) range vs x = (ii+j)%vs x (ii+j)%vs ii+j ii j vs umm += v[x] umm v[x] v x steps = 1+(x-ii+vs)%vs steps 1+(x-ii+vs)%vs 1 (x-ii+vs)%vs x-ii+vs x-ii x ii vs vs if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm) k >= vs k vs if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				 su > 0 su 0 r = max((k-steps)//vs,0) r max((k-steps)//vs,0) max (k-steps)//vs k-steps k steps vs 0 res = max(res, r*su + umm) res max(res, r*su + umm) max res r*su + umm r*su r su umm res = max(res,umm) res max(res,umm) max res umm if steps <= k:
						res = max(res,umm) steps <= k steps k res = max(res,umm) res max(res,umm) max res umm print(res) print res P = [int(x)-1 for x in input().split()] [int(x)-1 for x in input().split()] P res = max(res,umm) max(res,umm) res x = P[i] P[i] x C = [int(x) for x in input().split()] [int(x) for x in input().split()] C x = P[x] P[x] x k = map(int, input().split()) map(int, input().split()) k res = max(res,umm) max(res,umm) res umm = 0 0 umm res = -10000000000000000000000 -10000000000000000000000 res x = (ii+j)%vs (ii+j)%vs x su += C[x] C[x] su vs = len(v) len(v) vs steps = 1+(x-ii+vs)%vs 1+(x-ii+vs)%vs steps vis = [0 for i in range(n+1)] [0 for i in range(n+1)] vis umm += v[x] v[x] umm r = max((k-steps)//vs,0) max((k-steps)//vs,0) r res = max(res, r*su + umm) max(res, r*su + umm) res su += C[i] C[i] su v = [] [] v n, k = map(int, input().split()) map(int, input().split()) n su = 0 0 su
n, k = map(int, input().split())
P = [int(x)-1 for x in input().split()]
C = [int(x) for x in input().split()]

vis = [0 for i in range(n+1)]
	

res = -10000000000000000000000
for i in range(0,n):
	if vis[i] == 0:
		su = 0
		vis[i] = 1
		v = []
		v.append(C[i])
		su += C[i]
		x = P[i]
		while x != i:
			vis[x] = 1
			su += C[x]
			v.append(C[x])
			x = P[x]
		vs = len(v)
		for ii in range(vs):
			umm = 0
			for j in range(vs):
				x = (ii+j)%vs
				umm += v[x]
				steps = 1+(x-ii+vs)%vs
				if k >= vs:
					if(su > 0):
						r = max((k-steps)//vs,0)
						res = max(res, r*su + umm)
					else:
						res = max(res,umm)
				else:
					if steps <= k:
						res = max(res,umm)


print(res)