#include <bits/stdc++.h>
using namespace std;
const unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long ksm(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x % mod;
    y >>= 1ll;
    x = x * x % mod;
  }
  return ret;
}
long long qpow(long long x, long long y) {
  long long ret = 1;
  while (y) {
    if (y & 1ll) ret = ret * x;
    y >>= 1ll;
    x = x * x;
  }
  return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
long long l = infi, n, m = 1, s, v[100010], a, b;
vector<long long> G[100010];
void f(long long n, long long p, long long d) {
  m = m * d / _gcd(m, d);
  if (d > s || d < 0) cout << s << '\n', exit(0);
  if (G[n].size() == 1 && n > 1) l = min(l, d * v[n]);
  for (int e : G[n])
    if (e != p) f(e, n, d * (G[n].size() - (n != 1)));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) v[i] = read(), s += v[i];
  for (register int i = 1; i <= n - 1; ++i)
    a = read(), b = read(), G[a].push_back(b), G[b].push_back(a);
  f(1, 0, 1);
  cout << s - l / m * m << '\n';
  return 0;
}
