#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
int read(int &x) {
  scanf("%d", &x);
  return x;
}
int readln() {
  int x;
  scanf("%d\n", &x);
  return x;
}
int readln(int &x) {
  scanf("%d\n", &x);
  return x;
}
const int MAX_N = 100005;
const long long oo = 1000000000000000000LL;
int n;
vector<int> e[MAX_N];
int nChild[MAX_N], p[MAX_N];
long long a[MAX_N], s[MAX_N], f[MAX_N], g[MAX_N], x[MAX_N];
long long gcd(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return 0;
  return (a / gcd(a, b)) * b;
}
void dfs(int u) {
  nChild[u] = 0;
  g[u] = 1;
  s[u] = a[u];
  int szE = int(e[u].size());
  for (int i = 0; i < int(szE); ++i) {
    int v = e[u][i];
    if (p[v] != 0) continue;
    p[v] = u;
    dfs(v);
    ++nChild[u];
    g[u] = lcm(g[u], f[v]);
    s[u] += s[v];
  }
  x[u] = (nChild[u] > 0) ? oo : a[u];
  if (g[u] < 1) x[u] = 0;
  for (int i = 0; i < int(szE); ++i) {
    int v = e[u][i];
    if (p[v] != u) continue;
    if (x[u] < 1) break;
    x[u] = min(x[u], (f[v] * x[v]) / g[u]);
  }
  f[u] = (nChild[u] > 0) ? (g[u] * nChild[u]) : 1;
}
int main() {
  scanf("%d", &n);
  for (int i = int(1); i <= int(n); ++i) a[i] = read();
  for (int i = 0; i < int(n - 1); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back((y));
    e[y].push_back((x));
  }
  memset(p, (0), sizeof(p));
  p[1] = -1;
  dfs(1);
  long long res = 0;
  res = s[1] - f[1] * x[1];
  cout << res << endl;
  return 0;
}
