#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
vector<int> g[MAXN];
int a[MAXN];
int ans[MAXN];
int n;
void dfs1(int s, int p) {
  for (int x : g[s]) {
    if (x != p) {
      dfs1(x, s);
      if (ans[x] > 0) ans[s] += ans[x];
    }
  }
  if (a[s])
    ans[s]++;
  else
    ans[s]--;
}
void dfs2(int s, int p) {
  if (p != -1) {
    int x = ans[p];
    if (ans[s] > 0) x -= ans[s];
    if (x > 0) ans[s] += x;
  }
  for (auto x : g[s]) {
    if (x != p) {
      dfs2(x, s);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 1; i <= n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(0, -1);
  dfs2(0, -1);
  for (int i = 0; i < n; ++i) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
