#include <bits/stdc++.h>
using namespace std;
const int MAXN = int(1e5) + 10;
const long long INF = (long long)1e13 + 1;
int n;
vector<int> to[MAXN];
int sz[MAXN];
int a[MAXN];
long long sum[MAXN];
long long lcm[MAXN];
long long mx[MAXN];
long long gcd(long long a, long long b) {
  return (b == 0) ? (a) : gcd(b, a % b);
}
long long _lcm(long long a, long long b) {
  long long g = gcd(a, b);
  if (INF / b < a / g) return INF;
  return a / gcd(a, b) * b;
}
void dfs(int v, int pr) {
  sum[v] = a[v];
  lcm[v] = 1;
  mx[v] = -1;
  long long curmn = INF;
  for (int i = 0; i < int(to[v].size()); ++i) {
    int u = to[v][i];
    if (u == pr) continue;
    dfs(u, v);
    sum[v] += sum[u];
    lcm[v] = _lcm(lcm[v], lcm[u]);
    curmn = min(curmn, mx[u]);
  }
  if (sz[v] == 0)
    mx[v] = a[v];
  else {
    lcm[v] *= sz[v];
    mx[v] = min(curmn * sz[v], sum[v]);
    mx[v] -= mx[v] % lcm[v];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < int(n); ++i) scanf("%d", &a[i]);
  for (int i = 0; i < int(n - 1); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  for (int i = 0; i < int(n); ++i) sz[i] = to[i].size() - 1;
  sz[0] = to[0].size();
  dfs(0, -1);
  cout << sum[0] - mx[0] << '\n';
  return 0;
}
