#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-16;
const int MOD = (int)1e9 + 9;
const long long INF = (int)1e9 + 10;
const int p = 31;
template <typename T>
vector<T> readVector0(int n) {
  vector<T> res(n);
  for (int i = 0; i < n; i++) cin >> res[i];
  return res;
}
template <typename T>
vector<T> readVector1(int n) {
  vector<T> res(n + 1);
  for (int i = 1; i <= n; i++) cin >> res[i];
  return res;
}
inline int mod(int first, int m = MOD) {
  if (first < 0) first += m;
  return (first % m);
}
template <typename T>
bool sortBy2(const pair<T, T> &a, const pair<T, T> &b) {
  return (a.second < b.second);
}
int n;
vector<int> a;
vector<vector<int> > g;
vector<int> df;
vector<int> df2;
void dfs(int v, int p) {
  for (auto u : g[v]) {
    if (u != p) {
      dfs(u, v);
      if (df[u] > 0) df[v] += df[u];
    }
  }
  df[v] += (a[v] ? 1 : -1);
}
void dfs2(int v, int p) {
  if (p == -1)
    df2[v] = df[v];
  else {
    if (df[v] >= 0)
      df2[v] = max(df2[p], df[v]);
    else
      df2[v] = max(df2[p] + df[v], df[v]);
  }
  for (auto u : g[v]) {
    if (u != p) {
      dfs2(u, v);
    }
  }
}
int main() {
  cin >> n;
  a = vector<int>(n);
  g = vector<vector<int> >(n);
  df = vector<int>(n, 0);
  df2 = vector<int>(n, 0);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int v, u;
  for (int i = 0; i < n - 1; ++i) {
    cin >> v >> u;
    g[v - 1].push_back(u - 1);
    g[u - 1].push_back(v - 1);
  }
  dfs(0, -1);
  dfs2(0, -1);
  for (int i = 0; i < n; ++i) cout << df2[i] << " ";
  return 0;
}
