#include <bits/stdc++.h>
using namespace std;
const int S = 2e5 + 5;
vector<int> adj[S];
vector<long long int> a(S);
vector<long long int> ans(S);
vector<long long int> dp(S);
void dfs(int i, int p) {
  dp[i] = a[i];
  for (auto x : adj[i]) {
    if (x == p) continue;
    dfs(x, i);
    dp[i] += max(0LL, dp[x]);
  }
}
void dfs2(int i, int p, int prev_score) {
  ans[i] = dp[i] + prev_score;
  for (auto x : adj[i]) {
    if (x == p) continue;
    dfs2(x, i, max(0LL, ans[i] - max(dp[x], 0LL)));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) a[i] -= (!a[i]);
  int x, y;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(0, -1);
  dfs2(0, -1, 0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << '\n';
}
