#include <bits/stdc++.h>
using namespace std;
long long frac[100005];
long long a[100005];
vector<int> leaf;
vector<int> v[100005];
bool mark[100005];
long long sum = 0;
bool SetFrac(int now, long long upto) {
  if (v[now].size() == 1 && mark[v[now][0]]) {
    if (upto > sum) return false;
    frac[now] = upto;
    return true;
  }
  mark[now] = true;
  for (int i = 0; i < v[now].size(); i++)
    if (!mark[v[now][i]]) {
      if (!SetFrac(v[now][i], upto * (v[now].size() - (now != 1))))
        return false;
    }
  return true;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int u, u2;
    cin >> u >> u2;
    v[u].push_back(u2);
    v[u2].push_back(u);
  }
  for (int i = 2; i <= n; i++)
    if (v[i].size() == 1) {
      leaf.push_back(i);
      if (a[i] == 0) {
        cout << sum << endl;
        return 0;
      }
    }
  if (!SetFrac(1, 1)) {
    cout << sum << endl;
    return 0;
  }
  long long Max = -1;
  for (int i = 0; i < leaf.size(); i++) {
    if (Max == -1 || a[leaf[i]] * frac[leaf[i]] < Max)
      Max = a[leaf[i]] * frac[leaf[i]];
  }
  long long Lcm = frac[leaf[0]];
  for (int i = 1; i < leaf.size() && Lcm < Max; i++)
    Lcm = lcm(Lcm, frac[leaf[i]]);
  long long ans = 0;
  long long root = Max / Lcm * Lcm;
  for (int i = 0; i < leaf.size(); i++)
    ans += a[leaf[i]] - root / frac[leaf[i]];
  cout << ans << endl;
  return 0;
}
