#include <bits/stdc++.h>
using namespace std;
const long long Mxn = 1e6 + 7;
const long long Mod = 1e9 + 7;
const long long Inf = 1e14 + 7;
int N, A[Mxn], Ans[Mxn], Dp[Mxn];
vector<int> g[Mxn];
void Prep(int v, int p = -1) {
  Dp[v] = A[v];
  for (int to : g[v]) {
    if (to == p) continue;
    Prep(to, v);
    Dp[v] += max(0, Dp[to]);
  }
}
void Dfs(int v, int p = -1) {
  Ans[v] = Dp[v];
  for (int to : g[v]) {
    if (to == p) continue;
    Dp[v] -= max(0, Dp[to]);
    Dp[to] += max(0, Dp[v]);
    Dfs(to, v);
    Dp[to] -= max(0, Dp[v]);
    Dp[v] += max(0, Dp[to]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (!A[i]) A[i]--;
  }
  for (int i = 1, u, v; i < N; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  Prep(1);
  Dfs(1);
  for (int i = 1; i <= N; i++) {
    cout << Ans[i] << ' ';
  }
  return false;
}
