#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<vector<int>>v;
int p[200003];
int ans[200003];
void bfs(int x) {
queue<int>q;
int vis[200003] = {};
vis[x] = 1;
q.push(x);
int t = 2000000000;
while (!q.empty()) {
int y = q.front();
if (vis[y] == 5) {
if (t > abs(p[x] - p[y])) {
ans[x] = y;
t = abs(p[x] - p[y]);
}
else if (t == abs(p[x] - p[y])) {
if (y < ans[x]) {
ans[x] = y;
}
}
q.pop();
continue;
}
for (int i = 0; i < v[y].size(); i++) {
if (!vis[v[y][i]]) {
q.push(v[q.front()][i]);
vis[v[y][i]] = vis[y] + 1;
}
}
q.pop();
}
}
void solve() {
int n;
cin >> n;
v.resize(n + 2);
int a;
for (int i = 2; i <= n; i++) {
cin >> a;
v[a].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> a;
p[i] = a;
}
for (int i = 1; i <= n; i++) {
bfs(i);
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
}
int main() {
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
Copy