#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 100;
const int inf = 0x7fffff;
int n;
long long v[N];
pair<long long, long long> a[N];
vector<int> E[N];
long long ans;
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
void dfs(int x, int f) {
  long long lcm = 1, ma = -1, y;
  for (int i = 0; i < (E[x].size()); i++)
    if ((y = E[x][i]) != f) dfs(y, x);
  for (int i = 0; i < (E[x].size()); i++)
    if ((y = E[x][i]) != f) {
      while (gcd(lcm, a[y].first) == 0)
        ;
      if (a[y].first) lcm = lcm / gcd(lcm, a[y].first) * a[y].first;
    }
  for (int i = 0; i < (E[x].size()); i++)
    if ((y = E[x][i]) != f)
      ma = ((ma == -1) ? a[y].second / lcm : min(ma, a[y].second / lcm));
  a[x].first = a[x].second = 0;
  for (int i = 0; i < (E[x].size()); i++)
    if ((y = E[x][i]) != f) {
      ans += a[y].second - ma * lcm;
      a[x].second += ma * lcm;
      a[x].first += lcm;
    }
  if (ma == -1) a[x] = make_pair(1, v[x]);
}
int main() {
  cin >> n;
  for (int i = 0; i < (n); i++) cin >> v[i];
  for (int i = 0; i < (n); i++) E[i].clear();
  for (int i = 0; i < (n - 1); i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  ans = 0;
  dfs(0, -1);
  cout << ans << endl;
  return 0;
}
