#include <bits/stdc++.h>
using namespace std;
const int maxN = 100000 + 10;
const int maxM = 200000 + 10;
struct edge {
  edge *pre;
  int other;
  edge() { pre = NULL; }
} * last[maxN], edgeData[maxM], *totEdge;
int a[maxN];
int vis[maxN], leaf[maxN];
long long f[maxN];
int cnt[maxN];
int N;
long long sum, ans;
int flag;
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }
void addEdge(int u, int v) {
  totEdge->pre = last[u];
  totEdge->other = v;
  last[u] = totEdge++;
}
void DFS(int x) {
  vis[x] = 1;
  f[x] = 1;
  for (edge *p = last[x]; p != NULL; p = p->pre)
    if (!vis[p->other]) {
      ++cnt[x];
      DFS(p->other);
      if (flag) return;
      f[x] = (f[x] * f[p->other]) / GCD(f[x], f[p->other]);
    }
  if (cnt[x])
    f[x] *= cnt[x];
  else
    leaf[x] = 1;
  if (f[x] > sum) flag = 1;
}
int check(int x, long long s) {
  if (leaf[x]) return (a[x] >= s);
  vis[x] = 1;
  for (edge *p = last[x]; p != NULL; p = p->pre)
    if (!vis[p->other])
      if (!check(p->other, s / cnt[x])) return 0;
  return 1;
}
int main() {
  totEdge = edgeData;
  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);
    addEdge(u, v);
    addEdge(v, u);
  }
  DFS(1);
  if (flag) {
    printf("%I64d\n", sum);
    return 0;
  }
  long long L = 0, R = sum / f[1];
  while (L <= R) {
    long long mid = (L + R) / 2;
    memset(vis, 0, sizeof vis);
    if (check(1, f[1] * mid)) {
      ans = mid;
      L = mid + 1;
    } else
      R = mid - 1;
  }
  printf("%I64d\n", sum - f[1] * ans);
  return 0;
}
