#include <bits/stdc++.h>
using namespace std;
vector<long long> v[200005];
int colour[200005];
long long dp[200005];
long long ans[200005];
void dfs(long long source, long long p) {
  dp[source] = colour[source];
  for (int i = 0; i < v[source].size(); ++i) {
    if (v[source][i] == p) continue;
    dfs(v[source][i], source);
    dp[source] += max((long long)0, dp[v[source][i]]);
  }
}
void dfs2(long long source, long long p) {
  ans[source] = dp[source];
  for (int i = 0; i < v[source].size(); ++i) {
    if (v[source][i] == p) continue;
    dp[source] -= max((long long)0, dp[v[source][i]]);
    dp[v[source][i]] += max((long long)0, dp[source]);
    dfs2(v[source][i], source);
    dp[v[source][i]] -= max((long long)0, dp[source]);
    dp[source] += max((long long)0, dp[v[source][i]]);
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long n, x, y;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> colour[i];
    if (!colour[i]) colour[i] = -1;
    ans[i] = 0;
  }
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }
  cout << "\n";
  return 0;
}
