#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); }
long long lcm(long long n, long long m) {
  if (gcd(n, m) == 0) return 1;
  return n / gcd(n, m) * m;
}
long long a[100030];
int num[100030];
vector<int> G[100030];
void dfs(int u, int fa) {
  if (G[u].size() == 1 && G[u][0] == fa) {
    num[u] = 1;
    return;
  }
  long long lc = 1;
  int son = 0;
  long long Min = 1e18;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != fa) {
      dfs(G[u][i], u);
      son++;
      lc = lcm(lc, num[v]);
      Min = min(Min, a[v]);
    }
  }
  num[u] = lc * son;
  long long sum = Min * son;
  if (num[u] == 0)
    sum = 0;
  else
    sum = sum / num[u] * num[u];
  a[u] = sum;
}
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    sum += a[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    ;
    G[v].push_back(u);
    ;
  }
  dfs(1, -1);
  cout << sum - a[1] << endl;
  return 0;
}
