Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []
lp_sum = []
range_s = []
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		lp_sum.append((lp_cum[-1][-1], len(lp[-1])))
		range_s.append(set())
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				range_s[-1].add((lp_cum[-1][x] - lp_cum[-1][y], x-y))
				if x < len(lp_cum[-1])-1 or y > 0:
					range_s[-1].add((lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y))

ok, ng = -10**9, 10**18+1

while ng-ok > 1:
	x = (ok+ng)//2
	flg = False
	for i in range(len(lp)):
		s_sum, s_num = lp_sum[i]
		if s_sum <= 0:
			for r_sum, r_num in range_s[i]:
				if r_sum >= x and r_num <= k:
					flg = True
					break
		else:
			for r_sum, r_num in range_s[i]:
				if r_num > k:
					continue
				if r_sum >= x:
					flg = True
					break
				rep = (x-1-r_sum) // s_sum + 1
				if rep * s_num + r_num <= k:
					flg = True
					break
	if flg:
		ok = x
	else:
		ng = x

print(ok)

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []

ans = -10**9
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		t_sum, t_num = lp_cum[-1][-1], len(lp[-1])
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				r_sum, r_num = lp_cum[-1][x] - lp_cum[-1][y], x-y
				if t_sum <= 0:
					if r_num <= k:
						ans = max(ans, r_sum)
				else:
					if r_num <= k:
						ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))
				if x < len(lp_cum[-1])-1 or y > 0:
					r_sum, r_num = lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y
					if t_sum <= 0:
						if r_num <= k:
							ans = max(ans, r_sum)
					else:
						if r_num <= k:
							ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []
lp_sum = []
range_s = []
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		lp_sum.append((lp_cum[-1][-1], len(lp[-1])))
		range_s.append(set())
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				range_s[-1].add((lp_cum[-1][x] - lp_cum[-1][y], x-y))
				if x < len(lp_cum[-1])-1 or y > 0:
					range_s[-1].add((lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y))

ok, ng = -10**9, 10**18+1

while ng-ok > 1:
	x = (ok+ng)//2
	flg = False
	for i in range(len(lp)):
		s_sum, s_num = lp_sum[i]
		if s_sum <= 0:
			for r_sum, r_num in range_s[i]:
				if r_sum >= x and r_num <= k:
					flg = True
					break
		else:
			for r_sum, r_num in range_s[i]:
				if r_num > k:
					continue
				if r_sum >= x:
					flg = True
					break
				rep = (x-1-r_sum) // s_sum + 1
				if rep * s_num + r_num <= k:
					flg = True
					break
	if flg:
		ok = x
	else:
		ng = x

print(ok)

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [x-1 for x in p]
c = list(map(int, input().split()))

visited = [False for _ in range(n)]
lp = []
lp_cum = []

ans = -10**9
for i in range(n):
	if not visited[i]:
		lp.append([i])
		lp_cum.append([0, c[i]])
		visited[i] = True
		while not visited[p[lp[-1][-1]]]:
			lp[-1].append(p[lp[-1][-1]])
			lp_cum[-1].append(lp_cum[-1][-1] + c[lp[-1][-1]])
			visited[lp[-1][-1]] = True
		t_sum, t_num = lp_cum[-1][-1], len(lp[-1])
		for x in range(1, len(lp_cum[-1])):
			for y in range(x):
				r_sum, r_num = lp_cum[-1][x] - lp_cum[-1][y], x-y
				if t_sum <= 0:
					if r_num <= k:
						ans = max(ans, r_sum)
				else:
					if r_num <= k:
						ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))
				if x < len(lp_cum[-1])-1 or y > 0:
					r_sum, r_num = lp_cum[-1][-1] - lp_cum[-1][x] + lp_cum[-1][y], len(lp[-1])-x+y
					if t_sum <= 0:
						if r_num <= k:
							ans = max(ans, r_sum)
					else:
						if r_num <= k:
							ans = max(ans, r_sum + t_sum * ((k - r_num) // t_num))

print(ans)