#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 2 * N;
int U[M], V[M], W[M], pos[M], vals[N];
vector<int> g[N];
void add_edge(int e, int x, int y, int w) {
  U[e] = x;
  V[e] = y;
  W[e] = w;
  pos[e] = g[x].size();
  g[x].push_back(e);
}
int far[M], vis[M];
vector<int> prefix[N], suffix[N];
int val_or_zero(vector<int>& v, int idx) {
  if (idx >= 0 && idx < (int)v.size()) return v[idx];
  return 0;
}
void recompute_prefix_suffix(int x) {
  int n = g[x].size();
  prefix[x].resize(n);
  suffix[x].resize(n);
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    prefix[x][i] = val_or_zero(prefix[x], i - 1) + max(0, far[g[x][i]]);
    suffix[x][j] = val_or_zero(suffix[x], j + 1) + max(0, far[g[x][j]]);
  }
}
void dfs(int x, int pe) {
  if (!vis[x]) {
    vis[x] = (pe ^ 1);
    for (auto e : g[x])
      if (e != vis[x]) dfs(V[e], e);
    recompute_prefix_suffix(x);
  } else if (vis[x] > 1) {
    dfs(V[vis[x]], vis[x]);
    vis[x] = 1;
    recompute_prefix_suffix(x);
  }
  if (pe) {
    int idx = pos[pe ^ 1];
    far[pe] = val_or_zero(prefix[x], idx - 1) +
              val_or_zero(suffix[x], idx + 1) + vals[x];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> vals[i];
    if (vals[i] == 0) vals[i]--;
  }
  for (int i = 1; i < n; i++) {
    int x, y, w = 1;
    cin >> x >> y;
    add_edge(2 * i, x, y, w);
    add_edge(2 * i + 1, y, x, w);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] != 1) {
      dfs(i, 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << vals[i] + suffix[i][0] << " ";
  }
}
