#include <bits/stdc++.h>
using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
i64 apples[100005];
i64 capa[100005];
i64 unit[100005];
i64 total[100005];
vector<int> edge[100005];
i64 parent[100005];
i64 gcd(i64 a, i64 b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
void dfs(int now) {
  int childs = 0;
  unit[now] = 1;
  i64 childMin = 1ll << 61;
  for (auto& e : edge[now]) {
    if (e == parent[now]) continue;
    parent[e] = now;
    childs++;
    dfs(e);
    total[now] += total[e];
    unit[now] = lcm(unit[now], unit[e]);
    childMin = min(childMin, capa[e]);
  }
  if (childs == 0) {
    capa[now] = apples[now];
    total[now] = apples[now];
  } else {
    unit[now] *= childs;
    unit[now] = min(unit[now], total[now]);
    if (unit[now] == 0) unit[now] = 1;
    capa[now] = childs * childMin;
    capa[now] = capa[now] / unit[now] * unit[now];
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &apples[i]);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(1);
  printf("%lld\n", total[1] - capa[1]);
  return 0;
}
