#include <bits/stdc++.h>
using namespace std;
vector<set<size_t>> e;
vector<unsigned long long> l;
void sum(size_t v) {
  for (auto evi = e[v].begin(); evi != e[v].end(); evi++)
    sum(*evi), l[v] += l[*evi];
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  return a == 0 ? b : gcd(b % a, a);
}
unsigned long long lcmu(unsigned long long a, unsigned long long b,
                        unsigned long long u) {
  unsigned long long d = gcd(a, b);
  if (u / d / (a / d) / (b / d) == 0) return 0;
  return d * (a / d) * (b / d);
}
unsigned long long fm(size_t v) {
  if (e[v].empty()) return 1;
  unsigned long long c = 1;
  for (auto evi = e[v].begin(); evi != e[v].end(); evi++) {
    unsigned long long t = fm(*evi);
    if (t == 0) return 0;
    c = lcmu(c, t, l[*evi]);
    if (c == 0) return 0;
  }
  return l[v] / c / e[v].size() == 0 ? 0 : c * e[v].size();
}
unsigned long long k(size_t v, unsigned long long t) {
  if (e[v].empty()) return l[v] / t;
  unsigned long long r = -1;
  for (auto evi = e[v].begin(); evi != e[v].end(); evi++)
    r = min(r, k(*evi, t / e[v].size()));
  return r;
}
void clear(size_t v) {
  for (auto evi = e[v].begin(); evi != e[v].end(); evi++)
    e[*evi].erase(v), clear(*evi);
}
int main() {
  size_t n;
  while (cin >> n) {
    l.clear();
    e.clear();
    l.resize(n, 0);
    e.resize(n, set<size_t>());
    for (size_t i = 0; i < n; i++) cin >> l[i];
    for (size_t j = n - 1; j--;) {
      int a, b;
      cin >> a >> b;
      e[a - 1].insert(b - 1);
      e[b - 1].insert(a - 1);
    }
    clear(0);
    sum(0);
    unsigned long long t = fm(0);
    cout << (t == 0 ? l[0] : l[0] - t * k(0, t)) << endl;
  }
}
