#include <bits/stdc++.h>
using namespace std;
int st(int x, int pos) { return x = x | (1 << pos); }
int Reset(int x, int pos) { return x = x & ~(1 << pos); }
vector<long long> graph[300001];
long long dp[300001], ans[300001], val[300001];
void dfs(long long u, long long p) {
  dp[u] = val[u];
  for (long long v : graph[u]) {
    if (v != p) {
      dfs(v, u);
      dp[u] += max(0LL, dp[v]);
    }
  }
}
void dfs1(long long u, long long p) {
  ans[u] = dp[u];
  for (long long v : graph[u]) {
    if (v != p) {
      dp[u] -= max(0LL, dp[v]);
      dp[v] += max(0LL, dp[u]);
      dfs1(v, u);
      dp[v] -= max(0LL, dp[u]);
      dp[u] += max(0LL, dp[v]);
    }
  }
}
int main() {
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
  for (int i = 1; i <= n; i++) {
    if (val[i] == 0) {
      val[i] = -1;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1, -1);
  dfs1(1, -1);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  printf("\n");
}
