#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> color;
vector<vector<int> > adj;
vector<map<int, int> > dp;
vector<int> total;
vector<int> parent;
void dfs1(int curr, int p) {
  parent[curr] = p;
  total[curr] = color[curr];
  for (int i = 0; i < adj[curr].size(); ++i) {
    if (adj[curr][i] == p) continue;
    dfs1(adj[curr][i], curr);
    dp[curr][adj[curr][i]] =
        max(total[adj[curr][i]] + color[curr], color[curr]);
    total[curr] += dp[curr][adj[curr][i]] - color[curr];
  }
}
void dfs2(int curr, int p) {
  if (p != -1) {
    dp[curr][p] =
        max(total[p] - dp[p][curr] + color[p] + color[curr], color[curr]);
    total[curr] += dp[curr][p] - color[curr];
  }
  for (int i = 0; i < adj[curr].size(); ++i) {
    if (adj[curr][i] == p) continue;
    dfs2(adj[curr][i], curr);
  }
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> n;
  color.resize(n);
  parent.resize(n);
  dp.resize(n);
  total.assign(n, 0);
  for (int i = 0; i < n; ++i) {
    int co;
    cin >> co;
    color[i] = 2 * co - 1;
  }
  adj.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
    dp[u][v] = -1;
    dp[v][u] = -1;
  }
  dfs1(0, -1);
  dfs2(0, -1);
  for (int i = 0; i < n; ++i) {
    cout << total[i] << " ";
  }
  cout << endl;
}
