#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
int a[100010], flag;
vector<int> e[100010];
long long sum, val[100010];
void dfs(int u, int pre, long long k) {
  int t;
  if (u == pre)
    t = e[u].size();
  else
    t = e[u].size() - 1;
  if (k > sum) {
    flag = 1;
    return;
  }
  if (t == 0) {
    val[u] = k;
  }
  for (int i = 0; i < (int)e[u].size(); i++) {
    int v = e[u][i];
    if (v == pre) continue;
    dfs(v, u, k * t);
  }
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int main() {
  int n, x, y;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(1, 1, 1);
  if (flag)
    printf("%I64d\n", sum);
  else {
    long long Min = sum;
    long long t = 1;
    for (int i = 1; i <= n; i++) {
      if (val[i]) {
        if (sum / val[i] >= a[i]) Min = min(Min, val[i] * a[i]);
        t /= gcd(val[i], t);
        if (sum / val[i] < t) {
          printf("%I64d\n", sum);
          return 0;
        }
        t *= val[i];
      }
    }
    printf("%I64d\n", sum - Min + Min % t);
  }
}
