#include <bits/stdc++.h>
using namespace std;
bool cp(pair<long long, long long> i, pair<long long, long long> j) {
  return i.first < j.first;
}
long long n;
int a[200005];
vector<long long> ad[200005];
long long dp[200005];
bool vis[200005];
void dfs(long long i) {
  vis[i] = true;
  long long k = a[i];
  for (auto u : ad[i]) {
    if (!vis[u]) {
      dfs(u);
      k = max(k, k + dp[u]);
    }
  }
  dp[i] = k;
}
void dfs2(long long i, long long j) {
  dp[i] = max(dp[i], dp[i] + j);
  vis[i] = true;
  for (auto u : ad[i]) {
    if (!vis[u]) {
      dfs2(u, dp[i] - max(dp[u], 0LL));
    }
  }
  return;
}
int main() {
  cin >> n;
  for (long long i = 1; i < n + 1; i = i + 1) cin >> a[i];
  for (long long i = 1; i < n + 1; i = i + 1)
    if (!a[i]) a[i]--;
  for (long long i = 0; i < n - 1; i = i + 1) {
    long long u, v;
    cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }
  memset(vis, false, sizeof(vis));
  memset(dp, 0, sizeof(dp));
  dfs(1);
  memset(vis, false, sizeof(vis));
  dfs2(1, 0);
  for (long long i = 1; i < n + 1; i = i + 1) cout << dp[i] << " ";
}
