#include <bits/stdc++.h>
using namespace std;
int n;
const int MAXN = 100000 + 10;
long long a[MAXN], vis[MAXN], mult[MAXN], ap[MAXN], res;
long long sum;
vector<int> G[MAXN];
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
void dfs(int x, int parent) {
  vis[x] = 1;
  mult[x] = 1;
  if (G[x].size() == 1 && x != 1) {
    ap[x] = a[x];
    return;
  }
  long long local_res = 0;
  long long m = 1000000000LL * 1000000000LL;
  for (int i = 0; i < G[x].size(); i++) {
    if (G[x][i] != parent) {
      dfs(G[x][i], x);
      mult[x] = lcm(mult[x], mult[G[x][i]]);
      if (mult[x] < 0) {
        printf("%I64d\n", sum);
        exit(0);
      }
    }
  }
  for (int i = 0; i < G[x].size(); i++)
    if (G[x][i] != parent) m = min(ap[G[x][i]] / mult[x], m);
  for (int i = 0; i < G[x].size(); i++)
    if (G[x][i] != parent) {
      local_res += ap[G[x][i]] - m * mult[x];
      ap[x] += m * mult[x];
    }
  mult[x] *= G[x].size() - 1;
  if (mult[x] < 0 || ap[x] < 0 || local_res < 0) {
    printf("%I64d\n", sum);
    exit(0);
  }
  res += local_res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    sum += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs(1, -1);
  printf("%I64d\n", res);
  return 0;
}
