#include <bits/stdc++.h>
int mod = 1000000000 + 7;
long long int inf = 1000000000000000000;
using namespace std;
mt19937 unlucko(chrono::steady_clock::now().time_since_epoch().count());
vector<int> tree[200000], way(200000), dp(200000), ans(200000);
void dfs(int node, int pnode) {
  dp[node] = way[node];
  for (auto it : tree[node]) {
    if (it == pnode) continue;
    dfs(it, node);
    dp[node] += max(dp[it], 0);
  }
}
void jfs(int node, int pnode, int curr) {
  ans[node] = dp[node] + curr;
  for (auto it : tree[node]) {
    if (it == pnode) continue;
    jfs(it, node, max(ans[node] - max(0, dp[it]), 0));
  }
}
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> way[i];
    if (!way[i]) way[i]--;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    y--;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  dfs(0, 0);
  jfs(0, 0, 0);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
