#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int n;
const int N = 2e5 + 5;
vector<int> a(N);
vector<int> adj[N];
vector<int> dp(N);
vector<int> ans(N);
void dfs(int v, int pv) {
  int sum = 0;
  for (auto u : adj[v]) {
    if (u == pv) continue;
    dfs(u, v);
    sum += max(0, dp[u]);
  }
  dp[v] = a[v] + sum;
}
void dfs2(int v, int pv) {
  ans[v] = dp[v];
  for (auto u : adj[v]) {
    if (u == pv) continue;
    dp[v] -= max(0, dp[u]);
    dp[u] += max(0, dp[v]);
    dfs2(u, v);
    dp[u] -= max(0, dp[v]);
    dp[v] += max(0, dp[u]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (long long i = 1; i <= (n); ++i) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (long long i = 1; i <= (n - 1); ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (long long i = 1; i <= (n); ++i) {
    cout << ans[i] << " ";
  }
  cout << '\n';
  return 0;
}
