#include <bits/stdc++.h>
using namespace std;
inline void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); }
inline long long Set(long long n, long long pos) {
  return n = n | (1LL << pos);
}
inline long long Reset(long long n, long long pos) {
  return n = n & ~(1LL << pos);
}
inline bool Check(long long n, long long pos) {
  return (bool)(n & (1LL << pos));
}
inline long long Count(long long n) { return __builtin_popcountll(n); }
long long const N = 3e5 + 5;
long long const MOD = 998244353;
long long color[N];
vector<long long> adj[N];
long long dp[N];
long long res[N];
long long dfs(long long node, long long p) {
  long long col = 0;
  for (auto c : adj[node]) {
    if (p == c) continue;
    long long temp = dfs(c, node);
    col += max(temp, 0LL);
  }
  col += color[node];
  return dp[node] = col;
}
void adfs(long long node, long long p, long long up) {
  up = max(up, 0LL);
  res[node] = dp[node] + up;
  for (auto c : adj[node]) {
    if (p == c) continue;
    long long temp = dp[node] - max(dp[c], 0LL) + up;
    adfs(c, node, temp);
  }
}
void solve() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> color[i];
    if (color[i] == 0) color[i] = -1;
  }
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  adfs(1, -1, 0);
  for (long long i = 1; i <= n; i++) cout << res[i] << " ";
}
int32_t main() {
  fast();
  solve();
}
