#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200005;
typedef long long Tarr[Maxn];
Tarr Par, Adj, f, Link, head, sc, child;
long long w[Maxn], d[Maxn], tong;
int n, ce;
bool Av[Maxn];
void Addlink(int u, int v) {
  ce++;
  Adj[ce] = v;
  Link[ce] = head[u];
  head[u] = ce;
}
long long UC(long long a, long long b) {
  while (b != 0) {
    long long c = a % b;
    a = b;
    b = c;
  }
  return a;
}
void Visit(int u) {
  int i, v;
  Av[u] = false;
  if (w[u]) {
    f[u] = w[u];
    d[u] = 1;
    return;
  }
  long long nn = -1;
  i = head[u];
  while (i > 0) {
    v = Adj[i];
    if (Av[v]) {
      child[u]++;
      Par[v] = u;
      Visit(v);
      if (nn == -1)
        nn = f[v];
      else
        nn = min(nn, f[v]);
    }
    i = Link[i];
  }
  if (nn <= 0) {
    f[u] = 0;
    d[u] = 0;
    return;
  }
  long long bc = 0;
  i = head[u];
  while (i > 0) {
    v = Adj[i];
    if (Par[v] == u) {
      if (bc == 0)
        bc = d[v];
      else {
        long long g = UC(d[v], bc);
        g = d[v] / g;
        if (nn / g < bc) {
          f[u] = 0;
          d[u] = 0;
          return;
        }
        bc *= g;
      }
    }
    i = Link[i];
  }
  d[u] = bc * child[u];
  f[u] = bc * (nn / bc) * child[u];
  if (!f[u]) d[u] = 0;
}
int main() {
  scanf("%d/n", &n);
  tong = 0;
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    tong += w[i];
  }
  scanf("\n");
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    Addlink(u, v);
    Addlink(v, u);
  }
  memset(Par, 0, sizeof(Par));
  Par[1] = -1;
  memset(Av, true, sizeof(Av));
  Visit(1);
  cout << tong - f[1] << endl;
  return 0;
}
