#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> g[100010];
int deg[100010];
long long c[100010];
long long lm[100010];
long long gcd(long long a, long long b) {
  long long t;
  while (b > 0) t = a, a = b, b = t % a;
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void Find(int x, int fa) {
  if (g[x].size() == 1 && fa) {
    lm[x] = 1;
    return;
  }
  int t;
  deg[x] = g[x].size();
  for (int i = 0; i < deg[x]; i++)
    if ((t = g[x][i]) != fa) Find(t, x);
  long long L = 1;
  for (int i = 0; i < deg[x]; i++)
    if ((t = g[x][i]) != fa) L = lcm(L, lm[t]);
  for (int i = 0; i < deg[x]; i++)
    if ((t = g[x][i]) != fa)
      if (c[t] % lm[t]) return;
  c[x] = ~0ULL >> 1;
  for (int i = 0; i < deg[x]; i++)
    if ((t = g[x][i]) != fa) c[x] = min(c[x], c[t] / L * L);
  c[x] *= (x == 1) ? deg[x] : deg[x] - 1;
  lm[x] = L * (deg[x] - 1);
  if (lm[x] > c[x]) lm[x] = max(1LL, c[x]);
}
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) scanf("%I64d", &c[i]), sum += c[i];
  int x, y;
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  Find(1, 0);
  cout << sum - c[1] << endl;
}
