#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T a, T b) {
  ;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
}  // namespace std
long long int readL() {
  long long int res;
  scanf("%I64d", &res);
  return res;
}
void printL(long long int res) { printf("%I64d", res); }
int n;
int ar[100005];
vector<int> g[100005];
const long long int INF = 1e18;
long long int L = 1;
long long int gcd(long long int a, long long int b) {
  if (a > b) swap(a, b);
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}
long long int sum;
long long int lcm(long long int a, long long int b) {
  long long int g = gcd(a, b);
  if (g == 0) return INF;
  double res = a / (double)g * b;
  if (res >= 1.1e13) {
    cout << sum << endl;
    exit(0);
  }
  return a / g * b;
}
long long int mul[100005];
void rec(int v, int p, long long int cur) {
  int chil = g[v].size();
  if (p != -1) --chil;
  L = lcm(L, cur);
  mul[v] = cur;
  for (auto to : g[v]) {
    if (to == p) continue;
    double mul2 = cur * (double)chil;
    if (mul2 >= 1.1e13) {
      cout << sum << endl;
      exit(0);
    }
    rec(to, v, cur * chil);
  }
}
int main() {
  cin >> n;
  sum = 0;
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &ar[i]);
    sum += ar[i];
  }
  for (int i = 0; i < (n - 1); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  rec(0, -1, 1);
  {
    long long int maxi = INF;
    for (int i = (1); i < (n); ++i)
      if (g[i].size() == 1) {
        long long int frac = L / mul[i];
        chmin(maxi, ar[i] / frac);
      }
    long long int sum = accumulate(ar, ar + n, 0ll);
    for (int i = (1); i < (n); ++i)
      if (g[i].size() == 1) {
        long long int frac = L / mul[i];
        sum -= frac * maxi;
      }
    cout << sum << endl;
  }
  return 0;
}
