#include <bits/stdc++.h>
using namespace std;
void debug() {}
void sol();
int main() {
  debug();
  sol();
  return 0;
}
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
vector<int> v[100005];
long long w[100005];
int si[100005];
int ma[100005];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
bool dfs(int x, int f) {
  si[x] = v[x].size() - 1;
  if (x == 1) si[x]++;
  if (si[x] == 0) {
    si[x] = 1;
    return 1;
  }
  long long g = 1, mi = 1LL << 60;
  for (int i = v[x].size() - 1; i >= 0; i--) {
    int to = v[x][i];
    if (to == f) continue;
    if (!dfs(to, x)) return 0;
    g = g / gcd(g, si[to]) * si[to];
    if (g > w[to]) return 0;
  }
  for (int i = v[x].size() - 1; i >= 0; i--) {
    int to = v[x][i];
    if (to == f) continue;
    if (g > w[to]) return 0;
    mi = min(mi, w[to] = w[to] / g * g);
  }
  w[x] = mi * si[x];
  si[x] *= g;
  return 1;
}
void sol() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &w[i + 1]);
    ans += w[i + 1];
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
    v[b].push_back(a);
  }
  if (dfs(1, 0))
    cout << ans - w[1] << endl;
  else
    cout << ans << endl;
}
