#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end()
#define answer(x) cout << (x ? "YES\n" : "NO\n")
#define test ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while(T--)
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
using namespace std;
const int N = 200100, M = 30;
ll n, val[N], ans[N], depth[N], dp[N][M], who[N];
vector<int> adj[N];
void Dfs(int u,int p,int d)
{
dp[u][0]=p;
depth[u]=d;
for(int v : adj[u])
if(v!=p)
Dfs(v,u,d+1);
}
int kth(int u,int k)
{
if(k>depth[u])
return 0;
for(int i=M-1; i>=0; i--)
if(k&(1<<i))
u=dp[u][i];
return u;
}
int main()
{
cin >> n;
for(int i=2 ;i <=n; i++)
{
ll father; cin >> father;
adj[father].push_back(i);
}
for(int i=1 ; i<= n; i++) cin >> val[i], ans[i] = 1e18;
Dfs(1, -1, 0);
for(int j=1; j<M; j++)
for(int i=1; i<=n; i++)
dp[i][j]=dp[dp[i][j-1]][j-1];
for(int i=1 ; i<=n; i++)
{
ll pr = kth(i, 4);
if(pr && ans[pr] > abs(val[i] - val[pr]))
who[pr] = i,
ans[pr] = abs(val[i] - val[pr]);
}
for(int i=1 ;i<=n ;i++)
{
if(ans[i] == 1e18) cout << "0 ";
else cout << who[i] << ' ';
}
}
Copy