#include <bits/stdc++.h>
#define ll long long
#define ss second
#define ff first
#define sz size()
#define el '\n'
#define tc ll tt; cin>>tt; while(tt--)
using namespace std;
void fastIO() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
const ll N = 1e5 + 5;
vector<ll> s[N],ps(N),temp(N);
ll dfs(ll i,ll n,ll t,ll k){
if (t == 0){
//cout << abs(ps[temp[i]] - ps[i]) << ' ' << abs(ps[n] - ps[i]) << "temps" << ' ' << temp[i] << el;
if (temp[i] == 0 || abs(ps[temp[i]] - ps[i]) > abs(ps[n] - ps[i]))
temp[i] = n;
//cout << n << ' ' << k << ' ' << ps[n] << el;
return abs(ps[n] - ps[i]);
}
ll f = 0,ans = 0;
for (auto x : s[n]){
f++;
ans = min(ans, dfs(i,x,t - 1,k));
}
if (!f) return -1;
return ans;
}
int main(){
fastIO();
ll n;
cin >> n;
ll res[N];
for (int i = 2;i <= n;i++){
ll x;
cin >> x;
s[x].push_back(i);
}
for (int i = 1;i <= n; i++) cin >> ps[i];
for (int i = 1;i <= n;i++){
ll k = dfs(i,i,4,ps[i]);
//cout <<'i' <<' ' << i << ' ' << ps[i] << el;
if (k != -1)
res[i] = temp[i];
else
res[i] = 0;
}
res[n] = 0;
for (int i = 1;i <= n;i++)
cout << res[i] << ' ';
}
Copy