#include <bits/stdc++.h>
using namespace std;
#define DBG cerr << __LINE__ << ' ' << __FUNCTION__ << endl
#define CLOSE ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define pb push_back
#define mk make_pair
#define sz(x) (int)(x).size()
#define isd(c) ('0'<=(c)&&(c)<='9')
#define isa(c) ('a'<=(c)&&(c)<='z')
#define isA(c) ('A'<=(c)&&(c)<='Z')
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define eps 1e-8
#define ri register int
#define rl register long long
#define LL long long
#define ULL unsigned long long

const LL mod = 1e9 + 7;
const int N = 2e5 + 50;

int to[N], d[N], dc[N];
LL a[N], b[N];
int n;

bool check(LL x) {
    queue<int>q;
    for(ri i = 1; i <= n; ++i) {
        if(!d[i]) {
            b[i] = x;
            q.push(i);
        } else {
            b[i] = 0;
        }
        dc[i] = d[i];
    }
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        if(b[cur] < a[cur]) {
            return false;
        }
        int e = to[cur];
        b[e] += (b[cur] - a[cur]);
        --dc[e];
        if(dc[e] == 0) {
            q.push(e);
        }
    }
    if(b[1] >= a[1]) {
        return true;
    } else {
        return false;
    }
}

int main() {
    CLOSE;
    cin >> n;
    for(ri i = 2; i <= n; ++i) {
        int t;
        cin >> t;
        ++d[t];
        to[i] = t;
    }
    for(ri i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    LL l = 0, r = 1e16;
    while(l < r) {
        LL mid = (l + r) >> 1;
        if(check(mid)) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    cout << r << endl;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left<inf) left+=temp;
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e15,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left<inf) left+=temp;
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e13,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)2e14,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e11,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        if(left<inf) left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e13,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e13,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left>inf-temp) flag = 2;
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)2e14,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if((dfs(1)>=0 && flag==0) || flag==2)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left<inf) left+=temp;
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 0,r = (int)1e15,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e14,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left<inf) left+=temp;
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 0,r = (int)1e13,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)1e18,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        if(dfs(1)>=0 && flag==0)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
const int inf = 100000000000000000;
#define maxn 1000005
#define mod 998244353
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){  for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
int modexp(int x,int y) {
   int res = 1;
   x = x % mod;
   while (y > 0){
       if (y & 1LL)
           res = (res*x) % mod;
       y = y>>1;
       x = (x*x) % mod;
   }
   return res;
}
int invmod(int a) {
    return modexp(a,mod-2);
}
vector<int> edge[maxn];
void ipgraph(int n,int m) {
    fp(i,1,n) edge[i].clear();
    if(m==0) return;
   fp(i,1,m) {
       int a,b;
       cin>>a>>b;
       edge[a].pb(b);
       edge[b].pb(a);
   }
}
/*
void dfs(int node,int p)
{
    for(int u : edge[node])
    {
        if(u!=p)
        {
            dfs(u,node);
        }
    }
}
*/
///////////////////////////////////////////////////////////////////////////////////
int a[maxn],ileft,flag = 0;
int dfs(int node)
{
    int left = -a[node];
    for(int u : edge[node])
    {
        int temp = dfs(u);
        if(left>inf-temp) flag = 2;
        left+=dfs(u);
    }
    if(edge[node].empty()) left+=ileft;
    if(left<0) flag = 1;
    return left;
}
void solve()
{
    int n;
    cin>>n;
    fp(i,2,n)
    {
        int temp;
        cin>>temp;
        edge[temp].pb(i);
    }
    fp(i,1,n) cin>>a[i];
    int l = 1,r = (int)2e14,ans = -1;
    while(l<=r)
    {
        int mid = (l+r)/2;
        flag = 0;
        ileft = mid;
        int temp = dfs(1);
        if((temp>=0 && flag==0) || flag==2)
        {
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    d(ans)
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   int t=1;
    //cin>>t;
   fp(i,1,t)
   {
       //cout<<"Case #"<<i<<": ";
       solve();
   }
   #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(int m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;cin>>p;
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)cin>>po[i];
    dfs(1,-1);

    ll l=0,r=1e12;
    ll ans=0;
    while(l!=r){
        int m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ull>leaf(200001,1);
vector<ull>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ull l=0,r=1e15;
    ull ans=0;
    while(l!=r){
        ull m=(l+r)/2;
        if(check(m,n))ans=m,r=m;
        else l=m+1;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ll l=0,r=1e12;
    ll ans=0;
    while(l!=r){
        ll m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(100001);
vector<int>leaf(1000001,1);
vector<int>pop(1000001);
vector<int>mx(1000001);
vector<bool>ind(1000001);

void dfs(int v,int e=-1){
  for(auto to:g[v]){
    if(to!=e)dfs(to,v);

  }
  if(e!=-1){
  if(!ind[e])leaf[e]--,ind[e]=true;
  leaf[e]+=leaf[v];
  pop[e]+=pop[v];
  mx[e]=max(mx[v],mx[e]);

  }
}
void DFS(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)DFS(to,v);
    }
    if(e!=-1){
        int lo=mx[e]*leaf[e];
        if(pop[e]>lo){
            int k=pop[e]-lo;
            if(k%leaf[e])mx[e]++;
            k/=leaf[e];
            mx[e]+=k;
        }
    }
}

int main(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int k;cin>>k;
        g[k].pb(i);
        g[i].pb(k);
    }
    for(int i=1;i<=n;i++){
        cin>>pop[i];
    }
    for(int i=2;i<=n;i++){
        if(g[i].size()==1)mx[i]=pop[i];
    }
    dfs(1);

    DFS(1);
    cout<<mx[1];
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ull>leaf(200001,1);
vector<ull>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ll l=0,r=1e15;
    ll ans=0;
    while(l!=r){
        ll m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ll l=0,r=1e15;
    ll ans=0;
    while(l!=r){
        ll m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(int m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;cin>>p;
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)cin>>po[i];
    dfs(1,-1);

    ll l=0,r=1e12;
    ll ans=0;
    while(l!=r){
        int m=(l+r)/2;
        if(check(m,n))ans=m,r=m;
        else l=m+1;
    }
    cout<<ans;
}
int main(){
solve();}


#include <bits/stdc++.h>
#define ll  long long
#define f first
#define s second
#define pii pair<int,int>
#define pll piar<ll,ll>
#define ull unsigned ll
#define pb push_back
#define epb emplace_back
using namespace std;

void solve(){
   int n;cin>>n;
   vector<int>ans;
   bool ind=false;
   set<int,greater<int> >s;
   for(int i=1;i<=n;i++)s.insert(i);
   int b=1;
   for(int i=1;i<=2*n;i++){
    char c;cin>>c;

    if(c=='-'&&i!=1){
        int num=1;
        int k;cin>>k;
        if(ind==true)continue;
        if(i-b==0&&s.find(k)!=s.end()){
            ind=true;
            continue;
        }
        if(s.find(k)!=s.end())
            ans.pb(k);
        s.erase(k);
        vector<int>vec;
        for(auto to:s){
            if(num>=i-b)break;
            if(to>k)ans.pb(to),vec.pb(to),num++;
            else {
                ind=true;
                break;
                }
            }
            for(auto to:vec)s.erase(to);
            b=i+1;
        }
       if(c=='-'&&i==1){
        ind=true;
        int k;cin>>k;
       }
    }
   if(ind)cout<<"NO"<<"\n";
   else{
    cout<<"YES"<<"\n";
    for(auto ans1:ans)cout<<ans1<<' ';
   }
   cout<<"\n";
}

int main(){
     solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<ll> >g(200001);
vector<ll>leaf(2000001,1);
vector<ll>pop(2000001);
vector<ll>mx(2000001);
vector<bool>ind(2000001);

void dfs(int v,int e=-1){
  for(auto to:g[v]){
    if(to!=e)dfs(to,v);

  }
  if(e!=-1){
  if(!ind[e])leaf[e]--,ind[e]=true;
  leaf[e]+=leaf[v];
  pop[e]+=pop[v];
  mx[e]=max(mx[v],mx[e]);

  }
}
void DFS(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)DFS(to,v);
    }
    if(e!=-1){
        ll lo=mx[e]*leaf[e];
        if(pop[e]>lo){
            ll k=pop[e]-lo;
            if(k%leaf[e])mx[e]++;
            k/=leaf[e];
            mx[e]+=k;
        }
    }
}

int main(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int k;cin>>k;
        g[k].pb(i);
        g[i].pb(k);
    }
    for(int i=1;i<=n;i++){
        cin>>pop[i];
    }
    for(int i=2;i<=n;i++){
        if(g[i].size()==1)mx[i]=pop[i];
    }
    dfs(1);

    DFS(1);
    cout<<mx[1];
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<int>leaf(2000001,1);
vector<int>pop(2000001);
vector<int>mx(2000001);
vector<bool>ind(2000001);

void dfs(int v,int e=-1){
  for(auto to:g[v]){
    if(to!=e)dfs(to,v);

  }
  if(e!=-1){
  if(!ind[e])leaf[e]--,ind[e]=true;
  leaf[e]+=leaf[v];
  pop[e]+=pop[v];
  mx[e]=max(mx[v],mx[e]);

  }
}
void DFS(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)DFS(to,v);
    }
    if(e!=-1){
        int lo=mx[e]*leaf[e];
        if(pop[e]>lo){
            int k=pop[e]-lo;
            if(k%leaf[e])mx[e]++;
            k/=leaf[e];
            mx[e]+=k;
        }
    }
}

int main(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int k;cin>>k;
        g[k].pb(i);
        g[i].pb(k);
    }
    for(int i=1;i<=n;i++){
        cin>>pop[i];
    }
    for(int i=2;i<=n;i++){
        if(g[i].size()==1)mx[i]=pop[i];
    }
    dfs(1);

    DFS(1);
    cout<<mx[1];
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<ll> >g(200001);
vector<ll>leaf(2000001,1);
vector<ll>pop(2000001);
vector<ll>mx(2000001);
vector<bool>ind(2000001);
vector<int>p(2000001,-1);

void dfs(int v,int e=-1){
    p[v]=e;
  for(auto to:g[v]){
    if(to!=e)dfs(to,v);

  }
  if(e!=-1){
  if(!ind[e])leaf[e]--,ind[e]=true;
  leaf[e]+=leaf[v];
  pop[e]+=pop[v];
  mx[e]=max(mx[v],mx[e]);

  }
}
void DFS(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)DFS(to,v);
    }
    if(e!=-1){
        ll lo=mx[e]*leaf[e];
        if(pop[e]>lo){
            ll k=pop[e]-lo;
            if(k%leaf[e])mx[e]++;
            k/=leaf[e];
            mx[e]+=k;
            mx[p[e]]=max(mx[p[e]],mx[e]);
        }
    }
}

int main(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int k;cin>>k;
        g[k].pb(i);
        g[i].pb(k);
    }
    for(int i=1;i<=n;i++){
        cin>>pop[i];
    }
    for(int i=2;i<=n;i++){
        if(g[i].size()==1)mx[i]=pop[i];
    }
    dfs(1);

    DFS(1);
    cout<<mx[1];
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(int m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ll l=0,r=1e12;
    ll ans=0;
    while(l!=r){
        int m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ll>leaf(200001,1);
vector<ll>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ll l=0,r=1e18;
    ll ans=0;
    while(l!=r){
        ll m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;

vector<vector<int> >g(200001);
vector<ull>leaf(200001,1);
vector<ull>po(200001,0);
vector<bool>ind(200001,false);

void dfs(int v,int e=-1){
    for(auto to:g[v]){
        if(to!=e)dfs(to,v);
    }
    if(e!=-1){
        po[e]+=po[v];

        if(!ind[e])leaf[e]--;leaf[e]+=leaf[v],ind[e]=true;
    }
}

bool check(ll m,int n){
    for(int i=1;i<=n;i++){
        if(leaf[i]*m<po[i])return false;
    }
    return true;
}

void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        int p;scanf("%d",&p);
        g[p].pb(i);
        g[i].pb(p);
    }
     for(int i=1;i<=n;i++)scanf("%d",&po[i]);
    dfs(1,-1);

    ull l=0,r=1e15;
    ull ans=0;
    while(l!=r){
        ull m=(l+r+1)/2;
        if(check(m,n))ans=m,r=m-1;
        else l=m;
    }
    cout<<ans;
}
int main(){
solve();}


///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***   ///

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define endl '\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }

int dx[] = { 0, 0, +1, -1, -1  +1, -1, +1 };
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };

const int mx = 2e5+123;
vi adj[mx];
int val;
int a[mx];
bool done = 0;

int dfs (int u, int p)
{
    if ( p != -1 &&  sz(adj[u]) == 1 ) return a[u]-val;

    int ret = a[u];
    for ( auto v : adj[u] ) {
        if ( v != p ) ret += dfs ( v, u );
    }

    if ( ret > 0 ) done = 0;

    return ret;
}

int main()
{
	optimize();

	int n;
	cin >> n;
	for ( int i = 2; i <= n; i++ ) {
        int p;
        cin >> p;
        adj[i].PB ( p );
        adj[p].PB ( i );
	}

	for ( int i = 1; i <= n; i++ ) cin >> a[i];

	int l = 0, r = 2e9, ans = 2e9;

	while ( l <= r ) {
        int mid = ( l + r ) >> 1;

        done = 1;
        val = mid;
        if ( dfs(1, -1) > 0 ) done = 0;
        if ( done ) {
            ans = min ( ans, mid );
            r = mid-1;
        }
        else l = mid+1;
	}

	cout << ans << endl;

	return 0;
}


///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***   ///

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define endl '\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }

int dx[] = { 0, 0, +1, -1, -1  +1, -1, +1 };
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };

const int mx = 2e5+123;
vi adj[mx];
ll val;
int a[mx];
bool done = 0;

ll dfs (int u, int p)
{
    if ( p != -1 &&  sz(adj[u]) == 1 ) return a[u]-val;

    ll ret = a[u];
    for ( auto v : adj[u] ) {
        if ( v != p ) ret += dfs ( v, u );
    }

    if ( ret > 0 ) done = 0;

    return ret;
}

int main()
{
	optimize();

	int n;
	cin >> n;
	for ( int i = 2; i <= n; i++ ) {
        int p;
        cin >> p;
        adj[i].PB ( p );
        adj[p].PB ( i );
	}

	ll sum = 0;
	for ( int i = 1; i <= n; i++ ) {
            cin >> a[i];
            sum += a[i];
	}

	ll l = 0, r = sum, ans = sum;

	while ( l <= r ) {
        ll mid = ( l + r ) >> 1;

        done = 1;
        val = mid;
        if ( dfs(1, -1) > 0 ) done = 0;
        if ( done ) {
            ans = min ( ans, mid );
            r = mid-1;
        }
        else l = mid+1;
	}

	cout << ans << endl;

	return 0;
}


///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***   ///

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define endl '\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }

int dx[] = { 0, 0, +1, -1, -1  +1, -1, +1 };
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };

const int mx = 2e5+123;
vi adj[mx];
ll val;
ll a[mx];
bool done = 0;

ll dfs (int u, int p)
{
    if ( p != -1 &&  sz(adj[u]) == 1 ) {
        if ( a[u] > val ) done = 0;
        return a[u]-val;
    }

    ll ret = a[u];
    for ( auto v : adj[u] ) {
        if ( v != p ) ret += dfs ( v, u );
    }

    if ( ret > 0 ) done = 0;

    return ret;
}

int main()
{
	optimize();

	int n;
	cin >> n;
	for ( int i = 2; i <= n; i++ ) {
        int p;
        cin >> p;
        adj[i].PB ( p );
        adj[p].PB ( i );
	}

	ll sum = 0;
	for ( int i = 1; i <= n; i++ ) {
        cin >> a[i];
        sum += a[i];
	}

	ll l = 0, r = sum, ans = sum;

	if ( sz(adj[1]) > 100 ) {
    done = 1;
    val = 98946768168616;

    cout << dfs(1, -1) << " " << done << endl;

	}
	while ( l <= r ) {
        ll mid = ( l + r ) >> 1;

        done = 1;
        val = mid;
        if ( dfs(1, -1) > 0 ) done = 0;
        if ( done ) {
            ans = min ( ans, mid );
            r = mid-1;
        }
        else l = mid+1;
	}

	cout << ans << endl;

	return 0;
}


///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***   ///

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define endl '\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }

int dx[] = { 0, 0, +1, -1, -1  +1, -1, +1 };
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };

const int mx = 2e5+123;
vi adj[mx];
dl val;
ll a[mx];
bool done = 0;

dl dfs (int u, int p)
{
    if ( p != -1 &&  sz(adj[u]) == 1 ) {
        if ( a[u] > val ) done = 0;
        return a[u]-val;
    }

    dl ret = a[u];
    for ( auto v : adj[u] ) {
        if ( v != p ) ret += dfs ( v, u );
    }

    if ( ret > 0 ) done = 0;

    return ret;
}

int main()
{
	optimize();

	int n;
	cin >> n;
	for ( int i = 2; i <= n; i++ ) {
        int p;
        cin >> p;
        adj[i].PB ( p );
        adj[p].PB ( i );
	}

	ll sum = 0;
	for ( int i = 1; i <= n; i++ ) {
        cin >> a[i];
        sum += a[i];
	}

	ll l = 0, r = sum, ans = sum;

	while ( l <= r ) {
        ll mid = ( l + r ) >> 1;

        done = 1;
        val = mid;
        if ( dfs(1, -1) > 0 ) done = 0;
        if ( done ) {
            ans = min ( ans, mid );
            r = mid-1;
        }
        else l = mid+1;
	}

	cout << ans << endl;

	return 0;
}


///   ***   ---   |||		 In the name of ALLAH		|||   ---   ***   ///

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define endl '\n'
#define PB push_back
#define F first
#define S second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007

#define mem(a,b) memset(a, b, sizeof(a) )
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
//
//debug
template<typename F,typename S>ostream&operator<<(ostream&os,const pair<F,S>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&v){os<<"{";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"}";}
template<typename T>ostream&operator<<(ostream&os,const set<T>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<",";os<<*it;}return os<<"]";}
template<typename T>ostream&operator<<(ostream&os,const multiset<T>&v) {os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<*it;}return os<<"]";}
template<typename F,typename S>ostream&operator<<(ostream&os,const map<F,S>&v){os<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())os<<", ";os<<it->first<<" = "<<it->second;}return os<<"]";}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu(){cerr << endl;}
template<typename T>void faltu(T a[],int n){for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;}
template<typename T,typename...hello>void faltu(T arg,const hello&...rest){cerr<<arg<<' ';faltu(rest...);}
//#else
//#define dbg(args...)

ll gcd ( ll a, ll b ) { return __gcd ( a, b ); }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ); }

int dx[] = { 0, 0, +1, -1, -1  +1, -1, +1 };
int dy[] = { +1, -1, 0, 0, -1, +1, +1, -1 };

const int mx = 2e5+123;
vi adj[mx];
ll val;
ll a[mx];
bool done = 0;

ll dfs (int u, int p)
{
    if ( p != -1 &&  sz(adj[u]) == 1 ) {
        if ( a[u] > val ) done = 0;
        return a[u]-val;
    }

    ll ret = a[u];
    for ( auto v : adj[u] ) {
        if ( v != p ) ret += dfs ( v, u );
    }

    if ( ret > 0 ) done = 0;

    return ret;
}

int main()
{
	optimize();

	int n;
	cin >> n;
	for ( int i = 2; i <= n; i++ ) {
        int p;
        cin >> p;
        adj[i].PB ( p );
        adj[p].PB ( i );
	}

	ll sum = 0;
	for ( int i = 1; i <= n; i++ ) {
        cin >> a[i];
        sum += a[i];
	}

	ll l = 0, r = sum, ans = sum;

	if ( sz(adj[1]) > 10 ) {
    done = 1;
    val = 1982929396;

    cout << dfs(1, -1) << " " << done << endl;

	}
	while ( l <= r ) {
        ll mid = ( l + r ) >> 1;

        done = 1;
        val = mid;
        if ( dfs(1, -1) > 0 ) done = 0;
        if ( done ) {
            ans = min ( ans, mid );
            r = mid-1;
        }
        else l = mid+1;
	}

	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int a[1000001];
bool ip(int x){
    for(int i=2;i*i<=x;i++){
        if(x%i==0)return false;
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(int(sqrt(a[i]))==sqrt(a[i])){
            if(ip(sqrt(a[i])))cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1000005;
bool a[N];
int b[N];
long long c[10005];
int sqrt(long long a){
	int i=1;
	while(i*i!=a){
		if(i*i>a) return 0;
		i++;
	}
	return i;
}
int main(){
	int x,m,n=100000,k=0;
	a[1]=false;
	a[0]=false;
	cin>>m;
	memset(a,1,sizeof(a));
	for(int i=2;i<=n;i++){
		if(a[i]==true){ k++; b[k]=i; }
		for(int j=1;j<=k&&b[j]*i<=n;j++){
			a[b[j]*i]=false;
			if(i%b[j]==0) break;
		}
	}
	for(int i=1;i<=m;i++){
		cin>>c[i];
	}
	for(int i=1;i<=m;i++){
		if(a[sqrt(c[i])]==true && sqrt(c[i])!=0 && c[i]!=1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,p[1000010],x;
string a[2]={"NO\n","YES\n"};
int main(){
	cin>>n;
	for(int i=2;i<=1e6;i++)
		if(!p[i]){
			for(int j=i<<1;j<=1e6;j+=i)
				p[i]=1;
		}
	while(n--){
		cin>>x;
		int c=sqrt(x);
		if(c*c==x)cout<<a[p[c]];
		else cout<<a[0];
	}
}


#include<stdio.h>
#include<iostream>
#include<stack>
#include<vector>
#include<map>
#include<string.h>
#include<set>
#include<algorithm>
#include<list>
#include<queue>
#include<string>
using namespace std;

int main()
{
    int n,i,j,ff;
    cin>>n;
    int a[n];
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    for(i=0;i<n;i++){
        ff=0;
        for(j=2;j*j<=a[i];j++){
            if(a[i]%j==0&&j*j!=a[i]){
                ff=1;
                cout<<"NO"<<endl;
                break;
            }
            if(j*j==a[i]){
                ff=1;
                cout<<"YES"<<endl;
                break;
            }
        }
        if(ff==0) cout<<"NO"<<endl;
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool prime(int a)
{
    long int j;
	if(a==1||a==0)
		return false;
	if(a==2)
		return true;
    for(j=2;j<=sqrt(a)+1;j++)
        if(a%j==0)
            return false;
    return true;
}
int main()
{
    long long int n,j,k,m;
    cin>>n;
    for(j=0;j<n;j++)
    {
        cin>>m;
        if(pow(m,1.0/2)==(float)pow(m,1.0/2)&&prime(pow(m,1.0/2)))
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}


#define _for(i, a, b) for (int i = a; i < b; ++i)
#define _rep(i, a, b) for (int i = a; i <= b; ++i)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
typedef long long LL;
using namespace std;
const LL lim = 1e12;
vector<LL> prim;
bool ntp[1000001];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    for(int i = 2; i <= 1000000; ++i) {
        if( !ntp[i] ) prim.push_back( i );
        for(const auto x:prim) {
            if( x*i > 1000000 ) break;
            ntp[x*i]  = 1;
            if( i%x == 0 ) break;
        }
    }
    for(auto &x:prim) x*=x;
    int n; cin >> n;
    while( n-- ) {
        long long x; cin >> x;
        if( binary_search(prim.begin(), prim.end(), x) )
            cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<cstdio>
#include<cmath>
#define maxn 100000005
bool num[maxn];
int pn=0,n,m,a,prim[5800005];
int main()
{
	long long x;
	int n;
    for(int i=2;i<=maxn;i++){
        if(!num[i]){
        	prim[pn++]=i;
		}
        for(int j=0;j<=pn && i*prim[j]<=maxn;j++){
            num[i*prim[j]]=1;
            if(i%prim[j]==0){
            	break;
			}
        }
    }

    scanf("%d",&n);
    while(n--){
    	scanf("%I64d",&x);
    	if(sqrt(x)*sqrt(x)==x && !num[(int)sqrt(x)]){
    		printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}

}


#include<bits/stdc++.h>
using namespace std;
bool check(int x)
{
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0) return false;
    }
    return true;
}
int main()
{
    long long n,x;
    cin>>n;
    while(n--)
    {
        cin>>x;
        if(sqrt(x)!=(int)sqrt(x)) cout<<"NO"<<endl;
        else
        {
            if(check(sqrt(x))) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool vis[10000005]={false};
int ss[1000000];
long long  a[100005];
int pos=0;
void slove()
{
	int i,j,k;
	for(i=2;i<=10000000;i++)
	{
		if(vis[i]==false) ss[pos++]=i;
		for(j=0;ss[j]<pos&&ss[j]*i<=10000000;j++)
		{
			vis[i*ss[j]]=true;
			if(i%ss[j]==0)
				break;
		}
	}
}

int main()
{
	int t,i;
	cin>>t;
	long long max=0;
	for(int i=0;i<t;i++)
	{
		cin>>a[i];
		if(max<a[i])
			max=a[i];
	}
	slove();
	for(i=0;i<t;i++)
	{
		long t=sqrt(a[i]);
		if(t*t==a[i])
			if(vis[t]==false)
				{
					printf("YES\n");
					continue;
				}
		printf("NO\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool v[10000005],w[10000005];
int prime(int n)
{
	for(int i=2;i<=n;i++)
		v[i]=false;
	for(int i=2;i<=n;i++)
		w[i]=false;
	for(int i=2;i<=n;i++)
	{
		if(v[i]) continue;
		w[i]=true;
		for(int j=i;j<=n/i;j++)
			v[i*j]=true;
	}
}
int main()
{
	int n,m,j,jud;
	prime(1000005);
	cin>>m;
	prime(n);
	for(int i=1;i<=m;i++)
	{
		cin>>j;
		jud=sqrt(j);
        if(w[jud]&&j==jud*jud)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int t,n;
inline bool pd(int x){
    if(x<2) return false;
    for(int i=2;i*i<=x;i++)
        if(x%i==0) return false;
    return true;
}
signed main(){
    scanf("%I64d",&t);
    while(t--){
        scanf("%I64d",&n);
        if(sqrt(n)!=(int)sqrt(n)){
            puts("NO");
            continue;
        }
        else{
            if(pd(sqrt(n))){
                puts("YES");
            }
            else puts("NO");
        }
    }
    return 0;
}


//CF 230B
#include<cmath>
#include<cstdio>
#include<cstring>
#define N 1000005
#define ll long long
using namespace std;
bool isprime[N];
void prime() {
	for(ll i=2; i*i<N; i++) {
		if(!isprime[i]) continue;
		for(ll j=i*i;j<N;j += i) {
			isprime[j] = 0;
		}
	}
}
bool isTprime(ll x) {
	ll tmp = sqrt(x);
	if(tmp*tmp != x) return 0;
	return isprime[tmp];
}
int main() {
	memset(isprime,1,sizeof(isprime));
	isprime[0] = isprime[1] = 0;
	prime();
	int n;
	ll x;
	scanf("%d",&n);
	for(int i=0; i<n; i++) {
		scanf("%lld",&x);
		if(isTprime(x)) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool prime(long long a){
	if(a==1)return 0;
	if(a==2||a==3)return 1;
	if(a%6!=1||a%6!=5)return 0;
	for(long long i=5;i<=sqrt(a);i+=6){
		if(a%i==0||a%(i+2)==0)return 0;
	}
	return 1;
}
int main()
{
	long long n;
	cin>>n;
	long long k;
	for(int i=0;i<n;i++){
		cin>>k;
		if(k==1){
			cout<<"NO";
			continue;
		}
		long long j=sqrt(k);
		if(j==sqrt(k)&&prime(j))cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
namespace IN{
    const int MAX_INPUT = 1000000;
    #define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
    char buf[MAX_INPUT],*p1,*p2;
    template<typename T>inline bool read(T &x) {
        static std::streambuf *inbuf=cin.rdbuf();
        x=0;
        register int f=0,flag=false;
        register char ch=getc();
        while(!isdigit(ch)){
            if (ch=='-') f=1;
        	ch=getc();
        }
        if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
        while(isdigit(ch)) {
            x=x*10+ch-48;
            ch=getc();
        }
        x=f?-x:x;
        return flag;
    }
    template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
       return read(a)&&read(args...);
    }
    #undef getc
}

namespace OUT{
    template<typename T>inline void put(T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top=0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc('\n');
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc('\n');
    }
    inline void putc(const char ch){
        static std::streambuf *outbuf=cout.rdbuf();
        outbuf->sputc(ch);
    }
    inline void putstr(string s){
    	for(register int i=0;i<s.length();i++) putc(s[i]);
	}
    template<typename T>inline void put(const char ch,T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top = 0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc(ch);
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc(ch);
    }
    template<typename T,typename ...Args> inline void put(T a,Args ...args){
        put(a);put(args...);
    }
    template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
        put(ch,a);put(ch,args...);
    }
}
using IN::read;
using OUT::put;
using OUT::putc;
using OUT::putstr;
long long n,x;
bool isPrime(long long n){
	if(n==2||n==3) return 1;
	if(n%6!=1&&n%6!=5||n<2) return 0;
	double n_sqrt=floor(sqrt((double)n));
	for(long long i=5;i<=n_sqrt;i+=6)
	    if(n%(i)==0||n%(i+2)==0) return 0;
    return 1;
}
int main(int argc, char const *argv[]){
	read(n);
	while(n--){
		read(x);
		long long sqrt_x=sqrt((double)x);
		if(sqrt_x*sqrt_x==x&&isPrime(sqrt_x)) putstr("YES\n");
		else putstr("NO\n");
	}
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool vis[1000005]={false};
int ss[1000000];
long long  a[100005];
int pos=0;
void slove()
{
	int i,j,k;
	for(i=2;i<=1000000;i++)
	{
		if(vis[i]==false) ss[pos++]=i;
		for(j=0;ss[j]<pos&&ss[j]*i<=1000000;j++)
		{
			vis[i*ss[j]]=true;
			if(i%ss[j]==0)
				break;
		}
	}
}

int main()
{
	int t,i;
	cin>>t;
	long long max=0;
	for(int i=0;i<t;i++)
	{
		cin>>a[i];
		if(max<a[i])
			max=a[i];
	}
	slove();
	for(i=0;i<t;i++)
	{
		long t=sqrt(a[i]);
		if(t*t==a[i])
			if(vis[t]==false)
				{
					printf("YES\n");
					continue;
				}
		printf("NO\n");
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
long long n,a,b,s;
long long prime(long long c)
{
	for(long long i=2;i<=c;i++)
	{
		if(c%i==0)
		s++;
		if(s>3)
		return s;
	}
	return s;
}
int main()
{
	cin>>n;
	for(long long i=1;i<=n;i++)
	{
		cin>>a;
		b=sqrt(a);
		if(b*b!=a)
		{
			cout<<"NO"<<endl;
		}
		else
		{
			s=1;
			prime(a);
			if(s==3)
			{
				cout<<"YES"<<endl;
			}
			else
			cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool isPrime(int n)
{
	if(n==1) return 0;
	if(n==2||n==3)	return 1;
	if(n%6!=1&&n%6!=5)	return 0;
	for(int i=5;i<=floor(sqrt(n));i+=6)
		if(n%i==0||n%(i+2)==0)	return 0;
	return 1;
}
int main()
{
	int n,m,i;
	cin>>n;
	while(n--)
	{
		cin>>m;
		if(sqrt(m)==(int)sqrt(m)&&isPrime(sqrt(m)))
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read()
{
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
ll n;
bool vis[1000005];
void primes(ll n)
{
	memset(vis,true,sizeof(vis));
	vis[1]=false,vis[2]=true;
	for(ll i=2;i<=n;i++)
	{
		if(vis[i]==false) continue;
		for(int j=i;j<n/i;j++)vis[i*j]=false;
	}
}
int main()
{
	n=read();
	primes(1000005);
	for(ll i=1;i<=n;i++)
	{
		ll a=read();
		ll k=sqrt(a);
		if(k*k!=a)
		{
			cout<<"NO"<<endl;
			continue;
		}
		else if(vis[k]==true&&k*k==a)
		{
			cout<<"YES"<<endl;
			continue;
		}
	}

}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

#define int long long

using namespace std;

inline int read(){
	int sum=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
	return sum*f;
}

int n,tot;
int a[100017];
int prime[1000017];
int isprime[1000017];

void Euler(){
	memset(isprime,1,sizeof isprime);
	for(int i=2;i<=1000000;i++){
		if(isprime[i])
			prime[++tot]=i;
		for(int j=1;j<=tot&&i*prime[j]<=1000000;j++){
			isprime[i*prime[j]]=0;
			if(i%prime[j]==0)break;
		}
	}
}

signed main(){
	n=read(); Euler();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++){
		int tmp=(int)sqrt(a[i]);
		if(isprime[tmp]){
			if(tmp*tmp==a[i])puts("YES");
			else puts("NO");
		}
		else puts("NO");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define INF 17483647
#define M 200005
#define re register
#define ll long long
unsigned ll t,n;
bool prime(unsigned x){
    if(x<2) return 0;
    for(unsigned ll i=2;i*i<=x;i++) if(x%i==0) return 0;
    return 1;
}
signed main(){
    std::ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
    	cin>>n;
    	if(sqrt(n)!=(int)sqrt(n)){
    		printf("NO\n");
    		continue;
    	}
    	else{
    		if(prime(sqrt(n))) printf("YES\n");
    		else printf("NO\n");
    	}
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <string.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define INF 2147483647
#define LLINF 9223372036854775807
typedef long long ll;
using namespace std;
bool a[1500000];
int main(){
	int n;
	cin>>n;
	a[0]=1;
	a[1]=1;
	for(int i=2;i<=1000010;i++){
		if(!a[i]){
			for(int j=i+i;j<=1000010;j+=i){
				a[j]=1;
			}
		}
	}
	rep(i,n){
		ll m;
		cin>>m;
		if(sqrt(m)*sqrt(m)!=m)cout<<"NO"<<endl;
		else if(a[(int)(sqrt(m))])cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
bool isprime[1000005];
void prime(){
    isprime[0]=isprime[1]=1;
    for(int i=2;i<=1000000;i++){
        if(isprime[i])continue;
        for(int j=i*i;j<=1000000;j+=i)isprime[j]=1;
    }
}
signed main(){
    prime();
    int n,t;
    cin>>n;
    while(n--){
        cin>>t;
        int tmp=sqrt(t);
        if(tmp*tmp==t && !isprime[tmp])cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}


#define _for(i, a, b) for (int i = a; i < b; ++i)
#define _rep(i, a, b) for (int i = a; i <= b; ++i)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
typedef long long LL;
using namespace std;
const int C = 45;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long x; int q;
    cin >> q;
    while( q-- ) {
        cin >> x;
        long long sq = sqrt(x);
        if( sq*sq == x ) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<iostream>
#include<memory.h>
#include<cmath>
using namespace std;
typedef long long LL;
bool pr[1000100];

int main(){
    std::ios::sync_with_stdio(false);
    int n;  cin>>n;
    LL  k,q;
    memset(pr,true,sizeof(pr));
    pr[1]=false;
    for(int i=2;i<=1000000;i++)
        if(pr[i])
        for(int j=i*2;j<=1000000;j+=i)
            pr[j]=false;
    for(int i=1;i<=n;i++)
    {
        cin>>k;
        if(k==1)                    cout<<"NO"<<endl;
        else
        {   q=sqrt(k);
            if(q*q==k&&pr[q]==true) cout<<"YES"<<endl;
            else                    cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool prime(int a){
	bool flag=true;
	if(a==1)flag=false;
	if(a==2)flag=true;
	for(int i=3;i<=a;i++){
		if(a%i==0)flag=false;
	}
	return flag;
}
int main()
{
	int n;
	cin>>n;
	int a[n]={0};
	for(int i=0;i<n;i++){
		cin>>a[n];
		int j=sqrt(a[n]);
		if(j*j!=a[n])cout<<"NO";
		if(prime(j))cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<cstdio>
using namespace std;
int main()
{
    long n,x;
    scanf("%lld",&n);
    while(n--)
    {
        scanf("%lld",&x);
        for(long long i=2;i*i<=x;i++)
            if(x%i==0)
            {
                if(i*i==x)printf("YES\n");
                else printf("NO\n");
                break;
            }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int prime[200005],n,ans;
bool vis[1000005];

void ola(void){
         for(int i=2;i<=1000000;i++){
                  if(!vis[i])prime[++ans]=i;
                  for(int j=1;j<=ans;j++){
                           if(i*prime[j]>1000000)break;
                           vis[i*prime[j]]=true;
                           if(i%prime[j]==0)break;
                  }
         }
}

int main(void){
         int a,b;
         double q;
         ola();
         cin >> n;
         for(int i=1;i<=n;i++){
                  cin >> a;
                  q=sqrt(a);
                  if(abs(q-(int)q)<1e-8 && !vis[(int)q]){
                           cout << "YES" << endl;
                  }else{
                           cout << "NO" << endl;
                  }
         }
         return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lng long long
const int N=1e6;
lng n,x;
bool p[N+10]={1,1};
int main(){
	for(lng i=2;i<=sqrt(N);i++){
		if(p[i]) continue;
		for(lng j=i*i;j<=N;j+=i)
			p[j]=1;
	} scanf("%d",&n);
	for(lng i=1;i<=n;i++){
		scanf("%lld",&x);
		lng tmp=sqrt(x);
		if(tmp*tmp==x&&!p[tmp]) puts("YES");
		else puts("NO");
	} return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool prime(int a){
	bool flag=true;
	if(a==1)flag=false;
	for(int i=2;i<a;i++){
		if(a%i==0){
		    flag=false;
		    break;
		}
	}
	return flag;
}
int main()
{
	int n;
	cin>>n;
	long long a[n]={0};
	for(int i=0;i<n;i++){
		cin>>a[n];
		int j=sqrt(a[n]);
		if(j*j!=a[n]){
			cout<<"NO"<<endl;
			continue;
		}
		if((long long)prime(j))cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1000005;
bool a[N];
int b[N],c[10005];
int sqrt(int a){
	int i=1;
	while(i*i!=a){
		if(i*i>a) return 0;
		i++;
	}
	return i;
}
int main(){
	int x,m,n=1000000,k=0;
	a[1]=false;
	a[0]=false;
	cin>>m;
	memset(a,1,sizeof(a));
	for(int i=2;i<=n;i++){
		if(a[i]==true){ k++; b[k]=i; }
		for(int j=1;j<=k&&b[j]*i<=n;j++){
			a[b[j]*i]=false;
			if(i%b[j]==0) break;
		}
	}
	for(int i=1;i<=m;i++){
		cin>>c[i];
	}
	for(int i=1;i<=m;i++){
		if(a[sqrt(c[i])]==true && sqrt(c[i])!=0 && c[i]!=1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
unsigned long long T,n,k;
bool prime(unsigned long long x){
	for(unsigned long long i=2;i<=sqrt(x);i++)
		if(x%i==0) return false;
	return true;
}
int main(){
	cin>>T;
	while(T--){
		cin>>n;
		if(prime(sqrt(n))&&(unsigned long long)sqrt(n)==sqrt(n)) puts("YES");
		else puts("NO");
	}
    return 0;
}


#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
unsigned long long a,b;
string f(unsigned long long x)
{
	if (sqrt(x)*sqrt(x)==x)
	{
		return "YES";
	}
	else return "NO";
}
int main()
{
	cin>>a;
	for (int i=0;i<a;i++)
	{
		cin>>b;
		cout<<f(b)<<endl;
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool ip(long long x){
    for(long long i=2;i*i<=x;i++){
        if(x%i==0)return false;
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        long long x;
        cin>>x;
        if(int(sqrt(x))==sqrt(x)){
            if(ip(sqrt(x)))cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
bool find(int n){
    int sum=0;
    for (int i = 1; i <= n; ++i) {
        if(n%i==0)sum++;
    }
    return sum == 3;
}
int main() {
    long long n,a[100005];
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        cin>>a[i];
        if(find(a[i])) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<cstring>
using namespace std;
bool zs(int x){
	if(x<2)return 0;
	for(int i=2;i*i<=x;i++){
		if(x%i==0)return 0;
	}
	return 1;
}
int main(){
	int n;
	cin>>n;
	long long a;
	long long p;
	for(int i=1;i<=n;i++){
		cin>>a;
		p=sqrt(a);
		if(p*p!=a){
			printf("NO\n");continue;
		}
		if(zs(p)){
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

inline int read(){
	int sum=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
	return sum*f;
}

int n;
int a[100017];

int main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++){
		int tmp=(int)sqrt(a[i]);
		if(tmp&1||tmp==2){
			if(tmp*tmp==a[i])puts("YES");
			else puts("NO");
		}
		else puts("NO");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

inline bool isPrime(int n)
{
    for(int i=2; i*i <= n; ++i)
        if(!n%i)
           return false;
    return true;
}

int main()
{
	int n;
	cin >> n;
	bool ans[n];
	for(int i=0;i<n;i++)
	{
		int x;
		cin >> x;
		if(x>1)
		{
			if(sqrt(x) == int(sqrt(x)) && isPrime(sqrt(x)))
			{
				ans[i]=true;
				continue;
			}
		}
		ans[i]=false;
	}
	for(int i=0;i<n;i++)
		if(ans[i])
			puts("YES");
		else
			puts("NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    long long x;
    while(n--)
    {
        int f=1;
        cin>>x;
        if(x<=1){cout<<"NO"<<endl;continue;}
        long long s=sqrt(x);
        if(s*s!=x){cout<<"NO"<<endl;continue;}
        for(long long i=2;i*i<=s;i++)
        if(s%i==0){cout<<"NO"<<endl;f=0;break;}
        if(f)cout<<"YES"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
	{
	int a;
	cin>>a;
	while(a--)
	{
		int b;
		bool kj=false;
		cin>>b;
		int c=sqrt(b)+0.00001;
		cout<<a<<"."<<sqrt(b);
		if(c*c!=b)
		{
			cout<<"NO\n";

			continue;
		}
		else
			for(int i=2;i*i<=c;i++)
				if(c%i==0)
				{
					cout<<"NO\n";
					kj=true;
					break;
				}
		if(kj==true)
		{
			continue;}
		cout<<"YES\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
bool prime(int a)
{
  if(a==1)return 0;
  if(a==2||a==3)return 1;
  if(a%6!=1&&a%6!=5)return 0;
  for(int i=5; i<=sqrt(a); i+=6)
    if(a%i==0||a%(i+2)==0)return 0;
  return 1;
}
int main()
{
  int n,m;
  cin>>n;
  while(n--)
  {
    cin>>m;
    int n1=(int)sqrt(m);
    if(n1==sqrt(m)&&prime(n1))
    	cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
  }
  return 0;
}


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <string.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define INF 2147483647
#define LLINF 9223372036854775807
typedef long long ll;
using namespace std;
bool a[1500000];
int main(){
	int n;
	cin>>n;
	a[0]=1;
	a[1]=1;
	for(int i=2;i<=1000010;i++){
		if(!a[i]){
			for(int j=i+i;j<=1000010;j+=i){
				a[j]=1;
			}
		}
	}
	rep(i,n){
		ll m;
		cin>>m;
		int t=sqrt(m);
		if(t*t==m&&!a[t])cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cctype>
using namespace std;
#define ll long long
#define MAXM 10001000
int n,cnt;
ll x;
int prime[MAXM];
bool vis[MAXM],check;
int main()
{
	for(int i=2;i<=MAXM;++i)
    {
        if(!vis[i])
		prime[cnt++]=i;
        for(int j=0;j<cnt&&i*prime[j]<=MAXM;++j)
        {
            vis[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld",&x);
		for(int i=0;i<=cnt;++i)
		{
			if(prime[i]*prime[i]==x)
			{
				check=true;
				break;
			}
		}
		if(check) printf("YES\n");
		else printf("NO\n");
		check=false;
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool vis[1000005]={false};
int ss[1000000];
long long  a[100005];
int pos=0;
void slove(int n)
{
	int i,j,k;
	for(i=2;i<=n;i++)
	{
		if(vis[i]==false) ss[pos++]=i;
		for(j=0;ss[j]<pos&&ss[j]*i<=n;j++)
		{
			vis[i*ss[j]]=true;
			if(i%ss[j]==0)
				break;
		}
	}
}

int main()
{
	int t,i;
	cin>>t;
	long long max=0;
	for(int i=0;i<t;i++)
	{
		cin>>a[i];
		if(max<a[i])
			max=a[i];
	}
	max=sqrt(max)+1;
	slove(max);
	for(i=0;i<t;i++)
	{
		long long t=sqrt(a[i]);
		if(a[i]<4)
		{
			printf("NO\n");
			continue;
		}
		if(t*t==a[i])
			if(vis[t]==false)
				{
					printf("YES\n");
					continue;
				}
		printf("NO\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long long x;
    while(n--)
    {
        int f=1;
        cin>>x;
        if(x<=1){cout<<"NO"<<endl;continue;}
        long long s=sqrt(x);
        if(s*s!=x){cout<<"NO"<<endl;continue;}
        for(long long i=2;i*i<=s;i++)
        if(s%i==0){cout<<"NO"<<endl;f=0;break;}
        if(f)cout<<"YES"<<endl;
    }
}


//
#include<bits/stdc++.h>
using namespace std;
bool is_prime(int a)
{
	if(a<2)
		return false;
	for(int i=2;i<a;i++)
	{
		if(a%i==0)
		{
			return false;
		}
		return true;
	}
}
int main()
{
	long long n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		long long a;
		cin>>a;
		int v=floor(sqrt(a)+0.1);
		if(is_prime(v))
		{
			if(v*v==a)
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}


# include <stdio.h>
# include <math.h>
# define MAXN 1000000
int p[MAXN+1]={1,1};
int main()
{
    int i, j, t;
    long long n;
    double temp;
    for(i=2; i<=MAXN; ++i)
        if(!p[i])
        for(j=i+i; j<=MAXN; j+=i)
            p[j] = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d", &n);
        temp = sqrt(n);
        if(temp == (int)temp && !p[(int)temp])
            puts("YES");
        else
            puts("NO");

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,j,k,m;
    float h;
    cin>>n;
    for(j=0;j<n;j++)
    {
        cin>>m;
        k=pow(m);
        h=(float)pow(m);
        if(k==h)
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1000005;
bool a[N];
int b[N],c[N];
int sqrt(int a){
	int i=1;
	while(i*i!=a){
		if(i*i>a) return 0;
		i++;
	}
	return i;
}
int main(){
	int x,m,n=1000000,k=0;
	a[1]=false;
	a[0]=false;
	cin>>m;
	memset(a,1,sizeof(a));
	for(int i=2;i<=n;i++){
		if(a[i]==true){ k++; b[k]=i; }
		for(int j=1;j<=k&&b[j]*i<=n;j++){
			a[b[j]*i]=false;
			if(i%b[j]==0) break;
		}
	}
	for(int i=1;i<=m;i++){
		cin>>c[i];
	}
	for(int i=1;i<=m;i++){
		if(a[sqrt(c[i])]==true && sqrt(c[i])!=0) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

long long n,prime[1000011];
long long m;
long long m1;

int cut(){
	for(int i=2;i<=1000010;i++){
		if(prime[i]==0){
			for(int j=i*2;j<=1000010;j+=i)prime[j]=1;
		}
	}
	return 0;
}

int main(){
	cin >>n;
	cut();
	for(int i=1;i<=n;i++){
		scanf("%lld",&m);
		if(m==1 || m==0){
			cout << "NO"<<endl;
			continue;
		}
		m1=sqrt(m);
		if((long long)m1*(long long)m1!=m)printf("NO\n");
		else if (prime[m1]==0) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool ip(int x){
    if(x==1)return false;
    for(int i=2;i*i<=x;i++){
        if(x%i==0)return false;
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(int(sqrt(x))==sqrt(x)){
            if(ip(sqrt(x)))cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
bool fprime(long long x){
    int cha=0;
    for(int i=1;i<=x;i++){
        if(x%i==0)cha++;
        if(cha>3)return false;
    }
    if(cha!=3)return false;
    return true;
}
long long a[100001];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(fprime(a[i])==true){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
bool vis[1000005]={false};
int ss[1000000];
long long  a[100005];
int pos=0;
void slove(int n)
{
	int i,j,k;
	for(i=2;i<=n;i++)
	{
		if(vis[i]==false) ss[pos++]=i;
		for(j=0;ss[j]<pos&&ss[j]*i<=n;j++)
		{
			vis[i*ss[j]]=true;
			if(i%ss[j]==0)
				break;
		}
	}
}

int main()
{
	int t,i;
	cin>>t;
	long long max=0;
	for(int i=0;i<t;i++)
	{
		cin>>a[i];
		if(max<a[i])
			max=a[i];
	}
	max=sqrt(max)+1;
	slove(max);
	for(i=0;i<t;i++)
	{
		long t=sqrt(a[i]);
		if(a[i]<4)
		{
			printf("NO\n");
			continue;
		}
		if(t*t==a[i])
			if(vis[t]==false)
				{
					printf("YES\n");
					continue;
				}
		printf("NO\n");
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std ;

int main ()
{
    int64_t n,x,r,re,ch=0 ;
    long double rr ;

    string xx[100001] ;
    cin>>n ;

    for (int i=0 ; i<n ; i++)
    {
        cin>>x ;
    if (x==1)
    {
        xx[i]="NO" ;
        continue ;
    }
        ch=0 ;
        rr=sqrt(x) ;
        r=sqrt(x) ;
        if (rr!=r)   {xx[i]="NO" ;}
        else
        {
            for (int j=2 ; j<r ; j++)
            {
                if (r%j==0)
                {
                    ch=1 ;
                    xx[i]="NO" ;
                    break ;
                }
            }
            if (ch==0)
            {
                xx[i]="YES" ;
            }
        }

    //for (int i=0 ; i<n ; i++)
  //  {
        cout<<xx[i]<<endl ;
//    }

    }

}


#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std ;

int main ()
{
    int64_t n,x,r,re,ch=0 ;
    long double rr ;

    string xx[100001] ;
    cin>>n ;

    for (int i=0 ; i<n ; i++)
    {
        cin>>x ;
        if (x==1)
        {
            cout<<"NO"<<endl ;
            continue ;
        }
        ch=0 ;
        rr=sqrt(x) ;
        r=sqrt(x) ;
        if (rr!=r)
        {
            cout<<"NO"<<endl ;
        }
        else
        {
            int u = sqrt (r) ;
            double uu = sqrt (r) ;

            if (u!=uu)
            {
                for (int j=2 ; j<u ; j++)
                {
                    if (r%j==0)
                    {
                        ch=1 ;
                        cout<<"NO"<<endl ;
                        break ;
                    }

                }
            }
            else
            {
                        ch=1 ;
                        cout<<"NO"<<endl ;
            }

            if (ch==0)
            {
                cout<<"YES"<<endl ;
            }
        }

        //for (int i=0 ; i<n ; i++)
        //  {
        // cout<<xx[i]<<endl ;
//    }

    }

}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
using namespace std ;

int main ()
{
    int64_t n,x,r,re,ch=0 ;
    long double rr ;

    string xx[100001] ;
    cin>>n ;

    for (int i=0 ; i<n ; i++)
    {
        cin>>x ;
        if (x==1)
        {
            printf("NO\n") ;
            continue ;
        }
        ch=0 ;
        rr=sqrt(x) ;
        r=sqrt(x) ;
        if (rr!=r)
        {
            printf("NO\n") ;
        }
        else
        {
            int u = sqrt (r) ;
            double uu = sqrt (r) ;
            //cout<< u << uu <<endl ;
            if (u!=uu)
            {
                for (int j=2 ; j<=u ; j++)
                {
                    if (r%j==0)
                    {
                        ch=1 ;
                        printf("NO\n") ;
                        break ;
                    }

                }
            }
            else
            {
                        ch=1 ;
                        printf("NO\n") ;
            }

            if (ch==0)
            {
                printf("YES\n") ;
            }
        }

        //for (int i=0 ; i<n ; i++)
        //  {
        // cout<<xx[i]<<endl ;
//    }

    }

}