#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
const long long MAXV = 2000000000000000;
int a[MAXN];
vector<vector<int> > v;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
bool overflow = false;
long long sum = 0;
long long lcm(long long a, long long b) {
  long long res = a / gcd(a, b);
  if (MAXV / b < res) {
    cout << sum << endl;
    exit(0);
  }
  res *= b;
  return res;
}
pair<long long, long long> solve(int cur, int p) {
  long long sum = 0;
  vector<pair<long long, long long> > tmp;
  for (int i = 0; i < v[cur].size(); i++)
    if (v[cur][i] != p) tmp.push_back(solve(v[cur][i], cur));
  if (tmp.empty()) return make_pair(a[cur], 1);
  long long step = 1;
  for (int i = 0; i < tmp.size(); i++) step = lcm(step, tmp[i].second);
  sum = MAXV;
  for (int i = 0; i < tmp.size(); i++)
    sum = min(sum, tmp[i].first / step * step);
  sum *= tmp.size();
  step *= tmp.size();
  return make_pair(sum, step);
}
long long count(int cur, int p, long long cnt) {
  long long res = 0;
  int child = v[cur].size();
  if (p != -1) child--;
  if (child == 0) return a[cur] - cnt;
  for (int i = 0; i < v[cur].size(); i++)
    if (v[cur][i] != p) res += count(v[cur][i], cur, cnt / child);
  return res;
}
int main() {
  int n;
  scanf("%d", &n);
  v.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  long long res = solve(0, -1).first;
  cout << count(0, -1, res) << endl;
  return 0;
}
