#include <bits/stdc++.h>
using namespace std;
long long a[123456];
vector<long long> mul(123456);
vector<long long> mn(123456);
vector<vector<int> > adj(123456);
vector<int> leaf;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long dfs(int idx, int p) {
  long long LCM = 1;
  if (p == -1) {
    for (int i = 0; i < adj[idx].size(); i++)
      LCM = lcm(LCM, dfs(adj[idx][i], idx));
    LCM = LCM * (adj[idx].size());
  } else {
    if (adj[idx].size() == 1) {
      leaf.push_back(idx);
      return mul[idx] = 1;
    }
    for (int i = 0; i < adj[idx].size(); i++) {
      if (adj[idx][i] == p) continue;
      LCM = lcm(LCM, dfs(adj[idx][i], idx));
    }
    LCM = LCM * (adj[idx].size() - 1);
  }
  return mul[idx] = LCM;
}
void DFS(int idx, int p) {
  if (p == 0)
    mn[idx] = mn[p] * (adj[p].size());
  else
    mn[idx] = mn[p] * (adj[p].size() - 1);
  for (int j = 0; j < adj[idx].size(); j++) {
    if (adj[idx][j] == p) continue;
    DFS(adj[idx][j], idx);
  }
}
bool can(long long m) {
  for (int i = 0; i < leaf.size(); i++) {
    long long cur = m / mn[leaf[i]];
    if (cur > a[leaf[i]]) return 0;
  }
  return 1;
}
int main() {
  int n, x, y;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  long long LCM = dfs(0, -1);
  if (LCM == 0) {
    cout << sum << endl;
    return 0;
  }
  mn[0] = 1;
  for (int i = 0; i < adj[0].size(); i++) DFS(adj[0][i], 0);
  long long L = 0, R = sum / LCM, mid;
  while (L < R) {
    mid = (L + R) / 2;
    if (can(mid * LCM)) {
      L = mid;
      R++;
    } else
      R = mid - 1;
  }
  cout << sum - L * LCM << endl;
  return 0;
}
