#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
constexpr int N_MAX = 200000;
vector<int> color(N_MAX, 0), dp(N_MAX, -INF);
vector<int> tree[N_MAX];
int dfs(int node, int par) {
  int min_child_ret = color[node];
  for (int child : tree[node]) {
    if (child == par) continue;
    min_child_ret += max(0, dfs(child, node));
  }
  return dp[node] = min_child_ret;
}
void dfs2(int node, int par) {
  for (int child : tree[node]) {
    if (child == par) continue;
    if (dp[node] > 0) {
      if (dp[child] > 0) {
        dp[child] += max(0, dp[node] - dp[child]);
      } else {
        dp[child] += dp[node];
      }
    }
    dfs2(child, node);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, u, v;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> color[i];
    if (color[i] == 0) color[i] = -1;
  }
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    --u;
    --v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs(0, -1);
  dfs2(0, -1);
  for (int i = 0; i < n; ++i) {
    if (i) cout << " ";
    cout << dp[i];
  }
  cout << "\n";
}
