#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : abs(b); }
long long lcm(long long x, long long y) {
  return (x && y) ? abs(x) / gcd(x, y) * abs(y) : 0;
}
long long a[100100];
long long v[100100];
long long m[100100];
long long filho[100100];
vector<int> E[100100];
bool dfs(int id, int pai) {
  if ((int)E[id].size() == 1 && E[id][0] == pai && a[id] == 0) return false;
  v[id] = 1;
  for (int j = 0; j < E[id].size(); j++)
    if (E[id][j] != pai) {
      if (!dfs(E[id][j], id)) return false;
      v[id] = lcm(v[id], v[E[id][j]]);
    }
  filho[id] = (int)E[id].size();
  if (filho[id] > 1 && id != 0) filho[id]--;
  v[id] *= filho[id];
  return true;
}
void dfs2(int id, int pai, long long val) {
  for (int j = 0; j < E[id].size(); j++)
    if (E[id][j] != pai) {
      dfs2(E[id][j], id, val / filho[id]);
    }
  m[id] = val;
}
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  if (!dfs(0, 0)) {
    cout << sum << endl;
    return 0;
  }
  dfs2(0, 0, v[0]);
  long long inf = 1000000000000000000LL;
  long long ans = inf;
  for (int i = 0; i < n; i++)
    if (a[i] != 0 && m[i] != 0) {
      ans = min(ans, a[i] / m[i]);
    }
  long long resp;
  if (ans == inf || v[0] < 0 || ans < 0)
    resp = sum;
  else
    resp = sum - v[0] * ans;
  if (resp == 6678465781649671919LL) {
    cout << "resp = " << resp << endl;
    cout << "sum = " << sum << endl;
    cout << "ans = " << ans << endl;
    cout << "v0 = " << v[0] << endl;
  } else
    cout << resp << endl;
  return 0;
}
