#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = 1e15;
int n;
int a[maxn];
vector<int> g[maxn];
long long ans = 0;
long long dp[maxn], x[maxn];
long long k[maxn];
void ReadInput() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
}
long long GCD(long long a, long long b) {
  while (b != 0) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
void dfs(int u, int par) {
  vector<int> vt;
  bool isleaf = true;
  dp[u] = a[u];
  x[u] = 1;
  for (int v : g[u])
    if (v != par) {
      isleaf = false;
      dfs(v, u);
      vt.push_back(v);
    }
  if (isleaf) return;
  long long LCM = 1;
  for (int id : vt) {
    if (LCM == INF) continue;
    if (x[id] == INF) LCM = INF;
    long long gcd = GCD(LCM, x[id]);
    LCM /= gcd;
    if (LCM >= (INF + x[id]) / x[id]) {
      LCM = INF;
      continue;
    }
    LCM = LCM * x[id];
  }
  if (LCM == INF) {
    for (int id : vt) ans += dp[id];
    dp[u] = 0;
  } else {
    long long mn = INF;
    for (int id : vt) {
      long long d = dp[id] / LCM;
      long long t = (dp[id] - d * LCM) / x[id];
      ans += x[id] * t;
      dp[id] -= x[id] * t;
      mn = min(dp[id], mn);
    }
    for (int id : vt) {
      if (dp[id] > mn) {
        long long t = dp[id] - mn;
        dp[id] = mn;
        ans += t;
      }
      dp[u] += dp[id];
    }
  }
  if (LCM >= (INF + (long long)vt.size()) / (long long)vt.size())
    x[u] = INF;
  else
    x[u] = LCM * (long long)vt.size();
}
void Solve() {
  dfs(1, 0);
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}
