#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
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) { return a / gcd(a, b) * b; }
long long value[N], d[N], du[N], n;
vector<long long> adj[N];
bool dfs(int now, int f) {
  long long _lcm = 1;
  for (int i = 0; i < adj[now].size(); ++i) {
    if (adj[now][i] != f) {
      ++du[now];
      if (!dfs(adj[now][i], now)) return 0;
      _lcm = lcm(_lcm, d[adj[now][i]]);
      if (_lcm > 1e13) return 0;
    }
  }
  if (du[now] == 0) {
    d[now] = 1ll;
    return 1;
  } else {
    d[now] = du[now] * _lcm;
    if (d[now] > 1e13) return 0;
    return 1;
  }
}
void dfs2(int ver, long long D, int f) {
  if (du[ver] == 0) {
    value[ver] = value[ver] / D * D;
    return;
  }
  long long _min = 1ll << 60, _D = D / du[ver];
  for (int i = 0; i < adj[ver].size(); ++i) {
    int _ver = adj[ver][i];
    if (_ver != f) {
      dfs2(_ver, _D, ver);
      _min = min(_min, value[_ver]);
    }
  }
  value[ver] = _min * du[ver];
}
int main() {
  long long sum = 0;
  cin >> n;
  int i, j;
  for (i = 1; i <= n; ++i) {
    cin >> value[i];
    sum += value[i];
  }
  for (i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if (!dfs(1, -1))
    cout << sum << endl;
  else {
    dfs2(1, d[1], -1);
    cout << sum - value[1] << endl;
  }
  return 0;
}
