#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 1;
const int maxn = 1e5 + 1;
int n, w[maxn], cnt[maxn];
long long sz[maxn], sum[maxn], valz[maxn];
vector<int> Adj[maxn], child[maxn];
bool visited[maxn], flag;
long long res = 0;
void Input() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> w[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    Adj[u].push_back(v);
    Adj[v].push_back(u);
  }
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  while (b != 0) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long LCM(long long a, long long b) {
  long long s = a * b;
  if (gcd(a, b) == 0) return 0;
  s /= gcd(a, b);
  return s;
}
void BuildTree(int u) {
  visited[u] = true;
  sz[u] = 1;
  cnt[u] = 0;
  sum[u] = w[u];
  for (int v : Adj[u])
    if (!visited[v]) {
      BuildTree(v);
      if (!flag) return;
      cnt[u]++;
      sz[u] = LCM(sz[u], sz[v]);
      child[u].push_back(v);
      sum[u] += sum[v];
      if (sz[u] > sum[u]) {
        flag = false;
        return;
      }
    }
  cnt[u] = cnt[u] == 0 ? 1 : cnt[u];
  sz[u] = sz[u] * (long long)cnt[u];
}
void DFSVisit(int u) {
  for (int v : child[u]) {
    DFSVisit(v);
    long long s = sum[v] * cnt[u];
    sum[u] = min(sum[u], s - (s % sz[u]));
  }
}
void Solve() {
  fill(visited + 1, visited + 1 + n, false);
  flag = true;
  BuildTree(1);
  if (!flag) {
    long long sum = 0;
    for (int i = 1; i <= n; i++) sum += (long long)w[i];
    cout << sum;
    return;
  }
  fill(valz + 1, valz + 1 + n, 0);
  DFSVisit(1);
  long long s = 0;
  for (int i = 1; i <= n; i++) s += (long long)w[i];
  s -= sum[1];
  cout << s;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  Input();
  Solve();
}
