#include <bits/stdc++.h>
using namespace std;
const int maxN = 100010;
int a[maxN], n, l[maxN], parent[maxN];
long long res, c[maxN], maxv[maxN];
vector<int> adj[maxN];
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long lcm(long long x, long long y) {
  if ((x == y) && (x == 0)) return 0;
  long long tmp = x / gcd(x, y);
  tmp = tmp * y;
  return tmp;
}
void dfs() {
  stack<int> s;
  s.push(1);
  for (int i = 1; i <= n; ++i) l[i] = 0;
  while (!s.empty()) {
    int u = s.top();
    if (u == 61518) u = 61518;
    if (l[u] == 0) c[u] = 1;
    if (l[u] == adj[u].size()) {
      s.pop();
      if (adj[u].size() != 1)
        if (u != 1)
          c[u] = c[u] * (l[u] - 1);
        else
          c[u] = c[u] * l[u];
      c[parent[u]] = lcm(c[parent[u]], c[u]);
      if (c[parent[u]] < 0) c[parent[u]] = 0;
      continue;
    }
    while (l[u] < adj[u].size()) {
      int v = adj[u][l[u]];
      ++l[u];
      if (parent[u] == v) continue;
      parent[v] = u;
      s.push(v);
      break;
    }
  }
}
void getres() {
  stack<int> s;
  s.push(1);
  for (int i = 1; i <= n; ++i) l[i] = 0;
  while (!s.empty()) {
    int u = s.top();
    if (l[u] == 0)
      if ((adj[u].size() == 1) && (u != 1))
        if (c[u] != 0)
          maxv[u] = a[u] / c[u];
        else
          maxv[u] = 0;
      else
        maxv[u] = -1;
    if (l[u] == adj[u].size()) {
      s.pop();
      if (maxv[parent[u]] == -1)
        maxv[parent[u]] = maxv[u];
      else
        maxv[parent[u]] = min(maxv[parent[u]], maxv[u]);
    }
    while (l[u] < adj[u].size()) {
      int v = adj[u][l[u]];
      ++l[u];
      if (parent[u] == v) continue;
      int k;
      if (u == 1)
        k = adj[u].size();
      else
        k = adj[u].size() - 1;
      s.push(v);
      c[v] = c[u] / k;
      break;
    }
  }
  res = -(c[1] * maxv[1]);
  for (int i = 1; i <= n; ++i) res = res + a[i];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n - 1; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs();
  getres();
  printf("%I64d", res);
  fclose(stdin);
}
