#include <bits/stdc++.h>
using namespace std;
bool flag = false;
long long gcd(long long x, long long y) {
  if (y == 0) {
    return x;
  } else {
    return gcd(y, x % y);
  }
}
long long gcm(long long x, long long y) { return x * y / gcd(x, y); }
void dfs1(long long n, long long cur, vector<long long>& val,
          vector<vector<long long>>& g, long long frm,
          vector<long long>& child) {
  if (cur != 1 && g[cur].size() == 1) {
    val[cur] = 1;
    return;
  }
  long long v = 1;
  for (auto nex : g[cur]) {
    if (nex == frm) {
      continue;
    }
    dfs1(n, nex, val, g, cur, child);
    child[cur]++;
    if (flag) {
      return;
    }
    v = gcm(v, val[nex]);
    if (val[cur] > 1e8) {
      flag = true;
    }
  }
  val[cur] = v * child[cur];
  if (val[cur] > 1e8) {
    flag = true;
  }
}
void dfs2(long long n, long long cur, vector<vector<long long>>& g,
          long long frm, vector<long long>& min_apl, vector<long long>& child) {
  for (auto nex : g[cur]) {
    if (nex == frm) {
      continue;
    }
    min_apl[nex] = min_apl[cur] / child[cur];
    dfs2(n, nex, g, cur, min_apl, child);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<vector<long long>> g(n + 1);
  for (long long i = 1; i <= n - 1; i++) {
    long long x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  vector<long long> val(n + 1);
  vector<long long> child(n + 1, 0);
  vector<long long> min_apl(n + 1);
  dfs1(n, 1, val, g, -1, child);
  if (flag) {
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      ans += a[i];
    }
    cout << ans << endl;
    return 0;
  }
  min_apl[1] = val[1];
  dfs2(n, 1, g, -1, min_apl, child);
  long long mulp = 1e9;
  for (long long i = 2; i <= n; i++) {
    if (g[i].size() == 1) {
      long long tmp = a[i] / min_apl[i];
      mulp = min(mulp, tmp);
    }
  }
  long long ans = 0;
  for (long long i = 2; i <= n; i++) {
    if (g[i].size() == 1) {
      ans += a[i] - min_apl[i] * mulp;
    }
  }
  cout << ans << endl;
}
