n = int(input())
m = [int(b) for b in input().split(" ")]
p = [int(b) for b in input().split(" ")]
h = {}
for i in range(1, n + 1):
h[i] = []
for i in range(n - 1):
h[m[i]].append(i + 2)
def find_candidates(current, depth):
q = []
q.append((current, depth))
while (len(q) > 0):
curr = q[0]
if curr[1] == 0:
candidates.append((curr[0], p[curr[0] - 1]))
for can in h[curr[0]]:
if depth > 1:
q.append((can, curr[1] - 1))
q = q[1::]
for i in range(1 ,n + 1):
candidates = []
find_candidates(i, 4)
if len(candidates) > 0:
candidates.sort(key = lambda a : abs(p[i - 1] - a[1]))
print(candidates[0][0], sep="", end="")
else:
print(0, sep="", end="")
if i != n:
print(" ", end="")
print()
Copy