#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(const T& a, const T& b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(const T& a, const T& b) {
  return a / gcd(a, b) * b;
}
long long int dfs(int me, int pa, vector<vector<int> >& g) {
  long long int ret = 1;
  long long int cnt = 0;
  for (int i = 0; i < (int)(g[me].size()); i++) {
    if (g[me][i] == pa) continue;
    ret = lcm(ret, dfs(g[me][i], me, g));
    cnt++;
  }
  if (!cnt) return 1;
  return ret * cnt;
}
long long int solve(long long int x, int me, int pa, vector<vector<int> >& g,
                    vector<int>& node) {
  long long int ret = 0;
  if (g[me].size() == 1) {
    if (x > (long long int)node[me])
      return -1;
    else
      return (long long int)node[me] - x;
  }
  for (int i = 0; i < (int)(g[me].size()); i++) {
    if (g[me][i] == pa) continue;
    long long int a = solve(x / (g[me].size() - 1), g[me][i], me, g, node);
    if (a < 0) return -1;
    ret += a;
  }
  return ret;
}
vector<int> node;
vector<vector<int> > g;
int n;
int main(void) {
  cin >> n;
  node.resize(n);
  g.resize(n);
  for (int i = 0; i < (int)(n); i++) cin >> node[i];
  for (int i = 0; i < (int)(n - 1); i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  g[0].push_back(0);
  long long int k = dfs(0, 0, g);
  long long int l = 0, r = (1e15 + 1) / k;
  while (l + 1 < r) {
    long long int mid = (l + r) / 2;
    long long int midret = solve(mid * k, 0, 0, g, node);
    if (midret == -1) {
      r = mid - 1;
    } else
      l = mid;
  }
  long long int a = solve(l * k, 0, 0, g, node),
                b = solve(r * k, 0, 0, g, node);
  if (a == -1)
    cout << b << endl;
  else if (b == -1)
    cout << a << endl;
  else
    cout << min(a, b) << endl;
  return 0;
}
