#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const long long INF_L = 0x7fffffffffffffff;
vector<int> G[101000];
int root = 1;
long long a[101000], d[101000];
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) {
  return a && b ? (a / (a > b ? gcd(a, b) : gcd(b, a))) * b : 0;
}
long long dfs(int from, int p = -1) {
  long long lmin = INF_L;
  long long l = 1;
  for (int i = 0; i < G[from].size(); ++i) {
    int to = G[from][i];
    if (to == p) continue;
    long long cur = dfs(to, from);
    l = lcm(l, d[to]);
    lmin = min(lmin, cur);
  }
  if (G[from].size() > 1 || from == root)
    d[from] = l * (G[from].size() - (from == root ? 0 : 1));
  if (l && lmin / l) {
    a[from] = (G[from].size() > 1 || from == root
                   ? (lmin / l) * (G[from].size() - (from == root ? 0 : 1)) * l
                   : a[from]);
  } else {
    a[from] = 0;
  }
  return a[from];
}
int main() {
  int n, x, y;
  long long sum = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
    if (a[i]) d[i] = 1;
  }
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(root);
  cout << sum - a[root] << endl;
  return 0;
}
