#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
int n, dp[M], ans[M];
vector<int> g[M];
void dfs(int v, int par) {
  for (auto u : g[v])
    if (u != par) dfs(u, v), dp[v] += max(dp[u], 0);
}
void dfs2(int v, int par, int up) {
  ans[v] = dp[v] + up;
  for (auto u : g[v])
    if (u != par) dfs2(u, v, max(0, up + dp[v] - max(0, dp[u])));
}
int32_t main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> dp[i];
    if (!dp[i]) dp[i]--;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 1);
  dfs2(1, 1, 0);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
