#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5;
long long a[MAX], dp[MAX];
vector<long long> v[MAX];
long long ans[MAX];
long long n, x, y;
void Build(long long m) {
  for (long long i = 1; i <= m; i++) cin >> a[i], a[i] = (a[i] == 0 ? -1 : 1);
  long long t = m - 1;
  while (t--) cin >> x >> y, v[x].push_back(y), v[y].push_back(x);
  return;
}
long long Root() {
  for (long long i = 1; i <= n; i++)
    if (v[i].size() == 1) return i;
}
void Dfs(long long cur, long long last) {
  dp[cur] = a[cur];
  for (auto now : v[cur]) {
    if (now == last) continue;
    Dfs(now, cur);
    dp[cur] += max(0ll, dp[now]);
  }
  return;
}
void Swap(long long cur, long long last) {
  dp[last] -= max(0ll, dp[cur]);
  dp[cur] += max(0ll, dp[last]);
}
void Reroot(long long cur, long long last) {
  ans[cur] = dp[cur];
  for (auto now : v[cur]) {
    if (now == last) continue;
    Swap(now, cur);
    Reroot(now, cur);
    Swap(cur, now);
  }
}
int main() {
  cin >> n;
  Build(n);
  long long root = Root();
  Dfs(root, root);
  Reroot(root, root);
  for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
