#include <bits/stdc++.h>
using namespace std;
long long int n, a[200100], u, v, cnt[200100], sum[200100];
vector<long long int> lst[200100], dp[200100];
long long int res(long long int u, long long int par = -1) {
  long long int ret = a[u] ? 1 : -1, i;
  if (cnt[u] == lst[u].size()) {
    if (par == -1) return ret + sum[u];
    i = lower_bound(lst[u].begin(), lst[u].end(), par) - lst[u].begin();
    if (dp[u][i] > 0) return ret + sum[u] - dp[u][i];
    return ret + sum[u];
  }
  for (int i = 0; i < lst[u].size(); i++) {
    if (lst[u][i] == par) continue;
    if (dp[u][i] == -2) {
      dp[u][i] = res(lst[u][i], u);
      cnt[u]++;
      if (dp[u][i] > 0) sum[u] += dp[u][i];
    }
    if (dp[u][i] > 0) ret += dp[u][i];
  }
  return ret;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    dp[u].push_back(-2);
    dp[v].push_back(-2);
    lst[u].push_back(v);
    lst[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) sort(lst[i].begin(), lst[i].end());
  for (int i = 1; i <= n; i++) cout << res(i) << " ";
  return 0;
}
