#include <bits/stdc++.h>
const int maxn = 100001;
int n, i, u, v, top, a[maxn], spt[maxn], vt[maxn], st[maxn], l[maxn];
long long sum, ans, tmp, val[maxn], uc, bc, ss, min;
std::vector<int> adj[maxn];
void dfs() {
  top = 1;
  st[1] = 1;
  while (top > 0) {
    u = st[top];
    if (vt[u] < spt[u] && adj[u][vt[u]] == st[top - 1]) vt[u]++;
    if (vt[u] < spt[u]) {
      if (u == 1)
        tmp *= spt[u];
      else
        tmp *= (spt[u] - 1);
      if (tmp > sum) {
        ans = -1;
        return;
      }
      st[++top] = adj[u][vt[u]];
      vt[u]++;
    } else {
      if (spt[u] == 1 && u != 1) {
        val[u] = tmp;
        uc = tmp;
        l[++l[0]] = u;
      }
      top--;
      if (st[top] == 1)
        tmp /= spt[st[top]];
      else
        tmp /= (spt[st[top]] - 1);
    }
  }
}
long long ucln(long long a, long long b) {
  while (a > 0 && b > 0)
    if (a > b)
      a %= b;
    else
      b %= a;
  if (a > 0)
    return a;
  else
    return b;
}
int main() {
  std::cin >> n;
  for (i = 1; i <= n; i++) {
    std::cin >> a[i];
    sum += a[i];
  }
  for (i = 1; i < n; i++) {
    std::cin >> u >> v;
    spt[u]++;
    spt[v]++;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  tmp = 1;
  dfs();
  if (ans == -1) {
    std::cout << sum;
    return 0;
  }
  bc = val[l[1]];
  for (i = 2; i <= l[0]; i++) {
    uc = ucln(bc, val[l[i]]);
    bc /= uc;
    if (bc > double(sum) / val[i]) {
      std::cout << sum;
      return 0;
    }
    bc *= val[l[i]];
  }
  min = 1000000000000000000;
  for (i = 1; i <= l[0]; i++) {
    val[l[i]] = bc / val[l[i]];
    if (min > a[l[i]] / val[l[i]]) min = a[l[i]] / val[l[i]];
  }
  if (min == 0) {
    std::cout << sum;
    return 0;
  }
  for (i = 1; i <= l[0]; i++) ans += a[l[i]] - val[l[i]] * min;
  std::cout << ans;
  return 0;
}
