#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int n, first[MAX], nextt[MAX * 2], u[MAX * 2], v[MAX * 2], cnt, white, black;
int ans[MAX], father[MAX], tem[MAX];
bool col[MAX];
void add(int a, int b) {
  u[cnt] = a, v[cnt] = b;
  nextt[cnt] = first[u[cnt]];
  first[u[cnt]] = cnt;
  ++cnt;
}
void dfs1(int dot, int fa) {
  father[dot] = fa;
  col[dot] ? ++tem[dot] : --tem[dot];
  for (int num = first[dot]; num != -1; num = nextt[num]) {
    if (v[num] == fa) continue;
    dfs1(v[num], dot);
    if (tem[v[num]] > 0) tem[dot] += tem[v[num]];
  }
}
void dfs2(int dot, int fa) {
  for (int num = first[dot]; num != -1; num = nextt[num]) {
    if (v[num] == fa) continue;
    int nn = ans[dot];
    if (tem[v[num]] > 0) nn -= tem[v[num]];
    if (nn > 0) ans[v[num]] += nn;
    dfs2(v[num], dot);
  }
}
void solve() {
  memset(first, -1, sizeof(first));
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &col[i]), col[i] ? ++white : black;
  for (int i = 1; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  dfs1(1, 0);
  for (int i = 1; i <= n; ++i) ans[i] = tem[i];
  dfs2(1, 0);
  for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
}
int main(void) {
  solve();
  return 0;
}
