#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  out << "[";
  for (auto k : v) out << k << " ";
  out << "]"
      << "\n";
  return out;
}
template <class T>
ostream& operator<<(ostream& out, set<T> s) {
  out << "{";
  for (auto k : s) out << k << " ";
  out << "}"
      << "\n";
  return out;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U> p) {
  out << "[ " << p.first << " , " << p.second << " ] ";
  return out;
}
template <class T, class U>
istream& operator>>(istream& in, pair<T, U>& p) {
  in >> p.first >> p.second;
  return in;
}
int hs[300000], n, a[300000], res[300000];
vector<int> adj[300000];
int DFSA(int nod, int p) {
  hs[nod] = a[nod];
  for (auto k : adj[nod])
    if (p != k) hs[nod] += max(0, DFSA(k, nod));
  return hs[nod];
}
void DFSB(int nod, int p, int ac) {
  ac = max(0, ac);
  for (auto k : adj[nod])
    if (k != p) DFSB(k, nod, ac + hs[nod] - max(0, hs[k]));
  res[nod] = hs[nod] + ac;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> n;
  for (int i = int(1); i < int(n + 1); i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] -= 1;
  }
  for (int i = 0; i < int(n - 1); i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  DFSA(1, -1);
  DFSB(1, -1, 0);
  for (int i = 0; i < int(n); i++) cout << res[i + 1] << " ";
  cout << "\n";
  return 0;
}
