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>
using namespace std;
char local = 'O';
template <typename T>
void read(T &a) {
  cin >> a;
}
template <typename T, typename T1>
void read(T &a, T1 &b) {
  cin >> a >> b;
}
template <typename T, typename T1, typename T2>
void read(T &a, T1 &b, T2 &c) {
  cin >> a >> b >> c;
}
template <typename T, typename T1>
void read(pair<T, T1> &p) {
  cin >> p.first >> p.second;
}
template <typename T>
void read(T arr[], long long n) {
  for (long long i = 0; i < n; i++) {
    read(arr[i]);
  }
}
template <typename T>
void read(vector<T> &arr) {
  for (long long i = 0; i < arr.size(); i++) {
    read(arr[i]);
  }
}
long long modularExponentiation(long long x, long long n, long long M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modularExponentiation((x * x) % M, n / 2, M);
  return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M;
}
long long binaryExponentiation(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binaryExponentiation(x * x, n / 2);
  else
    return x * binaryExponentiation(x * x, (n - 1) / 2);
}
long long GCD(long long A, long long B) {
  if (B == 0)
    return A;
  else
    return GCD(B, A % B);
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long modinv(long long a, long long mod) {
  long long x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long modPrimeInverse(long long A, long long M) {
  return modularExponentiation(A, M - 2, M);
}
vector<long long> prime;
void sieve(long long N) {
  bool isPrime[N + !1];
  for (long long i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (long long i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) {
      for (long long j = i * i; j <= N; j += i) isPrime[j] = false;
    }
  }
  for (long long i = 2; i <= N; i++) {
    if (isPrime[i] == true) {
      prime.push_back(i);
    }
  }
}
long long minPrime[1000007];
void factorSieve(long long n) {
  memset(minPrime, 0, sizeof(minPrime));
  for (long long i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {
      for (long long j = i * i; j <= n; j += i) {
        if (minPrime[j] == 0) {
          minPrime[j] = i;
        }
      }
    }
  }
  for (long long i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
      minPrime[i] = i;
    }
  }
}
long long factorize(long long n) {
  map<long long, long long> m;
  m.clear();
  while (n != 1) {
    m[minPrime[n]]++;
    n /= minPrime[n];
  }
  for (auto i : m) {
    if (i.second > 1) {
      return 0;
    }
  }
  return 1;
}
void merge(long long A[], long long start, long long mid, long long end) {
  long long p = start, q = mid + 1;
  long long Arr[end - start + 1], k = 0;
  for (long long i = start; i <= end; i++) {
    if (p > mid)
      Arr[k++] = A[q++];
    else if (q > end)
      Arr[k++] = A[p++];
    else if (A[p] < A[q])
      Arr[k++] = A[p++];
    else
      Arr[k++] = A[q++];
  }
  for (long long p = 0; p < k; p++) {
    A[start++] = Arr[p];
  }
}
void merge_sort(long long A[], long long start, long long end) {
  if (start < end) {
    long long mid = (start + end) / 2;
    merge_sort(A, start, mid);
    merge_sort(A, mid + 1, end);
    merge(A, start, mid, end);
  }
}
long long binarySearch(long long a[], long long low, long long high,
                       long long key) {
  while (low <= high) {
    long long mid = (low + high) / 2;
    if (a[mid] < key) {
      low = mid + 1;
    } else if (a[mid] > key) {
      high = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  if (a.first == b.first) {
    a.second > b.second;
  }
  return (a.first < b.first);
}
void fib(long long n, long long m, long long a[]) {
  if (n == 0) {
    a[0] = 0;
    a[1] = 1;
    return;
  }
  fib(n / 2, m, a);
  long long c, d;
  c = (a[0] * (2 * a[1] - a[0] + m)) % m;
  d = (a[0] * a[0] + a[1] * a[1]) % m;
  if (n % 2 == 0) {
    a[0] = c;
    a[1] = d;
  } else {
    a[0] = d;
    a[1] = c + d;
  }
}
struct comp {
  bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
    if (a.first == b.first) {
      return a.second > b.second;
    }
    return a.first < b.first;
  }
};
long long n;
vector<long long> a, ans, cal;
vector<vector<long long>> b;
long long check(long long x, long long p) {
  long long k = 0;
  for (auto i : b[x]) {
    if (i != p) {
      k += max(check(i, x), 0LL);
    }
  }
  cal[x] = max(k + a[x], a[x]);
  return cal[x];
}
void ma(long long x, long long p) {
  long long f = ans[p];
  if (cal[x] > 0) {
    ans[x] = max(f, cal[x]);
  } else {
    ans[x] = max(cal[x], f + cal[x]);
  }
  for (auto i : b[x]) {
    if (i != p) {
      ma(i, x);
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  a.clear();
  a.resize(n + 1);
  ans.resize(n + 1);
  cal.resize(n + 1);
  b.resize(n + 1);
  ans[0] = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      a[i] = -1;
    }
  }
  for (long long i = 0; i < n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    b[x].push_back(y);
    b[y].push_back(x);
  }
  check(1, 0);
  ma(1, 0);
  for (long long i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << "\n";
}