#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long inf = 1e18;
int n;
vector<int> g[N];
long long sz[N], tot;
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
pair<long long, long long> dfs(int u = 1, int p = -1) {
  vector<pair<long long, long long> > v;
  for (auto to : g[u])
    if (to != p) v.push_back(dfs(to, u));
  if (!v.size()) return make_pair(sz[u], 1ll);
  long long lcm = 1, mn = inf;
  for (auto it : v) {
    long long g = gcd(lcm, it.second);
    lcm = lcm / g * it.second;
    mn = min(mn, it.first);
  }
  if (lcm > mn * v.size()) {
    printf("%I64d", tot);
    exit(0);
  }
  return make_pair(mn / lcm * lcm * v.size(), lcm * v.size());
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &sz[i]);
  for (int i = 0, u, v; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v), g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) tot += sz[i];
  printf("%I64d\n", tot - dfs().first);
  return 0;
}
