#include <bits/stdc++.h>
using namespace std;
int n, apple[200000], a, b;
long long tot = 0;
vector<int> adj[200000];
int GCD(long long x, long long y) {
  if (y == 0) return x;
  return GCD(y, x % y);
}
pair<long long, long long> DFS(int ant, int act) {
  if (adj[act].size() == 1) return make_pair(apple[act], 1);
  vector<pair<long long, long long> > V;
  for (int i = 0; i < adj[act].size(); i++) {
    if (adj[act][i] == ant) continue;
    V.push_back(DFS(act, adj[act][i]));
  }
  long long mn = V[0].first;
  for (int i = 0; i < V.size(); i++) mn = min(mn, V[i].first);
  long long mcm = V[0].second;
  for (int i = 1; i < V.size(); i++) {
    if (mcm > mn) break;
    mcm = (mcm * V[i].second) / GCD(mcm, V[i].second);
  }
  return make_pair(V.size() * mcm * (mn / mcm), V.size() * mcm);
}
int main() {
  scanf("%d", &(n));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &(apple[i]));
    tot += apple[i];
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &(a), &(b));
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  adj[1].push_back(0);
  cout << tot - DFS(0, 1).first << endl;
}
