#include <bits/stdc++.h>
using namespace std;
int c[2 * 100005];
int n;
int tmp[2 * 100005];
int ans[2 * 100005];
void dfs1(vector<vector<int> >& v, int u, int p) {
  tmp[u] = c[u];
  for (int i = 0; i < v[u].size(); i++) {
    int ch = v[u][i];
    if (ch != p) {
      dfs1(v, ch, u);
      if (tmp[ch] > 0) tmp[u] += tmp[ch];
    }
  }
}
void dfs2(vector<vector<int> >& v, int u, int p) {
  ans[u] = tmp[u];
  if (p != -1) {
    if (tmp[u] <= 0 && ans[p] > 0)
      ans[u] = tmp[u] + ans[p];
    else if (tmp[u] > 0 && ans[p] > 0)
      ans[u] = max(ans[p], ans[u]);
  }
  for (int i = 0; i < v[u].size(); i++) {
    int ch = v[u][i];
    if (ch != p) dfs2(v, ch, u);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  vector<vector<int> > adj(n + 1);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    if (a == 1)
      c[i] = 1;
    else
      c[i] = -1;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs1(adj, 1, -1);
  dfs2(adj, 1, -1);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
