/*
 *  [Ctsc2010]星际旅行.cpp
 *
 *  Created on: 2011-4-19
 *      Author: user
 */

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <vector>
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
using namespace std;

const int MAX_N_VERTEXS = 50000 + 10;

struct Vertex {
	vector<Vertex*> adj;
	int H;
	int am[2][2];//normal ,if inc?
	int chAmSum[2];
	int ans;
	int dist;//dist from root to it
	int am2[2];//it's father chose or not/outside of subtree and it's ancs are inced
	Vertex*father;
};

Vertex vs[MAX_N_VERTEXS];
int nVs;

Vertex*que[MAX_N_VERTEXS];

void readInput() {
	cin >> nVs;
	for (int i = 0; i < nVs; ++i) {
		scanf("%d", &vs[i].H);
	}
	for (int i = 0; i < nVs - 1; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		Vertex*u = vs + a, *v = vs + b;
		u->adj.push_back(v);
		v->adj.push_back(u);
	}
}

void doBFS(Vertex*root) {
	int qh = 0, qt = 0;
	que[qt++] = root;
	root->father = 0;
	root->dist = 0;
	for (; qh < qt;) {
		Vertex*u = que[qh++];
		foreach(iter,u->adj) {
			Vertex*v = *iter;
			if (v == u->father)
				continue;
			que[qt++] = v;
			v->father = u;
			v->dist = u->dist + 1;
		}
	}
}
void work() {
	Vertex*root = vs + 0;
	doBFS(root);

	//calc am
	for (int at = nVs - 1; at >= 0; --at) {
		Vertex*u = que[at];
		for (int cf = 0; cf < 2; ++cf) {
			for (int inc = 0; inc < 2; ++inc) {
				int val = u->H + inc;
				int&ret = u->am[cf][inc];
				ret = INT_MAX;
				for (int cme = 0; cme < 2; ++cme) {
					if (!cf && !cme)
						continue;
					int tmp = cme ? val : 0;
					foreach(iter,u->adj) {
						Vertex*v = *iter;
						if (v == u->father)
							continue;
						tmp += v->am[cme][0];
					}
					ret = min(ret, tmp);
				}
			}
		}
		for (int c = 0; c < 2; ++c) {
			u->chAmSum[c] = 0;
			foreach(iter,u->adj) {
				Vertex*v = *iter;
				if (v == u->father)
					continue;
				u->chAmSum[c] += v->am[c][0];
			}
		}
	}

	//calc am2
	root->am2[0] = 0;
	root->am2[1] = 0;
	for (int i = 1; i < nVs; ++i) {
		Vertex*u = que[i];
		for (int cf = 0; cf < 2; ++cf) {
			int&ret = u->am2[cf];
			ret = INT_MAX;
			for (int cg = 0; cg < 2; ++cg) {
				if (!cg && !cf)
					continue;
				int tmp = 0;
				if (cf) {
					if (u->father != vs)
						tmp += u->father->H + 1;
					else
						tmp += u->father->H;
				}
				tmp += u->father->am2[cg];
				tmp += u->father->chAmSum[cf];
				tmp -= u->am[cf][0];
				ret = min(ret, tmp);
			}
		}
		//		cout << u->am2[0] << " " << u->am2[1] << endl;
	}

	//calc ans
	for (int i = 0; i < nVs; ++i) {
		Vertex*u = que[i];
		int&ret = u->ans;
		ret = INT_MAX;
		for (int cf = 0; cf < 2; ++cf) {
			int tmp = 0;
			tmp += u->am2[cf];
			if (u == vs)
				tmp += u->am[cf][0];
			else
				tmp += u->am[cf][1];
			ret = min(ret, tmp);
		}
	}

	//output
	for (int i = 0; i < nVs; ++i) {
		int ans = vs[i].ans;
		ans += nVs - 1;
		ans *= 2;
		ans -= vs[i].dist;
		printf("%d\n", ans);
	}
}

void prepare() {
	for (int i = 0; i < nVs; ++i) {
		vs[i].H -= vs[i].adj.size();
	}
}

void solve() {
	readInput();
	prepare();
	work();
}

int main() {
	solve();
}
