#include <bits/stdc++.h>
using namespace std;
const int INPUT_MAXCHARS = 1 << 16;
char buffer[INPUT_MAXCHARS];
struct FastReader {
  char *p;
  FastReader() : p(buffer) { fread(buffer, 1, sizeof buffer, stdin); }
  int next_int() {
    int remchars = INPUT_MAXCHARS - (p - buffer);
    if (remchars < 25) {
      memcpy(buffer, p, remchars);
      size_t cnt = fread(buffer + remchars, 1, sizeof buffer - remchars, stdin);
      if (remchars + cnt < sizeof buffer) {
        buffer[remchars + cnt] = 0;
      }
      p = buffer;
    }
    while (*p < '0') {
      p++;
    }
    int val = 0;
    while (*p >= '0') {
      val = val * 10 + (*p - '0');
      ++p;
    }
    return val;
  }
};
const int MAXN = 100000;
const long long INF = 12345678901234567ll;
long long mulof[MAXN];
long long W[MAXN];
int cchild[MAXN];
vector<vector<int> > G;
long long gcd(long long m, long long n) { return m == 0 ? n : gcd(n % m, m); }
long long lcm(long long m, long long n) { return m / gcd(m, n) * n; }
int prec(int u, int p) {
  int ret = 0;
  cchild[u] = 0;
  for (int i = 0; i < (int)G[u].size(); ++i) {
    int v = G[u][i];
    if (v == p) continue;
    ret = min(ret, prec(v, u));
    W[u] += W[v];
    ++cchild[u];
  }
  mulof[u] = 1;
  if (cchild[u] > 0) {
    for (int i = 0; i < (int)G[u].size(); ++i) {
      int v = G[u][i];
      if (v == p) continue;
      mulof[u] = lcm(mulof[u], mulof[v]);
      if (mulof[u] >= W[u] / cchild[u] + 1) {
        ret = -1;
        mulof[u] = 1;
        break;
      }
    }
    mulof[u] *= cchild[u];
  }
  return ret;
}
long long trim(int u, int p) {
  if (cchild[u] == 0) {
    return 0;
  }
  long long ret = 0;
  long long minhave = INF;
  for (int i = 0; i < (int)G[u].size(); ++i) {
    int v = G[u][i];
    if (v == p) continue;
    ret += trim(v, u);
    minhave = min(minhave, W[v]);
  }
  mulof[u] /= cchild[u];
  long long per = minhave / mulof[u] * mulof[u];
  W[u] = cchild[u] * per;
  for (int i = 0; i < (int)G[u].size(); ++i) {
    int v = G[u][i];
    if (v == p) continue;
    ret += W[v] - per;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  FastReader reader;
  int n = reader.next_int();
  for (int i = 0; i < n; ++i) {
    W[i] = reader.next_int();
  }
  G.assign(n, vector<int>());
  for (int i = 1; i < n; ++i) {
    int a = reader.next_int() - 1;
    int b = reader.next_int() - 1;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  long long sol = 0;
  if (prec(0, -1) == -1) {
    sol = W[0];
  } else {
    sol = trim(0, -1);
  }
  printf("%I64d\n", sol);
  return 0;
}
