#include <bits/stdc++.h>
using namespace std;
__inline bool nextInt(int &val) {
  char ch;
  int sgn = 1;
  while ((ch = getchar()) != EOF) {
    if (ch == '-') sgn = -1;
    if (ch >= '0' && ch <= '9') break;
  }
  if (ch == EOF) return false;
  val = (int)(ch - '0');
  while (true) {
    ch = getchar();
    if (ch >= '0' && ch <= '9') {
      val = 10 * val + (int)(ch - '0');
    } else
      break;
  }
  val *= sgn;
  return true;
}
__inline bool nextString(string &s) {
  char ch;
  while ((ch = getchar()) != EOF) {
    if (ch >= 33 && ch <= 126) break;
  }
  if (ch == EOF) return false;
  s = string(1, ch);
  while (true) {
    ch = getchar();
    if (ch >= 33 && ch <= 126) {
      s = s + string(1, ch);
    } else
      break;
  }
  return true;
}
int n, sub[111888], a[111888], c[111888];
long long coef[111888], INF = 1000000001LL, INF2 = 1000000001LL * 10001LL;
vector<int> e[111888];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  long long d = gcd(a, b);
  long long tmp = a / d * b;
  if (tmp >= INF) tmp = INF;
  return tmp;
}
void dfs(int u, int pu) {
  c[u] = 0;
  if (pu != -1 && ((int)(e[u]).size()) == 1) {
    sub[u] = 1;
    coef[u] = 1;
  } else {
    long long d = -1;
    for (int ie = 0; ie < (((int)(e[u]).size())); ie++) {
      int v = e[u][ie];
      if (v != pu) {
        dfs(v, u);
        c[u]++;
        if (d == -1)
          d = coef[v];
        else
          d = gcd(d, coef[v]);
        sub[u] += sub[v];
      }
    }
    long long prod = 1LL;
    for (int ie = 0; ie < (((int)(e[u]).size())); ie++) {
      int v = e[u][ie];
      if (v != pu) {
        prod = lcm(prod, coef[v]);
        if (prod >= INF) break;
      }
    }
    coef[u] = c[u] * prod;
    if (coef[u] >= INF) coef[u] = INF;
  }
}
void dfs2(int u, int pu) {
  if (pu != -1)
    if (coef[pu] < INF)
      coef[u] = coef[pu] / c[pu];
    else
      coef[u] = INF;
  for (int ie = 0; ie < (((int)(e[u]).size())); ie++) {
    int v = e[u][ie];
    if (v != pu) dfs2(v, u);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d", &n);
  for (int i = 0; i < (n); i++) scanf("%d", a + i);
  for (int i = 0; i < (n - 1); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dfs(0, -1);
  dfs2(0, -1);
  long long ret = 0, k = 0;
  if (coef[0] < INF) {
    long long low = 0, high = 1000000000LL, mid;
    while (low <= high) {
      mid = low + (high - low) / 2;
      bool ok = true;
      for (int u = 1; u < n && ok; u++)
        if (c[u] == 0 && mid * coef[u] > a[u]) {
          ok = false;
        }
      if (ok) {
        k = ((k) > (mid) ? (k) : (mid));
        low = mid + 1;
      } else
        high = mid - 1;
    }
  }
  for (int i = 0; i < (n); i++)
    if (c[i] == 0) ret += a[i] - coef[i] * k;
  cout << ret << endl;
  return 0;
}
