#include <bits/stdc++.h>
using namespace std;
int n, a[200005], pnt[400005], nxt[400005], head[200005], E = 0;
long long ans[200005], sum[200005];
void add_edge(int u, int v) {
  pnt[E] = v;
  nxt[E] = head[u];
  head[u] = E++;
}
void dfs(int u, int pre) {
  sum[u] += a[u];
  for (int i = head[u]; i != -1; i = nxt[i]) {
    int v = pnt[i];
    if (v == pre) continue;
    dfs(v, u);
    sum[u] += max(sum[v], 0LL);
  }
}
void dfs2(int u, int pre, long long maxn) {
  maxn = max(maxn, 0LL);
  maxn += sum[u];
  ans[u] = max(ans[u], maxn);
  for (int i = head[u]; i != -1; i = nxt[i]) {
    int v = pnt[i];
    if (v == pre) continue;
    dfs2(v, u, maxn - max(0LL, sum[v]));
  }
}
int main() {
  memset(ans, -0x3f, sizeof(ans));
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) {
      a[i]--;
    }
  }
  dfs(1, 0);
  dfs2(1, 0, -100000);
  for (int i = 1; i <= n; i++) {
    printf("%lld ", ans[i]);
  }
  return 0;
}
