#include <bits/stdc++.h>
using namespace std;
vector<unsigned long long> adj[100005];
unsigned long long a[100005];
unsigned long long lc = 1;
long double large = 100000000;
long double mn = (long long int)1e18;
unsigned long long ans = 0;
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
unsigned long long lcm(unsigned long long a, unsigned long long b) {
  if (a == 0 && b == 0) return 1;
  if (a == 0)
    return b;
  else if (b == 0)
    return a;
  if (a / gcd(a, b) >= (unsigned long long)10000000000000 / b)
    return 10000000000000;
  else
    return (a * b / gcd(a, b));
}
void dfs(unsigned long long s, unsigned long long e, unsigned long long val) {
  if (adj[s].size() == 1 && s != 1) {
    lc = lcm(lc, val);
    mn = min(mn, (long double)val * ((long double)a[s] / large));
  }
  unsigned long long ch = 0;
  for (auto x : adj[s]) {
    if (x != e) ch++;
  }
  for (auto x : adj[s]) {
    if (x != e) {
      dfs(x, s, val * ch);
    }
  }
}
void dfs2(unsigned long long s, unsigned long long e, unsigned long long val) {
  if (adj[s].size() == 1 && s != 1) {
    ans = ans + a[s] - val;
  }
  unsigned long long ch = 0;
  for (auto x : adj[s]) {
    if (x != e) ch++;
  }
  for (auto x : adj[s]) {
    if (x != e) {
      dfs2(x, s, val / ch);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned long long n;
  cin >> n;
  for (unsigned long long i = 1; i < n + 1; i++) cin >> a[i];
  for (unsigned long long i = 0; i < n - 1; i++) {
    unsigned long long a, b;
    cin >> a >> b;
    adj[a].push_back(b), adj[b].push_back(a);
  }
  dfs(1, 0, 1);
  mn = mn * large;
  mn = floor(mn);
  unsigned long long xx = mn / lc;
  unsigned long long rootval = xx * lc;
  dfs2(1, 0, rootval);
  cout << ans << '\n';
}
