#include <bits/stdc++.h>
using namespace std;
int n, u, v;
long long sum, V[100010], C[100010];
vector<vector<int> > node, node2;
long long getGcd(long long a, long long b) {
  long long x = a, y = b, z = a % b;
  while (z) {
    x = y;
    y = z;
    z = x % y;
  }
  return y;
}
long long dfs(int parent, int here) {
  if (V[here]) {
    return 1LL;
  }
  vector<long long> child;
  for (int i = 0; i < node[here].size(); ++i) {
    int there = node[here][i];
    if (parent != there) {
      node2[here].push_back(there);
      child.push_back(dfs(here, there));
    }
  }
  if (child.empty()) {
    printf("%lld\n", sum);
    exit(0);
  }
  long long lcm = child[0];
  for (int i = 1; i < (int)child.size(); ++i) {
    long long gcd = getGcd(lcm, child[i]);
    lcm = lcm * child[i] / gcd;
    if (lcm > sum) {
      printf("%lld\n", sum);
      exit(0);
    }
  }
  lcm = lcm * (long long)child.size();
  if (lcm > sum) {
    printf("%lld\n", sum);
    exit(0);
  }
  return lcm;
}
void calc(int here, long long cur) {
  if (node2[here].empty()) {
    C[here] = cur;
    return;
  }
  long long left = cur / (long long)node2[here].size();
  for (int i = 0; i < node2[here].size(); ++i) {
    int there = node2[here][i];
    calc(there, left);
  }
}
bool isPossible() {
  for (int i = 1; i <= n; ++i) {
    if (C[i] && C[i] > V[i]) {
      return false;
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  node.resize(n + 1);
  node2.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &V[i]);
    sum += V[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d", &u, &v);
    node[u].push_back(v);
    node[v].push_back(u);
  }
  long long mul = dfs(1, 1);
  long long lo = 1, hi = 987654321LL;
  while (lo < hi) {
    long long mid = (lo + hi) / 2;
    calc(1, mul * mid);
    if (isPossible()) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }
  --lo;
  calc(1, mul * lo);
  long long sol = 0;
  for (int i = 1; i <= n; ++i) {
    sol += V[i] - C[i];
  }
  printf("%lld\n", sol);
}
