#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;
const int N = 2e5 + 5;
int n;
int color[N];
int dp[N], dp1[N];
vi adj[N];
void dfs(int u, int p) {
  if (color[u]) {
    dp[u] = 1;
  } else {
    dp[u] = -1;
  }
  for (int v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
    dp[u] += max(0, dp[v]);
  }
}
void bigbrain(int u, int p) {
  dp1[u] = dp[u];
  for (int v : adj[u]) {
    if (v == p) continue;
    dp[u] -= max(0, dp[v]);
    dp[v] += max(0, dp[u]);
    bigbrain(v, u);
    dp[v] -= max(0, dp[u]);
    dp[u] += max(0, dp[v]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> color[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  bigbrain(1, 0);
  for (int i = 1; i <= n; i++) {
    cout << dp1[i] << ' ';
  }
  cout << '\n';
  return 0;
}
