#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 100;
const int MOD = 1000000007;
const double eps = 1e-8;
const double PI = acos(-1.0);
vector<int> g[200005];
int clr[200005];
int f[200005], ans[200005];
int vis[200005];
void dfs(int u, int pa) {
  if (clr[u] == 1) {
    f[u]++;
  } else {
    f[u]--;
  }
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == pa) {
      continue;
    }
    dfs(v, u);
    f[u] += max(f[v], 0);
  }
}
void bfs() {
  queue<int> q;
  ans[1] = f[1];
  vis[1] = 1;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (vis[v]) {
        continue;
      }
      ans[v] = f[v] + max(0, ans[u] - max(0, f[v]));
      vis[v] = 1;
      q.push(v);
    }
  }
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &clr[i]);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs(1, 0);
  bfs();
  for (int i = 1; i <= n; i++) {
    printf("%d%c", ans[i], i == n ? '\n' : ' ');
  }
  return 0;
}
