#include <bits/stdc++.h>
using namespace std;
long long int infty;
long long int gcd(long long int p, long long int q) {
  if (q == 0) return p;
  return gcd(q, p % q);
}
long long int lcm(long long int p, long long int q) {
  long long int d = gcd(p, q);
  if (infty / p < q / d) return infty;
  return (q / d) * p;
}
struct vert {
  int a;
  long long int min;
  bool visit;
};
vert v[100001];
vector<int> viz[100001];
void dfs(int x) {
  v[x].visit = true;
  v[x].min = 1;
  if (v[x].a) return;
  int nx = viz[x].size();
  if (nx == 1 && x != 1) {
    v[x].min = infty;
    return;
  }
  for (int i = 0; i < nx; i++) {
    int y = viz[x][i];
    if (v[y].visit) continue;
    dfs(y);
    v[x].min = lcm(v[x].min, v[y].min);
  }
  if (x != 1) nx--;
  if (nx * v[x].min >= infty)
    v[x].min = infty;
  else
    v[x].min *= nx;
  return;
}
void nova_dfs(int x) {
  v[x].visit = true;
  if (v[x].a) return;
  int nx = viz[x].size();
  for (int i = 0; i < nx; i++) {
    int y = viz[x][i];
    if (v[y].visit) continue;
    if (x == 1)
      v[y].min = v[x].min / nx;
    else
      v[y].min = v[x].min / (nx - 1);
    nova_dfs(y);
  }
  return;
}
int main() {
  infty = 1;
  for (int i = 0; i < 13; i++) infty *= 10;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i].a);
    v[i].visit = false;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    viz[x].push_back(y);
    viz[y].push_back(x);
  }
  dfs(1);
  if (v[1].min == infty) {
    long long int s = 0;
    for (int i = 1; i <= n; i++) s += v[i].a;
    cout << s << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) v[i].visit = false;
  nova_dfs(1);
  long long int t = infty;
  for (int i = 1; i <= n; i++)
    if (v[i].a && t > v[i].a / v[i].min) t = v[i].a / v[i].min;
  long long int S = 0;
  for (int i = 1; i <= n; i++)
    if (v[i].a) S += v[i].a - v[i].min * t;
  cout << S << endl;
  return 0;
}
