#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n";
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define tr(cont,it) for(decltype((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
const ll mod = 1000000007;
const ll inf = (ll)1e15;
ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = (x*x)%m; if(b % 2) x = (x*a)%m; return x;}
const int N=5e5+5;
ll c[N],dp[N];
vector<int> adj[N];
int n;
ll sbts[N];
void dfs(int v,int p=-1)
{
for(int u : adj[v])
{
if(u!=p)
{
dfs(u,v);
sbts[v]+=(sbts[u]+1);
}
}
}
bool cmp(int n1,int n2)
{
return ((2*sbts[n1]-dp[n1])<(2*sbts[n2]-dp[n2])); //returns true if a is ahead of b
}
void dfs2(int v,int p=-1)
{
for(int u : adj[v])
{
if(u!=p)
{
dfs2(u,v);
}
}
sort(adj[v].begin(),adj[v].end(),cmp);
dp[v]=c[v];
ll add=0;
for(int u : adj[v])
{
if(u==p)
continue;
dp[v]=max(dp[v],dp[u]+add+1);
add+=(sbts[u]);
}
}
ll ans=0,trav=0;
void dfs3(int v,int p=-1)
{
for(int u : adj[v])
{
if(u!=p)
{
trav++;
ans=max(ans,trav+c[u]);
dfs3(u,v);
trav++;
}
}
}
void solve()
{
cin >> n;
for(int i=1;i<=n;i++)
cin >> c[i];
for(int i=1;i<n;i++)
{
int ui,vi;
cin >> ui >> vi;
adj[ui].pb(vi);
adj[vi].pb(ui);
}
dfs(1);
for(int i=1;i<=n;i++)
sbts[i]*=2;
dfs2(1);
dfs3(1);
ans=max(ans,trav+c[1]);
cout << ans << endl;
}
int main()
{
IOS;
int t = 1, num = 1; ///// change this t for number of testcase globally
while (t--)
{
solve();
}
return 0;
}
CiNpbm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