#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100010];
long long values[100010];
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long x, long long y) {
  if (x <= 0 || y <= 0) return 0;
  if (gcd(x, y) == 0ll) return 0;
  return x * (y / gcd(x, y));
}
long long final_ans = 0;
long long max(long long a, long long b) { return a < b ? b : a; }
pair<long long, long long> dfs(int node, int parent) {
  int size = graph[node].size(), dist = 0;
  pair<long long, long long> ans = make_pair(0, 1);
  long long mini;
  bool flag = false;
  for (int i = 0; i < size; i++) {
    if (graph[node][i] == parent) continue;
    pair<long long, long long> temp = dfs(graph[node][i], node);
    ans.first += temp.first;
    if (dist == 0) {
      ans.second = temp.second;
      mini = temp.first;
    } else {
      ans.second = lcm(ans.second, temp.second);
      if (ans.second < 0 || ans.second >= 1000000000000000000LL) ans.second = 0;
      mini = mini < temp.first ? mini : temp.first;
    }
    dist++;
  }
  if (ans.second == 0) {
    final_ans = final_ans + ans.first;
    return make_pair(0, 0);
  }
  if (dist <= 1) {
    return make_pair(max(values[node], ans.first), ans.second);
  } else {
    long long num = mini, sons;
    sons = mini / ans.second;
    ans.second *= dist;
    long long diff = ans.first - sons * ans.second;
    final_ans = final_ans + diff;
    return make_pair(sons * ans.second, ans.second);
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> values[i];
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  pair<long long, long long> ans = dfs(1, 1);
  cout << final_ans << endl;
  return 0;
}
