#include <bits/stdc++.h>
using std::max;
using std::vector;
const int kINF = 1e9 + 7;
const int kMaxN = 2e5 + 5;
vector<int> G[kMaxN];
int n, a[kMaxN];
int u, v;
int f[kMaxN], g[kMaxN];
void AddEdge(int u, int v) { G[u].push_back(v), G[v].push_back(u); }
void DFS1(int k, int fa) {
  f[k] = 0;
  for (auto to : G[k]) {
    if (to == fa) continue;
    DFS1(to, k);
    f[k] += max(0, f[to]);
  }
  if (a[k])
    ++f[k];
  else
    --f[k];
}
void DFS2(int k, int fa) {
  for (auto to : G[k]) {
    if (to == fa) continue;
    g[to] = f[to] + max(0, g[k] - max(0, f[to]));
    DFS2(to, k);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    AddEdge(u, v);
  }
  DFS1(1, 0);
  g[1] = f[1];
  DFS2(1, 0);
  for (int i = 1; i <= n; ++i) printf("%d ", g[i]);
  return 0;
}
