#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
long long n;
long long a[MAXN];
vector<int> G[MAXN];
long long cnt[MAXN], mx[MAXN];
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
void dfs(int u, int fa) {
  int nchild = 0;
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (v == fa) continue;
    dfs(v, u);
    if (nchild == 0) {
      mx[u] = mx[v];
      cnt[u] = cnt[v];
    } else {
      if (cnt[u] < 1e14) cnt[u] = lcm(cnt[u], cnt[v]);
      mx[u] = min(mx[u], mx[v]) / cnt[u] * cnt[u];
    }
    nchild++;
  }
  if (nchild == 0)
    mx[u] = a[u], cnt[u] = 1;
  else {
    mx[u] = nchild * mx[u];
    if (cnt[u] < 1e14) cnt[u] = nchild * cnt[u];
  }
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += a[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  cout << ans - mx[1] << endl;
  return 0;
}
