#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> dp_in, dp_out;
vector<vector<pair<int, int>>> g;
vector<pair<int, int>> color;
void dfs1(int u, int p) {
  dp_in[u] = color[u];
  int v;
  for (auto& row : g[u]) {
    v = row.first;
    if (v ^ p) {
      dfs1(v, u);
      if ((dp_in[u].first - dp_in[u].second) <
          (dp_in[u].first - dp_in[u].second + dp_in[v].first -
           dp_in[v].second)) {
        dp_in[u].first += dp_in[v].first;
        dp_in[u].second += dp_in[v].second;
        row.second = 1;
      }
    }
  }
}
void dfs2(int u, int p) {
  int v;
  for (auto& row : g[u]) {
    v = row.first;
    if (v ^ p) {
      dp_out[v] = color[v];
      auto t = dp_in[u];
      if (row.second) {
        t.first -= dp_in[v].first;
        t.second -= dp_in[v].second;
      }
      if ((dp_out[v].first - dp_out[v].second) <
          (dp_out[v].first - dp_out[v].second + t.first - t.second +
           dp_out[u].first - dp_out[u].second - color[u].first +
           color[u].second)) {
        dp_out[v].first += t.first + dp_out[u].first - color[u].first;
        dp_out[v].second += t.second + dp_out[u].second - color[u].second;
      }
      dfs2(v, u);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  color = vector<pair<int, int>>(n, {0, 0});
  int x, y;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    if (x)
      color[i].first = 1;
    else
      color[i].second = 1;
  }
  g = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>());
  for (int i = 1; i < n; ++i) {
    cin >> x >> y;
    g[x - 1].emplace_back(y - 1, 0);
    g[y - 1].emplace_back(x - 1, 0);
  }
  dp_in = vector<pair<int, int>>(n + 1, {0, 0});
  dp_out = vector<pair<int, int>>(n + 1, {0, 0});
  dfs1(0, -1);
  dp_out[0] = color[0];
  dfs2(0, -1);
  int result;
  for (int i = 0; i < n; ++i) {
    auto t = color[i];
    result = max({dp_in[i].first - dp_in[i].second,
                  dp_out[i].first - dp_out[i].second,
                  dp_in[i].first - dp_in[i].second + dp_out[i].first -
                      dp_out[i].second - t.first + t.second});
    cout << result << " ";
  }
  return 0;
}
