#include <bits/stdc++.h>
constexpr int DEBUG = 0;
using namespace std;
using int64 = long long;
struct Edge {
  int from, to;
  Edge(int from, int to) : from(from), to(to) {}
};
vector<vector<Edge>> ReadUndirectedGraph(int n, int m,
                                         bool one_indexed = false) {
  vector<vector<Edge>> graph(n);
  for (int i = 0; i < m; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (one_indexed) {
      v1--;
      v2--;
    }
    graph[v1].push_back(Edge(v1, v2));
    graph[v2].push_back(Edge(v2, v1));
  }
  return graph;
}
template <class T, class U>
vector<T> ReRooting(const vector<vector<Edge>>& graph,
                    function<U(T, Edge)> edge_fn, function<U(U, U)> merge_fn,
                    U merge_id, function<T(U, int)> vertex_fn) {
  int n = graph.size();
  vector<int> vs({0}), ps(n, -2);
  ps[0] = -1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (const auto& e : graph[v]) {
      if (e.to == ps[e.to] || ps[e.to] >= -1) continue;
      vs.push_back(e.to);
      ps[e.to] = v;
      q.push(e.to);
    }
  }
  vector<T> dp_1(n);
  for (int i = n - 1; i >= 0; i--) {
    int v = vs[i];
    int p = ps[v];
    auto a = merge_id;
    for (const auto& e : graph[v]) {
      if (e.to == ps[v]) continue;
      a = merge_fn(a, edge_fn(dp_1[e.to], e));
    }
    dp_1[v] = vertex_fn(a, v);
  }
  vector<T> dp_2(n), dp_3(n);
  dp_2[0] = merge_id;
  for (int i = 0; i < n; i++) {
    int v = vs[i];
    int p = ps[v];
    int m = graph[v].size();
    vector<T> ls(m + 1), rs(m + 1);
    ls[0] = merge_id;
    for (int j = 0; j < m; j++) {
      const auto& e = graph[v][j];
      if (e.to == p)
        ls[j + 1] = ls[j];
      else
        ls[j + 1] = merge_fn(ls[j], edge_fn(dp_1[e.to], e));
    }
    rs[m] = merge_id;
    for (int j = m - 1; j >= 0; j--) {
      const auto& e = graph[v][j];
      if (e.to == p)
        rs[j] = rs[j + 1];
      else
        rs[j] = merge_fn(rs[j + 1], edge_fn(dp_1[e.to], e));
    }
    dp_3[v] = vertex_fn(merge_fn(ls[m], dp_2[v]), v);
    for (int j = 0; j < m; j++) {
      const auto& e = graph[v][j];
      if (e.to == p) continue;
      for (const auto& re : graph[e.to]) {
        if (re.to != v) continue;
        T merged = merge_fn(merge_fn(ls[j], rs[j + 1]), dp_2[v]);
        dp_2[e.to] = edge_fn(vertex_fn(merged, v), re);
      }
    }
  }
  return dp_3;
}
template <class T>
void UpdateMin(T& a, T b) {
  if (a > b) a = b;
}
template <class T>
void UpdateMax(T& a, T b) {
  if (a < b) a = b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int64> ws(n);
  for (int i = (int)(0); i < (int)(n); i++) cin >> ws[i];
  auto graph = ReadUndirectedGraph(n, n - 1, true);
  using PI64 = pair<int64, int64>;
  function<PI64(PI64, Edge)> edge_fn = [](PI64 in, Edge e) -> PI64 {
    return in;
  };
  function<PI64(PI64, PI64)> merge_fn = [](PI64 in1, PI64 in2) -> PI64 {
    return {in1.first + in2.first, in1.second + in2.second};
  };
  PI64 merge_id = {0, 0};
  function<PI64(PI64, int)> vertex_fn = [&](PI64 in, int v) -> PI64 {
    PI64 out;
    out.first = in.first + ws[v];
    out.second = in.second + in.first;
    return out;
  };
  auto result = ReRooting(graph, edge_fn, merge_fn, merge_id, vertex_fn);
  int64 ans = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    UpdateMax(ans, result[i].second);
  }
  cout << ans << endl;
}

### Prompt

Please formulate a Cpp solution to the following problem:

You are given a tree consisting exactly of n vertices. Tree is a connected undirected graph with n-1 edges. Each vertex v of this tree has a value a_v assigned to it.

Let dist(x, y) be the distance between the vertices x and y. The distance between the vertices is the number of edges on the simple path between them.

Let's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be v. Then the cost of the tree is ∑_{i = 1}^{n} dist(i, v) ⋅ a_i.

Your task is to calculate the maximum possible cost of the tree if you can choose v arbitrarily.

Input

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

The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the value of the vertex i.

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 one integer — the maximum possible cost of the tree if you can choose any vertex as v.

Examples

Input


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


Output


121


Input


1
1337


Output


0

Note

Picture corresponding to the first example: <image>

You can choose the vertex 3 as a root, then the answer will be 2 ⋅ 9 + 1 ⋅ 4 + 0 ⋅ 1 + 3 ⋅ 7 + 3 ⋅ 10 + 4 ⋅ 1 + 4 ⋅ 6 + 4 ⋅ 5 = 18 + 4 + 0 + 21 + 30 + 4 + 24 + 20 = 121.

In the second example tree consists only of one vertex so the answer is always 0.

### Response

```cpp
#include <bits/stdc++.h>
constexpr int DEBUG = 0;
using namespace std;
using int64 = long long;
struct Edge {
  int from, to;
  Edge(int from, int to) : from(from), to(to) {}
};
vector<vector<Edge>> ReadUndirectedGraph(int n, int m,
                                         bool one_indexed = false) {
  vector<vector<Edge>> graph(n);
  for (int i = 0; i < m; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (one_indexed) {
      v1--;
      v2--;
    }
    graph[v1].push_back(Edge(v1, v2));
    graph[v2].push_back(Edge(v2, v1));
  }
  return graph;
}
template <class T, class U>
vector<T> ReRooting(const vector<vector<Edge>>& graph,
                    function<U(T, Edge)> edge_fn, function<U(U, U)> merge_fn,
                    U merge_id, function<T(U, int)> vertex_fn) {
  int n = graph.size();
  vector<int> vs({0}), ps(n, -2);
  ps[0] = -1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (const auto& e : graph[v]) {
      if (e.to == ps[e.to] || ps[e.to] >= -1) continue;
      vs.push_back(e.to);
      ps[e.to] = v;
      q.push(e.to);
    }
  }
  vector<T> dp_1(n);
  for (int i = n - 1; i >= 0; i--) {
    int v = vs[i];
    int p = ps[v];
    auto a = merge_id;
    for (const auto& e : graph[v]) {
      if (e.to == ps[v]) continue;
      a = merge_fn(a, edge_fn(dp_1[e.to], e));
    }
    dp_1[v] = vertex_fn(a, v);
  }
  vector<T> dp_2(n), dp_3(n);
  dp_2[0] = merge_id;
  for (int i = 0; i < n; i++) {
    int v = vs[i];
    int p = ps[v];
    int m = graph[v].size();
    vector<T> ls(m + 1), rs(m + 1);
    ls[0] = merge_id;
    for (int j = 0; j < m; j++) {
      const auto& e = graph[v][j];
      if (e.to == p)
        ls[j + 1] = ls[j];
      else
        ls[j + 1] = merge_fn(ls[j], edge_fn(dp_1[e.to], e));
    }
    rs[m] = merge_id;
    for (int j = m - 1; j >= 0; j--) {
      const auto& e = graph[v][j];
      if (e.to == p)
        rs[j] = rs[j + 1];
      else
        rs[j] = merge_fn(rs[j + 1], edge_fn(dp_1[e.to], e));
    }
    dp_3[v] = vertex_fn(merge_fn(ls[m], dp_2[v]), v);
    for (int j = 0; j < m; j++) {
      const auto& e = graph[v][j];
      if (e.to == p) continue;
      for (const auto& re : graph[e.to]) {
        if (re.to != v) continue;
        T merged = merge_fn(merge_fn(ls[j], rs[j + 1]), dp_2[v]);
        dp_2[e.to] = edge_fn(vertex_fn(merged, v), re);
      }
    }
  }
  return dp_3;
}
template <class T>
void UpdateMin(T& a, T b) {
  if (a > b) a = b;
}
template <class T>
void UpdateMax(T& a, T b) {
  if (a < b) a = b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int64> ws(n);
  for (int i = (int)(0); i < (int)(n); i++) cin >> ws[i];
  auto graph = ReadUndirectedGraph(n, n - 1, true);
  using PI64 = pair<int64, int64>;
  function<PI64(PI64, Edge)> edge_fn = [](PI64 in, Edge e) -> PI64 {
    return in;
  };
  function<PI64(PI64, PI64)> merge_fn = [](PI64 in1, PI64 in2) -> PI64 {
    return {in1.first + in2.first, in1.second + in2.second};
  };
  PI64 merge_id = {0, 0};
  function<PI64(PI64, int)> vertex_fn = [&](PI64 in, int v) -> PI64 {
    PI64 out;
    out.first = in.first + ws[v];
    out.second = in.second + in.first;
    return out;
  };
  auto result = ReRooting(graph, edge_fn, merge_fn, merge_id, vertex_fn);
  int64 ans = 0;
  for (int i = (int)(0); i < (int)(n); i++) {
    UpdateMax(ans, result[i].second);
  }
  cout << ans << endl;
}

```