#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
long long ans;
int n;
vector<int> edge[maxn];
long long val[maxn];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long son[maxn];
long long dfs(int u, int fa) {
  long long LCM = 1, mi = 1e18, s = 0;
  int cnt = 0, ok = 1;
  for (int i = 0; i < edge[u].size(); i++) {
    int v = edge[u][i];
    if (v == fa) continue;
    long long t = dfs(v, u);
    LCM = lcm(LCM, son[v]);
    mi = min(mi, t);
    s += t;
    cnt++;
  }
  if (!cnt) {
    son[u] = 1;
    return val[u];
  }
  if (!LCM) {
    ans += s;
    return 0;
  }
  son[u] = LCM * cnt;
  long long ret = mi / LCM * LCM * cnt;
  ans += s - ret;
  return ret;
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    edge[x].push_back(y);
    edge[y].push_back(x);
  }
  dfs(1, 0);
  printf("%I64d\n", ans);
  return 0;
}
