#include <bits/stdc++.h>
const long long mod = (long long)1e9 + 7;
using namespace std;
vector<vector<long long>> adj;
vector<long long> cl;
vector<long long> bst;
vector<long long> ans;
long long dfs(long long s, long long p) {
  long long best = (cl[s] == 1) ? 1 : -1;
  for (auto i : adj[s]) {
    if (i == p) continue;
    best += dfs(i, s);
  }
  bst[s] = best;
  if (best < 0) return 0;
  return best;
}
void dfs2(long long s, long long p, long long bk) {
  if (s == 1) {
    ans[s] = bk;
  } else {
    if (bst[s] < 0) bk--;
    ans[s] = max(bst[s], bk);
  }
  for (auto i : adj[s]) {
    if (i == p) continue;
    dfs2(i, s, ans[s]);
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  adj.resize(n + 1);
  cl.resize(n + 1);
  bst.resize(n + 1);
  ans.resize(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> cl[i];
  }
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, -1);
  dfs2(1, -1, bst[1]);
  for (long long i = 1; i <= n; i++) cout << ans[i] << ' ';
  return 0;
}
