#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long hcf(long long a, long long b) {
  if (b > a) swap(a, b);
  return a * b / gcd(a, b);
}
long long sz[MAXN];
vector<long long> V[MAXN];
long long dp[MAXN];
long long ans;
long long N, a, b, T;
long long A[MAXN];
void dfs(int x, int p) {
  if ((long long)V[x].size() == 1 && x != 1) {
    sz[x] = 1;
    dp[x] = A[x];
    return;
  }
  long long running_gcd = 1;
  for (auto v : V[x])
    if (v != p) {
      dfs(v, x);
      running_gcd = hcf(running_gcd, sz[v]);
    }
  long long max_fact = 1e18;
  for (auto v : V[x])
    if (v != p) {
      long long rep = dp[v] / running_gcd;
      if (running_gcd > 1e15) {
        cout << T;
        exit(0);
      }
      max_fact = min(max_fact, rep * running_gcd);
    }
  for (auto v : V[x])
    if (v != p) ans += dp[v] - max_fact;
  long long child = (long long)V[x].size() - 1;
  if (x == 1) ++child;
  dp[x] = max_fact * child;
  sz[x] = child * running_gcd;
}
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
    T += A[i];
  }
  for (int i = 0; i < N - 1; ++i) {
    cin >> a >> b;
    V[a].emplace_back(b);
    V[b].emplace_back(a);
  }
  dfs(1, -1);
  cout << ans;
}
