#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e15
inline ll read()
{
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9')
{
if(c == '-')
{
f = -1;
}
c = getchar();
}
while(c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
const ll N = 4e6 + 5, V = 1e6;
vector<ll>g[N];
ll a[N], d[N];
ll ls[N << 2], rs[N << 2], cnt[N << 2], rt[N];
ll s[N << 2], ans[N];
ll n, tot;
void dlt(ll p)
{
ls[p] = rs[p] = s[p] = cnt[p] = 0;
}
void push_up(ll p)
{
cnt[p] = cnt[ls[p]] + cnt[rs[p]];
if(cnt[rs[p]] % 2 == 1)
{
s[p] = s[rs[p]] - s[ls[p]];
}
else
{
s[p] = s[rs[p]] + s[ls[p]];
}
}
ll add(ll u, ll l, ll r, ll id)
{
if(!id)
{
id = ++tot;
}
if(l == r)
{
cnt[id] ^= 1;
s[id] = 1ll * cnt[id] * l * l;
return id;
}
ll mid = (l + r) / 2;
if(u <= mid)
{
ls[id] = add(u, l, mid, ls[id]);
}
else
{
rs[id] = add(u, mid + 1, r, rs[id]);
}
push_up(id);
return id;
}
ll merge(ll a, ll b, ll l, ll r)
{
if(!a)
{
return b;
}
if(!b)
{
return a;
}
if(l == r)
{
cnt[a] ^= cnt[b];
s[a] = 1ll * cnt[a] * l * l;
dlt(b);
return a;
}
ll mid = (l + r) / 2;
ls[a] = merge(ls[a], ls[b], l, mid);
rs[a] = merge(rs[a], rs[b], mid + 1, r);
push_up(a);
dlt(b);
return a;
}
void solve(ll u)
{
if(g[u].empty())//叶子节点
{
rt[u] = add(a[u], 1, V, rt[u]);
return;
}
ll v1 = g[u][0], v2 = g[u][1];
solve(v1);
solve(v2);
ll k1 = s[rt[v1]];
ll k2 = s[rt[v2]];
rt[u] = merge(rt[v1], rt[v2], 1, V);
ll k3 = s[rt[u]];
ans[u] = (k1 + k2 - k3) / 2 * 4;
}
int main()
{
cin >> n;
for(ll i = 1; i <= n; i++)
{
cin >> a[i];
a[i] /= 2;
}
for(ll i = 1; i < n; i++)
{
ll u, v;
cin >> u >> v;
g[n + i].push_back(u);
g[n + i].push_back(v);
d[u]++;
d[v]++;
}
ll root = -1;
for(ll i = n + 1; i < n * 2; i++)
{
if(!d[i])//根节点
{
root = i;
break;
}
}
solve(root);
for(ll i = n + 1; i < (n * 2); i++)
{
prllf("%lld\n", ans[i]);
}
return 0;
}