#include <bits/stdc++.h>
using namespace std;
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(max(a, b), min(a, b))) * b);
}
long long val[100009];
vector<vector<int> > AdjList;
long long nodo_lcm[100009];
long long sum = 0;
bool temp;
long long dfs(int nodo, int padre) {
  long long ans = 1;
  int k = 0;
  for (int i = 0; i < AdjList[nodo].size(); i++) {
    int v = AdjList[nodo][i];
    if (v != padre) {
      ans = lcm(ans, dfs(v, nodo));
      k++;
    }
  }
  if (k == 0) {
    return nodo_lcm[nodo] = 1;
  }
  if (ans * k > sum) {
    temp = true;
  }
  if (temp) nodo_lcm[nodo] = 1;
  return nodo_lcm[nodo] = k * ans;
}
long long sol(int nodo, int padre) {
  long long ans = 10000000000000000LL;
  int k = 0;
  for (int i = 0; i < AdjList[nodo].size(); i++) {
    int v = AdjList[nodo][i];
    if (v != padre) {
      ans = min(ans, sol(v, nodo));
      k++;
    }
  }
  if (k == 0) return val[nodo];
  long long tt = (ans * k) / nodo_lcm[nodo];
  return tt * nodo_lcm[nodo];
}
int main() {
  int n, x, y;
  cin >> n;
  AdjList.assign(n, vector<int>());
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    val[i] = x;
    sum += val[i];
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &x, &y);
    x--, y--;
    AdjList[x].push_back(y);
    AdjList[y].push_back(x);
  }
  dfs(0, -1);
  if (temp) {
    cout << sum << endl;
    return 0;
  }
  cout << sum - sol(0, -1) << endl;
  return 0;
}
