#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, pair<S, T> const& p) {
  out << '(' << p.first << ", " << p.second << ')';
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
  long long l = v.size();
  for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
  if (l > 0) out << v[l - 1];
  return out;
}
template <typename T>
void trace(const char* name, T&& arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}
int n;
vector<int> col, dp, vis, ans;
vector<vector<int> > gr;
void dfs(int node) {
  vis[node] = 1;
  for (auto nbr : gr[node]) {
    if (vis[nbr] == 0) {
      dfs(nbr);
      dp[node] += max(0, dp[nbr]);
    }
  }
}
void dfs2(int node) {
  vis[node] = 1;
  for (auto nbr : gr[node]) {
    if (vis[nbr] == 1) {
      continue;
    }
    dp[node] -= max(0, dp[nbr]);
    dp[nbr] += max(0, dp[node]);
    ans[nbr] = dp[nbr];
    dfs2(nbr);
    dp[nbr] -= max(0, dp[node]);
    dp[node] += max(0, dp[nbr]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  col.resize(n);
  gr.resize(n);
  dp.resize(n);
  vis.resize(n, 0);
  ans.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> col[i];
    if (col[i] == 1) {
      dp[i] = 1;
    } else {
      dp[i] = -1;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    gr[u].push_back(v);
    gr[v].push_back(u);
  }
  dfs(0);
  ans[0] = dp[0];
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
  }
  dfs2(0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << "\n";
  return 0;
}
