#include <bits/stdc++.h>
using namespace std;
long long w[100005];
vector<int> cl[100005];
bool visited[100005];
long long total = 0;
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) { return a / GCD(a, b) * b; }
long long calc(int v) {
  visited[v] = true;
  int u, nc = 0;
  long long minw = 0x7FFFFFFFFFFFFFLL, lcm = 1;
  for (int i = 0; i < cl[v].size(); ++i) {
    u = cl[v][i];
    if (!visited[u]) {
      ++nc;
      lcm = LCM(lcm, calc(u));
      if (minw > w[u]) minw = w[u];
    }
  }
  if (nc > 0) {
    w[v] = minw / lcm * lcm * nc;
    lcm *= nc;
  }
  if (w[v] == 0) {
    cout << total << endl;
    exit(0);
  }
  return lcm;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> w[i];
    total += w[i];
  }
  int x, y;
  for (int i = 1; i <= n - 1; ++i) {
    cin >> x >> y;
    cl[x].push_back(y);
    cl[y].push_back(x);
  }
  calc(1);
  cout << total - w[1] << endl;
}
