#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 int N = 4e6 + 5, V = 1e6;
vector<int>g[N];
int a[N], d[N];
int ls[N << 2], rs[N << 2], cnt[N << 2], rt[N];
ll s[N << 2], ans[N];
int n, tot;
void dlt(int p)
{
ls[p] = rs[p] = s[p] = cnt[p] = 0;
}
void push_up(int 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(int u, int l, int r, int p)
{
if(!p)p = ++tot;
if(l == r)
{
cnt[p] ^= 1;
s[p] = 1ll * cnt[p] * l * l;
return p;
}
int mid = (l + r) / 2;
if(u <= mid)
{
ls[p] = add(u, l, mid, ls[p]);
}
else
{
rs[p] = add(u, mid + 1, r, rs[p]);
}
push_up(p);
return p;
}
int merge(int u, int y, int l, int r)
{
if(!u || !y)return u | y;
if(l == r)
{
cnt[u] ^= cnt[y];
s[u] = 1ll * cnt[u] * l * l;
dlt(y);
return u;
}
int mid = (l + r) / 2;
ls[u] = merge(ls[u], ls[y], l, mid);
rs[u] = merge(rs[u], rs[y], mid + 1, r);
push_up(u);
dlt(y);
return u;
}
void solve(int u)
{
if(g[u].empty())//叶子节点
{
rt[u] = add(a[u], 1, V, rt[u]);
return;
}
if(g[u].size() != 2)exit(-1);
int 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(int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] /= 2;
}
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
g[n + i].push_back(u);
g[n + i].push_back(v);
d[u]++;
d[v]++;
}
for(int i = n + 1; i < n * 2; i++)
{
if(!d[i])//根节点
{
dfs(i);
solve(i);
}
}
for(int i = n + 1; i < (n * 2); i++)
{
printf("%lld\n", ans[i]);
}
return 0;
}