#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long LL_INF = LLONG_MAX;
const int MOD = 1000000007;
int plus_mod(int a, int b) {
  long long x = (long long)a + b;
  if (x < -1 * MOD)
    x += 2 * MOD;
  else if (x < 0)
    x += MOD;
  else if (x >= MOD)
    x -= MOD;
  return (int)x;
}
int minus_mod(int a, int b) {
  int x = a - b;
  if (x < -1 * MOD)
    x += 2 * MOD;
  else if (x < 0)
    x += MOD;
  else if (x >= MOD)
    x -= MOD;
  return x;
}
int multiply(int a, int b) {
  long long x = ((long long)a * b) % MOD;
  if (x < 0) x += MOD;
  return (int)x;
}
void print_vector(vector<int>& v) {
  for (int i : v) cout << i << " ";
  cout << '\n';
}
void print_array(int a[], int s, int e) {
  for (int i = s; i < e; ++i) cout << a[i] << " ";
  cout << '\n';
}
string to_binary(long long no, int len) {
  string ret = "";
  for (int i = 0; i < len; ++i) {
    if (no & 1)
      ret = "1" + ret;
    else
      ret = "0" + ret;
    no = no / 2;
  }
  return ret;
}
const int MAXN = 100005;
int A[MAXN];
vector<int> adj[MAXN];
int VIS[MAXN];
int PAR[MAXN];
long long W[MAXN];
long long NOS[MAXN];
long long ans;
int n;
long long GCD(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  return GCD(b, a % b);
}
long long LCM(long long a, long long b) {
  if (a == 0 && b == 0) return 0;
  return ((a * b) / GCD(a, b));
}
void DFS(int u) {
  VIS[u] = true;
  for (int v : adj[u]) {
    if (v != PAR[u]) {
      PAR[v] = u;
      DFS(v);
    }
  }
  if (adj[u].size() == 1) {
    W[u] = A[u];
    NOS[u] = 1;
  } else {
    long long gcd = 0, lcm = 1, min_wt = LL_INF, nc = 0;
    for (int v : adj[u]) {
      if (v != PAR[u]) {
        gcd = GCD(gcd, NOS[v]);
        lcm = LCM(lcm, NOS[v]);
        min_wt = min(min_wt, W[v]);
        if (lcm > min_wt) lcm = 0;
        nc++;
      }
    }
    long long val = 0;
    if (lcm) val = (min_wt / lcm) * lcm;
    for (int v : adj[u]) {
      if (v != PAR[u]) ans += W[v] - val;
    }
    W[u] = val * nc;
    NOS[u] = lcm * nc;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  T = 1;
  while (T--) {
    ans = 0;
    cin >> n;
    memset(VIS, 0, sizeof(VIS));
    for (int i = 1; i < n + 1; ++i) cin >> A[i];
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    PAR[1] = -1;
    DFS(1);
    cout << ans << '\n';
  }
}
