#include <bits/stdc++.h>
using namespace std;
vector<int> e[200000];
bool f = 0;
long long p[200000], sum;
int a[200000];
inline void ins(int u, int v) {
  e[u].push_back(v);
  e[v].push_back(u);
}
void dfs(int u, int fa, long long d) {
  if (f) return;
  p[u] = d;
  int tot = e[u].size();
  if (fa) tot--;
  for (int i = 0; i < e[u].size(); i++) {
    int v = e[u][i];
    if (v == fa) continue;
    if (sum / tot < d) {
      f = 1;
      return;
    }
    dfs(v, u, d * tot);
  }
}
long long gcd(long long a, long long b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    sum += a[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ins(u, v);
  }
  dfs(1, 0, 1);
  long long ans = sum;
  for (int i = 2; i <= n; i++)
    if (e[i].size() == 1) ans = min(ans, a[i] * p[i]);
  if (f) {
    printf("%I64d\n", sum);
    return 0;
  }
  long long t = p[1];
  for (int i = 2; i <= n; i++)
    if (e[i].size() == 1) {
      t = lcm(t, p[i]);
      if (t > sum) {
        printf("%I64d\n", sum);
        return 0;
      }
    }
  printf("%I64d\n", sum - ans / t * t);
  return 0;
}
