#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
const int N = 200005;
vector<long long> v[N];
long long val[N];
long long dp[N];
void dfs(long long node, long long par) {
  dp[node] = ((val[node]) ? 1 : -1);
  for (auto i : v[node]) {
    if (i == par) continue;
    dfs(i, node);
    if (dp[i] > 0) dp[node] += dp[i];
  }
}
void dfs2(long long node, long long par) {
  for (auto i : v[node]) {
    if (i == par) continue;
    if (dp[i] > 0 && (dp[node] - dp[i]) > 0)
      dp[i] += dp[node] - dp[i];
    else if (dp[i] <= 0 && dp[node] > 0)
      dp[i] += dp[node];
    dfs2(i, node);
  }
}
void solve() {
  long long n, x, y;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> val[i];
  for (long long i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (long long i = 1; i <= n; i++) cout << dp[i] << " ";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
