#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
typedef pair<int, bool> sP;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);

int n;
const int mn = 300000;
int a[mn];
vector<int> G[mn];

int sz;
bool mp[mn];
int num[mn];
int g;
void dfs2(int id, int fr) {
	num[id] = 1;
	bool f = true;
	rep(j, G[id].size()) {
		int to = G[id][j];
		if (to == fr || !mp[to])continue;
		dfs2(to, id);
		num[id] += num[to];
		if (num[to] > sz / 2)f = false;
	}
	if (sz - num[id] > sz / 2)f = false;
	if (f)g = id;
}

map<P, sP> memo;
int out[mn];


int c[mn];
sP dfs(int id, int fr) {
	if (c[id] >= a[id]) {
		return { 0,false };
	}
	c[id]++;
	sP ret = { 1,true };
	rep(j, G[id].size()) {
		int to = G[id][j];
		if (to == fr)continue;
		if (!mp[to]) {
			sP p = memo[{to, id}];
			ret.first += p.first;
			ret.second = p.second;
			if (p.second == false) {
				return ret;
			}
		}
		else {
			sP p = dfs(to, id);
			ret.first += p.first;
			ret.second = p.second;
			if (p.second == false) {
				return ret;
			}
		}
		if (c[id] >= a[id])return { ret.first,false };
		c[id]++; ret.first++;
	}
	return ret;
}


vector<int> nex;
void tonex(int id, int fr) {
	nex.push_back(id);
	rep(j, G[id].size()) {
		int to = G[id][j];
		if (to == fr)continue;
		if (!mp[to])continue;
		tonex(to, id);
	}
}
queue<vector<int>> que;
void ans(vector<int> &v) {
	if (v.empty())return;
	sz = v.size();
	rep(i, v.size())mp[v[i]] = true;
	int root = v[0];
	g = root;
	dfs2(root, -1);
	root = g;
	rep(i, v.size())c[v[i]] = 0;
	bool exifalse = false;
	vector<sP> vp;
	vector<int> ids;
	rep(j, G[root].size()) {
		int to = G[root][j];
		sP p;
		if (mp[to]) {
			p = dfs(to, root);
			if (p.second == false && j<a[root]) {
				exifalse = true;
			}
			vp.push_back(p);
			ids.push_back(to);
		}
		else {
			p = memo[{to, root}];
			if (p.second == false && j<a[root]) {
				exifalse = true;
			}
			vp.push_back(p);
			ids.push_back(to);
		}
		//cout << root << " !!! " << to << " " << p.first << endl;
	}
	vector<int> rp(vp.size() + 1);
	rep(i, vp.size()) {
		rp[i + 1] = rp[i] + vp[i].first;
	}
	/*int chk = vp.size();
	if (!exifalse) {
	chk = min(chk, a[root]);
	}
	else {
	rep(i, vp.size()) {
	if (vp[i].second == false) {
	chk = i + 1; break;
	}
	}
	}*/
	/*Rep(i, chk, vp.size()) {
	int to = ids[i];
	if (mp[to]) {
	memo[{root, to}] = { rp[chk] + chk,false };
	}
	}*/
	out[root]++;
	if (a[root] == 0) {
		out[root] = 0;
		rep(i, vp.size()) {
			int to = ids[i];
			memo[{root, to}] = { 0,false };
		}
	}
	else if (!exifalse) {
		//cout << "Hello!" << endl;
		c[root] = 1;
		int chk = vp.size();
		rep(i, vp.size()) {
			//cout <<"hello "<< vp[i].first << endl;
			if (vp[i].second == false) {
				out[root] += vp[i].first;
				chk = i;
				break;
			}
			else {
				out[root] += vp[i].first;
				if (c[root] >= a[root]) {
					chk = i + 1;
					break;
				}
				c[root]++;
				out[root]++;
			}
		}
		//cout << "uoaaaa " << chk << endl;
		Rep(i, chk, vp.size()) {
			int cur = ids[i];
			if (mp[cur]) {
				//cout << root << " " << cur << " " << rp[chk] + chk << endl;
				memo[{root, cur}] = { rp[chk] + chk,false };
			}
		}
		if (chk == vp.size()) {
			rep(i, chk) {
				int to = ids[i];
				if (!mp[to])continue;
				int sum = rp[vp.size()] - vp[i].first;
				//cout << root << " " << to << " " << sum + chk << endl;
				memo[{root, to}] = { sum + chk,true };
			}
		}
		else {
			rep(i, chk) {
				int to = ids[i];
				if (!mp[to])continue;
				int sum = rp[chk + 1] - vp[i].first;
				//cout << root << " " << to << " " << sum + chk << endl;
				memo[{root, to}] = { sum + chk,false };
			}
		}
	}
	else {
		//cout << "aaaa" << endl;
		c[root] = 1;
		rep(i, vp.size()) {
			//cout << root << " ?? " << vp[i].first << ids[i]<<endl;
			//cout << vp[i].first << endl;
			if (vp[i].second == false) {
				out[root] += vp[i].first;
				//chk = i;
				break;
			}
			else {
				out[root] += vp[i].first;
				c[root]++;
				out[root]++;
			}
		}
		int chk = vp.size();
		rep(i, vp.size()) {
			if (vp[i].second == false) {
				chk = i; break;
			}
		}
		//cout << root << " " << ids[chk] << endl;
		Rep(i, chk + 1, vp.size()) {
			int cur = ids[i];
			if (!mp[cur])continue;
			//cout << root << " " << cur << " " << rp[chk+1] + chk+1 << endl;
			memo[{root, cur}] = { rp[chk + 1] + chk + 1,false };
		}
		rep(i, chk) {
			int to = ids[i];
			if (!mp[to])continue;
			int sum = rp[chk + 1] - vp[i].first;
			//cout << root << " " << to << " " << sum + chk << endl;
			memo[{root, to}] = { sum + chk,false };
		}
		c[root] = 1;
		int al = 1; bool va = true;
		rep(i, vp.size()) {
			if (i == chk)continue;
			if (vp[i].second == false) {
				al += vp[i].first;
				va = false;
				//chk = i;
				break;
			}
			else {
				al += vp[i].first;
				if (c[root] >= a[root]) {
					va = false; break;
				}
				c[root]++;
				al++;
			}
		}
		//cout << root << " " << ids[chk] << " " << al << endl;
		memo[{root, ids[chk]}] = { al,va };
	}
	rep(j, G[root].size()) {
		int to = G[root][j];
		if (!mp[to])continue;
		nex.clear();
		tonex(to, root);
		if (nex.size())que.push(nex);
	}
	rep(i, v.size())mp[v[i]] = false;
}

void solve() {
	cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	rep(i, n - 1) {
		int u, v; cin >> u >> v; u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<int> ori;
	rep(i, n) {
		sort(G[i].begin(), G[i].end());
		ori.push_back(i);
	}
	que.push(ori);
	while (!que.empty()) {
		vector<int> v = que.front(); que.pop();
		ans(v);
		//cout << "??" << endl;
	}
	rep(i, n) {
		cout << out[i] << endl;
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	//stop
	return 0;
}







Measured memory_bytes: 66456