#include <bits/stdc++.h>
using namespace std;
vector<int> E[100005];
long long d[100005];
long long mx[100005];
long long under[100005];
long long val[100005];
long long lost;
int n;
inline char muk(long long a, long long b) {
  long long cst = (10000000000000000ll);
  if (a == 0) return 1;
  if (cst / a >= b) return 1;
  return 0;
}
inline long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void dfs(int u, int up) {
  int sinovi = 0;
  for (int i = 0; i < E[u].size(); i++)
    if (E[u][i] != up) {
      sinovi++;
      dfs(E[u][i], u);
    }
  if (sinovi == 0) {
    under[u] = val[u];
    d[u] = 1;
    mx[u] = val[u];
    return;
  }
  d[u] = 0;
  for (int i = 0; i < E[u].size(); i++)
    if (E[u][i] != up) under[u] += under[E[u][i]];
  for (int i = 0; i < E[u].size(); i++)
    if (E[u][i] != up) {
      if (d[u] == 0)
        d[u] = d[E[u][i]];
      else {
        long long gd = gcd(d[u], d[E[u][i]]);
        d[u] /= gd;
        if (muk(d[u], d[E[u][i]]))
          d[u] = d[u] * d[E[u][i]];
        else
          d[u] = (10000000000000000ll);
      }
    }
  long long now = 1ll << 50;
  for (int i = 0; i < E[u].size(); i++)
    if (E[u][i] != up) now = min(now, (mx[E[u][i]] / d[u]) * d[u]);
  mx[u] = now * sinovi;
  for (int i = 0; i < E[u].size(); i++)
    if (E[u][i] != up) lost += mx[E[u][i]] - now;
  if (muk(d[u], sinovi))
    d[u] *= sinovi;
  else
    d[u] = (10000000000000000ll);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &val[i]);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    E[x].push_back(y);
    E[y].push_back(x);
  }
  dfs(1, 0);
  printf("%I64d\n", under[1] - mx[1]);
}
