#include <bits/stdc++.h>
using namespace std;
const long long INF = 99999999999999999;
long long n, value[200000], zarib[200000], LCM = 1, GCD, x = INF, ans, sum;
vector<int> Tree[200000];
long long gcd(long long l, long long r) {
  if (!l) return r;
  return gcd(r % l, l);
}
bool dfs(int v, int last, long long z) {
  zarib[v] = z;
  if (v && Tree[v].size() == 1 && zarib[v] > sum) return 0;
  z *= (Tree[v].size() - 1);
  if (!z) z = 1;
  for (int i = 0; i < Tree[v].size(); i++)
    if (Tree[v][i] != last)
      if (!dfs(Tree[v][i], v, z)) return 0;
  return 1;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> value[i];
    sum += value[i];
  }
  for (int i = 1; i < n; i++) {
    int l, r;
    cin >> l >> r;
    Tree[l - 1].push_back(r - 1);
    Tree[r - 1].push_back(l - 1);
  }
  if (!dfs(0, -1, 1)) {
    cout << sum << endl;
    return 0;
  }
  for (int i = 1; i < n; i++)
    if (Tree[i].size() == 1) {
      GCD = gcd(LCM, zarib[i]);
      LCM /= GCD;
      LCM *= zarib[i];
    }
  for (int i = 1; i < n; i++)
    if (Tree[i].size() == 1) {
      zarib[i] = LCM / zarib[i];
      x = min(x, value[i] / zarib[i]);
    }
  for (int i = 1; i < n; i++)
    if (Tree[i].size() == 1) ans += value[i] - zarib[i] * x;
  cout << ans << endl;
}
