#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double PI = 3.14159265359;
const long long MOD = 998244353;
void i_o() {}
vector<vector<int>> adj;
vector<int> dp, ans, col;
void dfs1(int v, int par = -1) {
  dp[v] = col[v];
  for (int i : adj[v]) {
    if (i == par) continue;
    dfs1(i, v);
    dp[v] += max(0, dp[i]);
  }
}
void dfs2(int v, int par = -1) {
  ans[v] = dp[v];
  for (int i : adj[v]) {
    if (i == par) continue;
    dp[v] -= max(0, dp[i]);
    dp[i] += max(0, dp[v]);
    dfs2(i, v);
    dp[i] -= max(0, dp[v]);
    dp[v] += max(0, dp[i]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  i_o();
  int n;
  cin >> n;
  col = vector<int>(n);
  for (int i = 0; i < n; i++) cin >> col[i];
  for (int i = 0; i < n; i++)
    if (col[i] == 0) col[i] = -1;
  adj = vector<vector<int>>(n);
  dp = vector<int>(n), ans = vector<int>(n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs1(0);
  dfs2(0);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
  return 0;
}
