#include <bits/stdc++.h>
using namespace std;
long long val[100100], visited[100100], degree[100100], least[100100], n,
    EXCEEDED = false;
vector<long long> A[100100];
void _visit() {
  for (long long i = 0; i < 100100; i++) visited[i] = false;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long dfs(long long u) {
  long long i, k, v, g = 1, minn = 1 << 30, p = 0, temp;
  visited[u] = true;
  if (degree[u] == 1) return 1;
  for (i = 0; i < A[u].size(); i++) {
    v = A[u][i];
    if (!visited[v] && !EXCEEDED) {
      temp = dfs(v);
      p++;
      g = (g * temp) / gcd(g, temp);
    }
  }
  if (p * g > 100000000) EXCEEDED = true;
  return p * g;
}
void second_dfs(long long u, long long val) {
  long long i, v;
  visited[u] = true;
  least[u] = val;
  for (i = 0; i < A[u].size(); i++) {
    v = A[u][i];
    if (!visited[v]) {
      second_dfs(v, val / (degree[u] - 1));
    }
  }
}
int main() {
  long long i, j, k, sum = 0, minn = 1 << 30;
  scanf("%I64d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%I64d", &val[i]);
    visited[i] = false;
    sum += val[i];
    degree[i] = 0;
  }
  for (i = 0; i < n - 1; i++) {
    scanf("%I64d%I64d", &j, &k);
    A[k].push_back(j);
    A[j].push_back(k);
    degree[j]++;
    degree[k]++;
  }
  degree[1]++;
  k = dfs(1);
  if (EXCEEDED) {
    printf("%I64d\n", sum);
    return 0;
  }
  _visit();
  second_dfs(1, k);
  for (i = 1; i <= n; i++) {
    if (degree[i] == 1)
      minn = (((minn) < (val[i] / least[i])) ? (minn) : (val[i] / least[i]));
  }
  printf("%I64d\n", sum - k * minn);
  return 0;
}
