#include <bits/stdc++.h>
using namespace std;
long long gcd(long long m, long long n) { return n ? gcd(n, m % n) : m; }
const long long INF = 1ll << 50;
long long lcm(long long m, long long n) {
  long long g = gcd(m, n);
  if (m / g > INF / n) return 0;
  return m / g * n;
}
const int N = 100001;
long long a[N];
long long sum[N];
long long mul[N];
vector<int> G[N];
bool balance(int n, int p) {
  vector<int> &v = G[n];
  int deg = 0;
  for (int i = 0; i < (v.size()); i++) {
    if (v[i] == p) continue;
    if (!balance(v[i], n)) return false;
    deg++;
  }
  if (!deg) {
    sum[n] = a[n];
    mul[n] = 1;
    return true;
  }
  long long LCM = 1;
  for (int i = 0; i < (v.size()); i++) {
    if (v[i] == p) continue;
    LCM = lcm(LCM, mul[v[i]]);
    if (!LCM) return false;
  }
  long long x = INF;
  for (int i = 0; i < (v.size()); i++) {
    if (v[i] == p) continue;
    x = min(x, sum[v[i]] / LCM * LCM);
  }
  sum[n] = deg * x;
  mul[n] = deg * LCM;
  return true;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < (n); i++) cin >> a[i];
  for (int i = 0; i < (n - 1); i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  long long s = accumulate(a, a + n, 0ll);
  if (balance(0, -1)) s -= sum[0];
  cout << s << endl;
  return 0;
}
