#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 200002;
int n;
int v[N_MAX];
vector<int> edges[N_MAX];
int dpDown[N_MAX], dp[N_MAX];
void dfsDown(int node, int parent = -1) {
  dpDown[node] = v[node];
  for (int u : edges[node])
    if (u != parent) {
      dfsDown(u, node);
      dpDown[node] += max(0, dpDown[u]);
    }
}
void dfs(int node, int parent = -1) {
  if (parent == -1)
    dp[node] = dpDown[node];
  else
    dp[node] = dpDown[node] + max(0, dp[parent] - max(0, dpDown[node]));
  for (int u : edges[node])
    if (u != parent) dfs(u, node);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    if (v[i] == 1)
      v[i] = 1;
    else
      v[i] = -1;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  dfsDown(1);
  dfs(1);
  for (int i = 1; i <= n; i++) cout << dp[i] << " ";
  cout << "\n";
  return 0;
}
