#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
long long s[100010], f[100010];
int a[100010];
long long sum = 0;
long long ans = 0;
long long gcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
void dfs(int x, int p) {
  s[x] = g[x].size() == 1 ? 1 : (x == 1 ? g[x].size() : g[x].size() - 1);
  long long lcm = 1;
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] != p) {
      int y = g[x][i];
      dfs(y, x);
      lcm = lcm / gcd(lcm, f[y]);
      if (lcm <= sum / f[y] + 1)
        lcm = lcm * f[y];
      else
        lcm = sum + 2;
      lcm = lcm / gcd(lcm, f[y]) * f[y];
    }
  if (lcm <= sum / s[x] + 1)
    lcm = lcm * s[x];
  else
    lcm = sum + 2;
  f[x] = lcm;
}
void dfs2(int x, int p, long long k) {
  if (g[x].size() == 1 && x != 1 && sum - a[x] / k * f[1] > ans)
    ans = sum - a[x] / k * f[1];
  for (int i = 0; i < g[x].size(); i++)
    if (g[x][i] != p) dfs2(g[x][i], x, k / s[x]);
}
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 x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  if (f[1] <= sum) {
    ans = 0;
    dfs2(1, 0, f[1]);
  } else
    ans = sum;
  printf("%lld\n", ans);
  return 0;
}
