#include <bits/stdc++.h>
using namespace std;
const int NS = 100010;
struct Edge {
  int to, next;
} edge[NS << 1];
int n, top, flag;
long long ans;
long long dp[NS];
int head[NS];
void add_edge(int u, int v) {
  edge[top].to = v;
  edge[top].next = head[u];
  head[u] = top++;
  edge[top].to = u;
  edge[top].next = head[v];
  head[v] = top++;
}
long long gcd(long long ta, long long tb) {
  return tb == 0 ? ta : gcd(tb, ta % tb);
}
long long lcm(long long ta, long long tb) { return ta / gcd(ta, tb) * tb; }
long long dfs(int rt, int fa) {
  if (flag) return 1;
  long long g = 1;
  long long cson = 0, sum = (1LL << 60);
  for (int i = head[rt]; i != -1; i = edge[i].next) {
    int to = edge[i].to;
    if (to != fa) {
      long long t = dfs(to, rt);
      if (flag) return 1;
      dp[rt] += dp[to];
      sum = min(sum, dp[to]);
      cson++;
      g = lcm(g, t);
    }
  }
  if (cson == 0) return 1;
  sum *= cson;
  g *= cson;
  sum -= sum % g;
  long long t = dp[rt] - sum;
  ans += t, dp[rt] -= t;
  if (dp[rt] <= 0) flag = 1;
  return g;
}
int main() {
  while (~scanf("%d", &n)) {
    long long res = 0;
    ans = top = flag = 0;
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= n; i++) scanf("%I64d", &dp[i]), res += dp[i];
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d %d", &u, &v);
      add_edge(u, v);
    }
    dfs(1, -1);
    if (flag) {
      ans = res;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
