#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
const unsigned long long inf = 100000000000000ULL;
int cnt[N];
unsigned long long multiple[N];
vector<int> za[N];
int n;
unsigned long long res = 0;
unsigned long long nwd(unsigned long long a, unsigned long long b) {
  if (b == 0) return a;
  return nwd(b, a % b);
}
bool error = false;
unsigned long long gogo(int vert, int par = -1) {
  if (error) return 0;
  multiple[vert] = 1;
  int childCount = 0;
  unsigned long long najmn = inf;
  unsigned long long sum = 0;
  for (typeof((za[vert]).begin()) p = ((za[vert]).begin());
       p != (za[vert]).end(); ++p)
    if (*p != par) {
      ++childCount;
      unsigned long long child = gogo(*p, vert);
      if (error || child == 0) return 0;
      unsigned long long nmult = multiple[*p];
      unsigned long long nwdk = nwd(nmult, multiple[vert]);
      multiple[vert] /= nwdk;
      if (multiple[vert] > inf / nmult) {
        error = true;
      } else {
        multiple[vert] = multiple[vert] * nmult;
      }
      sum += child;
      if (child < najmn) najmn = child;
    }
  if (childCount == 0) {
    return cnt[vert];
  } else {
    if (error) return 0;
    unsigned long long x = najmn / multiple[vert];
    unsigned long long curVert = x * multiple[vert];
    unsigned long long shouldBe = curVert * childCount;
    res += sum - shouldBe;
    multiple[vert] *= childCount;
    return shouldBe;
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin >> n;
  unsigned long long sum = 0;
  for (int i = (1); i <= (n); ++i) {
    cin >> cnt[i];
    sum += cnt[i];
  }
  for (int i = 0; i < (n - 1); ++i) {
    int a, b;
    cin >> a >> b;
    za[a].push_back(b);
    za[b].push_back(a);
  }
  unsigned long long ile = gogo(1);
  if (error || ile == 0)
    cout << sum << endl;
  else
    cout << res << endl;
}
