#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (!x || !y) return x + y;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return (x / gcd(x, y)) * y; }
int N, M, X, Y;
long long apples[100005], rem;
long long lc[100005], childm[100005], sub[100005], dp[100005], deg[100005];
vector<int> AdjList[100005];
void die() {
  cout << rem << '\n';
  exit(0);
}
void dfs(int x, int p) {
  lc[x] = 1;
  sub[x] = apples[x];
  for (auto it : AdjList[x]) {
    if (it == p) continue;
    deg[x]++;
    dfs(it, x);
    lc[x] = lcm(lc[x], lc[it]);
    sub[x] += sub[it];
  }
  if (deg[x] == 0) return;
  childm[x] = lc[x];
  if (lc[x] > (rem + deg[x] - 1) / deg[x]) die();
  lc[x] = lc[x] * deg[x];
}
void dfs2(int x, int p) {
  long long mini = 1e18;
  for (auto it : AdjList[x]) {
    if (it == p) continue;
    dfs2(it, x);
    mini = min(mini, dp[it]);
  }
  if (mini == 1e18) {
    dp[x] = apples[x];
  } else
    dp[x] = (mini / childm[x]) * childm[x] * deg[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> apples[i];
    rem += apples[i];
  }
  for (int i = 1; i < N; ++i) {
    cin >> X >> Y;
    AdjList[X].push_back(Y);
    AdjList[Y].push_back(X);
  }
  dfs(1, 0);
  dfs2(1, 0);
  cout << rem - dp[1] << '\n';
}
