#include <bits/stdc++.h>
using namespace std;
int n;
long long a[100100];
long long res = 0;
long long ch[100100];
vector<int> p[100100];
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long dp[100100];
void dfs(int x, int pp = -1) {
  if (p[x].size() == 0 || p[x].size() == 1 && p[x][0] == pp) {
    ch[x] = 1, dp[x] = a[x];
    return;
  }
  long long l = 1, mn = 1e18;
  for (int i = 0; i < p[x].size(); i++) {
    if (p[x][i] == pp) continue;
    dfs(p[x][i], x);
    mn = min(mn, dp[p[x][i]]);
  }
  long long val;
  if (mn != 0) {
    for (int i = 0; i < p[x].size(); i++) {
      if (p[x][i] == pp) continue;
      l = lcm(l, ch[p[x][i]]);
    }
    val = mn / l;
    val *= l;
  } else {
    val = 0;
  }
  long long r1 = 0;
  for (int i = 0; i < p[x].size(); i++) {
    if (p[x][i] == pp) continue;
    res += (dp[p[x][i]] - val);
    r1++;
  }
  ch[x] = r1 * l;
  dp[x] = r1 * val;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  int x, y;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &x, &y);
    p[x].push_back(y);
    p[y].push_back(x);
  }
  dfs(1);
  cout << res << endl;
  return 0;
}
