#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int t, n, m, a[200005] = {0}, p[200005] = {0}, vis[200005] = {0},
                       dp[200005] = {0};
vector<long long int> v[200005];
void dfs1(long long int x, long long int y) {
  p[x] = y;
  if (a[x] == 1)
    dp[x] = 1;
  else
    dp[x] = -1;
  long long int i, ss = 0;
  for (i = 0; i < v[x].size(); i++) {
    if (v[x][i] == y) continue;
    dfs1(v[x][i], x);
    if (dp[v[x][i]] >= 0) ss += dp[v[x][i]];
  }
  dp[x] += ss;
}
void dfs2(long long int x) {
  long long int ss = 0, i;
  if (x != 1) {
    if (dp[x] >= 0)
      ss = max(ss, dp[p[x]] - dp[x]);
    else
      ss = max(ss, dp[p[x]]);
    dp[x] += ss;
  }
  for (i = 0; i < v[x].size(); i++) {
    if (v[x][i] == p[x]) continue;
    dfs2(v[x][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, x, y;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs1(1, 1);
  dfs2(1);
  for (i = 1; i <= n; i++) cout << dp[i] << " ";
  return 0;
}
