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