#include <bits/stdc++.h>
long long INF;
using namespace std;
vector<int> V[100005];
long long ap[100005], mult[100005], sum = 0;
int n;
long long gcd(int a, int b) {
  while (b) {
    long long tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
void dfs(int id, int prev) {
  long long LCM = 1, GCD;
  int kk = -1;
  for (int i = 0; i < V[id].size(); i++) {
    int j = V[id][i];
    if (j == prev) continue;
    dfs(j, id);
    if (kk == -1) kk = i;
    GCD = gcd(LCM, mult[j]);
    if (LCM / GCD >= INF / mult[j]) {
      LCM = INF;
      break;
    } else
      LCM = LCM / GCD * mult[j];
  }
  if (kk == -1) {
    mult[id] = 1;
  } else if (LCM == INF) {
    ap[id] = 0;
    mult[id] = INF;
  } else {
    long long mini;
    int cnt = 0;
    for (int i = 0; i < V[id].size(); i++) {
      int j = V[id][i];
      if (j == prev) continue;
      cnt++;
      ap[j] = ap[j] / LCM * LCM;
      if (i == kk)
        mini = ap[j];
      else
        mini = min(mini, ap[j]);
    }
    ap[id] = mini * cnt;
    mult[id] = min(INF, LCM * cnt);
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &ap[i]), sum += ap[i];
  INF = sum + 1;
  for (int i = 2; i <= n; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    V[l].push_back(r);
    V[r].push_back(l);
  }
  dfs(1, -1);
  printf("%I64d\n", sum - ap[1]);
  return 0;
}
