#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const double pi = 3.14159265358979323846264338;
inline long long multi(long long x, long long y) {
  long long tmp =
      (x * y -
       (long long)((long double)x / 998244353 * y + 1.0e-8) * 998244353);
  return tmp < 0 ? tmp + 998244353 : tmp;
}
inline void Mod(long long &x) {
  static int mod = 998244353;
  if (x >= mod) x -= mod;
}
long long inv(long long a) {
  return a == 1 ? 1
                : 1LL * (998244353 - 998244353 / a) * inv(998244353 % a) %
                      998244353;
}
vector<int> a;
vector<int> dp;
vector<int> ans;
vector<vector<int> > g;
void dfs(int v, int pre = -1) {
  dp[v] = a[v];
  for (auto to : g[v]) {
    if (to == pre) continue;
    dfs(to, v);
    dp[v] += max(dp[to], 0);
  }
}
void dfs2(int v, int pre = -1) {
  ans[v] = dp[v];
  for (auto to : g[v]) {
    if (to == pre) continue;
    dp[to] += max(0, dp[v] - max(0, dp[to]));
    dfs2(to, v);
  }
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  a = dp = ans = vector<int>(n);
  g = vector<vector<int> >(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0);
  dfs2(0);
  for (auto it : ans) cout << it << " ";
  cout << "\n";
  return 0;
}
