#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], childRem[100005], chidKam[100005], childMinus,
    totalApple;
vector<int> g[100005];
long long gcd(long long a, long long b) {
  if (a < b) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcd(long long a, long long b) { return b / gcd(a, b) * a; }
long long dfs(int node, int par) {
  if (childRem[node]) {
    chidKam[node] = 1;
    return childRem[node];
  }
  long long lc = 1, minAppChild = 1000000000000015;
  for (int i = 0; i < g[node].size(); i++) {
    if (g[node][i] != par) {
      if (dfs(g[node][i], node) == 0) return 0;
      lc = lcd(lc, chidKam[g[node][i]]);
      if (childRem[g[node][i]] < minAppChild)
        minAppChild = childRem[g[node][i]];
    }
  }
  long long remindAppNum = (minAppChild / lc) * lc;
  if (remindAppNum == 0) return 0;
  for (int i = 0; i < g[node].size(); i++) {
    if (g[node][i] != par) {
      if (childRem[g[node][i]] >= remindAppNum) {
        childMinus += childRem[g[node][i]] - remindAppNum;
        childRem[g[node][i]] = remindAppNum;
        childRem[node] += remindAppNum;
      } else
        return 0;
    }
  }
  chidKam[node] = lc * (g[node].size() - 1);
  return childRem[node];
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> childRem[i];
    totalApple += childRem[i];
  }
  int tmp1, tmp2;
  for (int i = 0; i < n - 1; i++) {
    cin >> tmp1 >> tmp2;
    g[tmp1 - 1].push_back(tmp2 - 1);
    g[tmp2 - 1].push_back(tmp1 - 1);
  }
  if (dfs(0, -1) == 0)
    cout << totalApple << endl;
  else
    cout << childMinus << endl;
  return 0;
}
