#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > tree(100005);
int parent[100005];
long long weight[100004];
long long level[100004];
long long gcd(long long a, long long b) {
  if (a < 0 && b < 0)
    while (1)
      ;
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long balance(long long a1, long long l1, long long a2, long long l2) {
  long long x = a1 / l1;
  long long y = a2 / l2;
  long long g = gcd(l1, l2);
  long long k = (x / (l2 / g));
  k = min(k, (y / (l1 / g)));
  x = x - k * (l2 / g);
  y = y - k * (l1 / g);
  return a1 - l1 * x;
}
void dfs(int u) {
  int sz = tree[u].size();
  level[u] = -1;
  long long numChild = 0;
  for (int i = 0; i < sz; i++) {
    int v = tree[u][i];
    if (v == parent[u]) continue;
    parent[v] = u;
    dfs(v);
    numChild++;
    if (level[u] == -1) {
      level[u] = level[v];
      weight[u] = weight[v];
    }
    weight[u] = balance(weight[u], level[u], weight[v], level[v]);
    long long g = gcd(level[u], level[v]);
    level[u] = level[u] / g;
    if (level[u] >= 1e18 / level[v])
      level[u] = 1e18;
    else
      level[u] = level[u] * level[v];
  }
  if (level[u] == -1) {
    level[u] = 1;
    return;
  }
  weight[u] = weight[u] * numChild;
  if (level[u] >= 1e18 / numChild)
    level[u] = 1e18;
  else
    level[u] = level[u] * numChild;
}
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> weight[i];
    ans = ans + weight[i];
    parent[i] = -1;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs(0);
  ans = ans - weight[0];
  cout << ans << endl;
}
