#include <bits/stdc++.h>
using namespace std;
namespace solve {
long long int a[200007], sum[200007], dpDown[200007], dpUp[200007];
vector<int> graph[200007];
void rec(int u, int p) {
  sum[u] = a[u];
  for (int v : graph[u]) {
    if (v == p) continue;
    rec(v, u);
    dpDown[u] += dpDown[v] + sum[v];
    sum[u] += sum[v];
  }
}
void dfs(int u, int p) {
  for (int v : graph[u]) {
    if (v == p) continue;
    dpUp[v] = dpDown[u] - dpDown[v] - sum[v] + (sum[1] - sum[v]) + dpUp[u];
    dfs(v, u);
  }
}
void takeInput() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  rec(1, -1);
  dfs(1, -1);
  long long int res = 0;
  for (int i = 1; i <= n; i++) {
    res = max(res, dpDown[i] + dpUp[i]);
  }
  cout << res << endl;
}
}  // namespace solve
int main() {
  solve::takeInput();
  return 0;
}
