#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e14;
vector<int> v[100009];
long long apples[100009];
long long ans = 0, ssum = 0;
int visited[100009];
struct node {
  long long total;
  long long divide;
};
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long LCM(long long a, long long b) {
  long long gg = gcd(a, b);
  long long chc = (a * b) / gg;
  if (chc > inf) return inf;
  return (a / gg) * b;
}
node DFS(int start) {
  int i;
  visited[start] = 1;
  long long sum = 0, c = 0, minm = inf, lcm = 1;
  struct node rec;
  for (i = 0; i < v[start].size(); i++) {
    if (visited[v[start][i]] == 0) {
      rec = DFS(v[start][i]);
      lcm = LCM(lcm, rec.divide);
      minm = min(rec.total, minm);
      sum = sum + rec.total;
      c++;
    }
  }
  if (c == 0) return (node){apples[start], 1};
  minm = (minm / lcm) * lcm;
  long long chc = c * lcm;
  if (chc > inf)
    lcm = inf;
  else
    lcm = lcm * c;
  ans += sum - minm * c;
  return (node){minm * c, lcm};
}
int main() {
  int n, i, a, b;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> apples[i];
  for (i = 1; i <= n - 1; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  struct node aa = DFS(1);
  printf("%lld\n", ans);
  return 0;
}
