Problem: You are given a tree consisting of n vertices. A tree is a connected undirected graph with n-1 edges. Each vertex v of this tree has a color assigned to it (a_v = 1 if the vertex v is white and 0 if the vertex v is black).

You have to solve the following problem for each vertex v: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex v? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cnt_w white vertices and cnt_b black vertices, you have to maximize cnt_w - cnt_b.

Input

The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices in the tree.

The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 1), where a_i is the color of the i-th vertex.

Each of the next n-1 lines describes an edge of the tree. Edge i is denoted by two integers u_i and v_i, the labels of vertices it connects (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i).

It is guaranteed that the given edges form a tree.

Output

Print n integers res_1, res_2, ..., res_n, where res_i is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex i.

Examples

Input


9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9


Output


2 2 2 2 2 1 1 0 2 


Input


4
0 0 1 0
1 2
1 3
1 4


Output


0 -1 1 -1 

Note

The first example is shown below:

<image>

The black vertices have bold borders.

In the second example, the best subtree for vertices 2, 3 and 4 are vertices 2, 3 and 4 correspondingly. And the best subtree for the vertex 1 is the subtree consisting of vertices 1 and 3.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
using namespace std;
void doin() {
  cin.tie();
  cout.tie();
  ios::sync_with_stdio(0);
}
template <typename X, typename Y>
istream& operator>>(istream& in, pair<X, Y>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T>
void getv(T& i) {
  cin >> i;
}
template <typename T, typename... Ns>
void getv(vector<T>& v, int n, Ns... ns) {
  v.resize(n);
  for (auto& i : v) getv(i, ns...);
}
template <typename T>
void getv1(T& i) {
  cin >> i;
}
template <typename T, typename... Ns>
void getv1(vector<T>& v, int n, Ns... ns) {
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) getv(v[i], ns...);
}
inline void getch(char& x) {
  while (x = getchar_unlocked(), x < 33) {
    ;
  }
}
inline void getstr(string& str) {
  str.clear();
  char cur;
  while (cur = getchar_unlocked(), cur < 33) {
    ;
  }
  while (cur > 32) {
    str += cur;
    cur = getchar_unlocked();
  }
}
template <typename T>
inline bool sc(T& num) {
  bool neg = 0;
  int c;
  num = 0;
  while (c = getchar_unlocked(), c < 33) {
    if (c == EOF) return false;
  }
  if (c == '-') {
    neg = 1;
    c = getchar_unlocked();
  }
  for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48;
  if (neg) num *= -1;
  return true;
}
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int n;
long long ans = 0;
vector<vector<long long>> g;
vector<long long> a;
struct node {
  long long rsum, sum, lazyk;
  node() { rsum = sum = lazyk = 0; }
};
node tree[800100];
struct segtree {
  int n;
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].sum = tree[v].rsum = a[l];
      return;
    }
    int mid = (l + r) / 2;
    build(2 * v, l, mid);
    build(2 * v + 1, mid + 1, r);
    tree[v] = merge(tree[2 * v], tree[2 * v + 1]);
  }
  void init(int N) {
    n = N;
    build(1, 1, n);
  }
  node merge(node a, node b) {
    a.sum += b.sum;
    a.rsum += b.rsum;
    a.lazyk = 0;
    return a;
  }
  void push(int v, int l, int r) {
    if (!tree[v].lazyk) return;
    tree[v].sum += tree[v].rsum * tree[v].lazyk;
    if (l != r) {
      tree[2 * v].lazyk += tree[v].lazyk;
      tree[2 * v + 1].lazyk += tree[v].lazyk;
    }
    tree[v].lazyk = 0;
  }
  void add(int v, int l, int r, int qL, int qR, long long k) {
    push(v, l, r);
    if (l > qR || r < qL) return;
    if (l >= qL && r <= qR) {
      tree[v].lazyk = k;
      push(v, l, r);
      return;
    }
    int mid = (l + r) / 2;
    add(2 * v, l, mid, qL, qR, k);
    add(2 * v + 1, mid + 1, r, qL, qR, k);
    tree[v] = merge(tree[2 * v], tree[2 * v + 1]);
  }
  void add(int l, int r, long long k) { add(1, 1, n, l, r, k); }
  long long get(int v, int l, int r, int qL, int qR) {
    push(v, l, r);
    if (l > qR || r < qL) return 0;
    if (l >= qL && r <= qR) {
      return tree[v].sum;
    }
    int mid = (l + r) / 2;
    return get(2 * v, l, mid, qL, qR) + get(2 * v + 1, mid + 1, r, qL, qR);
  }
  long long get(int l, int r) { return get(1, 1, n, l, r); }
};
segtree st;
vector<long long> ein, eout;
int etimer = 1;
void edfs(int v = 1, int p = 1) {
  ein[v] = etimer++;
  for (auto i : g[v])
    if (i != p) edfs(i, v);
  eout[v] = etimer - 1;
}
void predfs(int v = 1, int p = 1, int h = 0) {
  st.add(ein[v], ein[v], h - 1);
  for (auto i : g[v])
    if (i != p) predfs(i, v, h + 1);
}
void dfs(int v = 1, int p = 1, int h = 0) {
  ans = max(ans, st.get(1, n));
  for (auto i : g[v])
    if (i != p) {
      st.add(1, n, 1);
      st.add(ein[i], eout[i], -2);
      dfs(i, v);
      st.add(1, n, -1);
      st.add(ein[i], eout[i], 2);
    }
}
int main() {
  doin();
  cin >> n;
  vector<long long> ta;
  getv1(ta, n);
  g.resize(n + 1);
  for (int f, t, i = 1; i < n; i++) {
    cin >> f >> t;
    g[f].push_back(t);
    g[t].push_back(f);
  }
  ein.resize(n + 1);
  eout.resize(n + 1);
  edfs();
  a.resize(n + 1);
  for (int i = 1; i <= n; i++) a[ein[i]] = ta[i];
  st.init(n);
  predfs();
  dfs();
  cout << ans;
}