#include <bits/stdc++.h>
using namespace std;
int n;
int app[100005];
struct node {
  int to, next;
} edge[100005 << 1];
int head[100005], esum;
long long num[100005];
long long f[100005];
long long p;
bool vis[100005];
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline void add(int u, int v) {
  edge[esum].to = v, edge[esum].next = head[u], head[u] = esum++;
}
void init() {
  memset(head, -1, sizeof(head));
  esum = 0;
  p = 1;
}
void bfs() {
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    f[top] = 0;
    for (int i = head[top]; ~i; i = edge[i].next) {
      int v = edge[i].to;
      if (vis[v]) continue;
      f[top]++;
    }
    if (!f[top]) continue;
    for (int i = head[top]; ~i; i = edge[i].next) {
      int v = edge[i].to;
      if (vis[v]) continue;
      num[v] = num[top] * f[top];
      if (num[v] > 100000000000001LL) num[v] = 100000000000001LL;
      q.push(v);
      vis[v] = 1;
    }
  }
}
bool ok(long long s) {
  for (int i = 1; i <= n; i++)
    if (!f[i])
      if (app[i] * num[i] < s) return 0;
  return 1;
}
int main() {
  while (~scanf("%d", &n)) {
    init();
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &app[i]);
      sum += app[i];
    }
    int u, v;
    for (int i = 0; i < n - 1; i++) {
      scanf("%d%d", &u, &v);
      add(u, v);
      add(v, u);
    }
    num[1] = 1;
    bfs();
    for (int i = 1; i <= n; i++) {
      p = lcm(p, num[i]);
      if (p >= 100000000000001LL) {
        p = 100000000000001LL;
        break;
      }
    }
    if (p >= 100000000000001LL) {
      printf("%I64d\n", sum);
      continue;
    }
    long long l = 0, r = sum / p;
    long long ans = 0;
    while (r >= l) {
      long long mid = (l + r) / 2;
      if (ok(mid * p)) {
        ans = mid;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    printf("%I64d\n", sum - p * ans);
  }
  return 0;
}
