#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
            for(;;);
//            cout<<ss<<"\n";
//            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];

LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
LLI lcm_calc(LLI a,LLI b)
{

    return (a*b)/gcd(a,b);
}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{
    LLI n,ss=0;
    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
LLI lcm_calc(LLI a,LLI b)
{

    LLI g=gcd(a,b);
    a=a/g;
    if(a>hh/b)

    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return a*b;

}
LLI mult(LLI a,LLI b)
{
      if(a>hh/b)
      {
            cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
      }
      return a*b;

}
LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=mult(ans,kk);

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
int ar[mx];
vector<int>G[mx];
LLI sum[mx];
LLI dfs(int u,int par)
{
    if(ar[u])return sum[u]=ar[u];
    LLI ans=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(v==par)continue;
        ans+=dfs(v,u);
    }

    return sum[u]=ans;
}
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
LLI lcm_calc(LLI a,LLI b)
{

    return (a*b)/gcd(a,b);
}

LLI dfs_lcm(int u,int par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    int kk=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    //cout<<"FF "<<u<<" "<<ans<<"\n";
    if(kk==0)
        kk=1;
    return LCM[u]=ans*kk;

}
LLI rec(int u,int par)
{

    LLI mini=1ll<<50;
    int kk=0;
    for(int i=0; i<G[u].size(); i++)
    {

        int v=G[u][i];
        if(par==v)continue;
        kk++;
        mini=min(mini,sum[v]);
    }
    LLI t=LCM[u]/kk;
    LLI x=t*(mini/t);
    LLI ans=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(par==v)continue;

      ans+=sum[v]-x;

    }

return ans;
}

int main()
{
    int n;
    cin>>n;
    FOR1(i,n)
    cin>>ar[i];
    FOR1(i,n-1)
    {
        int u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
    dfs(1,1);
     dfs_lcm(1,1);
//      FOR1(i,n)
//    cout<<i<<" : "<<LCM[i]<<"\n";

    cout<<rec(1,1)<<"\n";
//

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
          //  for(;;);
//            cout<<ss<<"\n";
//            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
LLI lcm_calc(LLI a,LLI b)
{

    LLI g=gcd(a,b);
    a=a/g;
    if(a>hh/b)

    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return a*b;

}
LLI mult(LLI a,LLI b)
{
      if(a>hh/b)
      {
            cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
      }

}
LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=mult(ans,kk);

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
          //  for(;;);
//            cout<<ss<<"\n";
            exit(0);
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
    if(ll>hh)
    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
   // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
int ar[mx];
vector<int>G[mx];
LLI sum[mx];
LLI dfs(int u,int par)
{
    if(ar[u])return sum[u]=ar[u];
    LLI ans=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(v==par)continue;
        ans+=dfs(v,u);
    }

    return sum[u]=ans;
}
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
LLI lcm_calc(LLI a,LLI b)
{

    return (a*b)/gcd(a,b);
}

LLI dfs_lcm(int u,int par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    int kk=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    //cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
LLI rec(int u,int par)
{

    LLI mini=1ll<<50;
    int kk=0;
    for(int i=0; i<G[u].size(); i++)
    {

        int v=G[u][i];
        if(par==v)continue;
        kk++;
        mini=min(mini,sum[v]);
    }
    LLI t=LCM[u]/kk;
    LLI x=t*(mini/t);
    LLI ans=0;
    for(int i=0; i<G[u].size(); i++)
    {
        int v=G[u][i];
        if(par==v)continue;

      ans+=sum[v]-x;

    }

return ans;
}

int main()
{
    int n;
    cin>>n;
    FOR1(i,n)
    cin>>ar[i];
    FOR1(i,n-1)
    {
        int u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
    dfs(1,1);
    dfs_lcm(1,1);
    cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<LCM[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
          //  for(;;);
//            cout<<ss<<"\n";
//            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    exit('0');
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
          //  for(;;);
//            cout<<ss<<"\n";
            exit(0);
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1ll<<63;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];

LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
LLI lcm_calc(LLI a,LLI b)
{

    return (a*b)/gcd(a,b);
}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{
    LLI n,ss=0;
    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
LLI lcm_calc(LLI a,LLI b)
{

    LLI g=gcd(a,b);
    a=a/g;
    if(a>hh/b)

    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return a*b;

}
LLI mult(LLI a,LLI b)
{
    if(a>hh/b)
    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
    }
    return a*b;

}

void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        LCM[u]=1;
        return;
    }

    LLI lcm=1;
    for(int i=0; i<G[u].size(); i++)
    {

        int v=G[u][i];
        if(v==par)continue;
        lcm=lcm_calc(lcm,LCM[v]);

    }
        LLI val=1<<30;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];

           if(v==par)continue;
           mini=min(mini,(sum[v]/lcm)*lcm);

    }

   LCM[u]=mult(lcm,child);
   sum[u]=mult(mini,child);
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
//    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=1ll<<60;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
bool overflow=0;

long long Mul(long long a, long long b) {
    if(a == 0 || b == 0)
        return 0;
    if(a > INF / b) {
        overflow = 1;
        return INF;
    }
    return a * b;
}

long long Lcm(long long a, long long b) {
    long long gg = __gcd(a, b);
    return Mul(a / gg, b);
}

void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        LCM[u]=1;
        return;
    }

    LLI lcm=1;
    for(int i=0; i<G[u].size(); i++)
    {

        int v=G[u][i];
        if(v==par)continue;
        lcm=Lcm(lcm,LCM[v]);
        if(overflow)return;

    }
        LLI val=1<<30;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];

           if(v==par)continue;
           mini=min(mini,(sum[v]/lcm)*lcm);
        if(overflow)return;
    }

   LCM[u]=Mul(lcm,child);
   sum[u]=Mul(mini,child);
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
//    dfs_lcm(1,1);
    rec(1,1);
    if(overflow)
        cout<<ss<<"\n";
    else
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
LLI lcm_calc(LLI a,LLI b)
{

    LLI g=gcd(a,b);
    a=a/g;
    if(a>hh/b)

    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return a*b;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1LL<<63;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            for(;;);
//            cout<<ss<<"\n";
//            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI node;
LLI apple[mx];
vector<LLI>G[mx];
LLI sum=0;
LLI rec(LLI u,LLI par)
{
    if(apple[u]!=0)return apple[u];
    LLI mini=1ll<<50,ss=0;
    LLI kk=0;
    FOR0(i,G[u].size())
    {
        LLI v=G[u][i];
        if(par==v)
            continue;
            kk++;
        LLI temp=rec(v,u);
      //  cout<<"porn "<<v<<" "<<temp<<"\n";
        ss+=temp;
        mini=min(mini,temp);
    }

    sum+=ss-(mini*kk);
//cout<<"FF "<<ss<<" "<<mini<<"  "<<ss-(mini*kk)<<" "<<sum<<"\n";
return (mini*kk);
}
int main()
{
    cin>>node;
    FOR1(i,node)
    cin>>apple[i];
    FOR1(i,node-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
    rec(1,1);
    cout<<sum<<"\n";
}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=1ll<<60;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
bool overflow=0;

long long Mul(long long a, long long b) {
    if(a == 0 || b == 0)
        return 0;
    if(a > INF / b) {
        overflow = 1;
        return INF;
    }
    return a * b;
}

long long Lcm(long long a, long long b) {
    long long gg = __gcd(a, b);
    return Mul(a / gg, b);
}

void rec(LLI u,LLI par)
{
    LLI child=0,mini=1ll<<63;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        LCM[u]=1;
        return;
    }

    LLI lcm=1;
    for(int i=0; i<G[u].size(); i++)
    {

        int v=G[u][i];
        if(v==par)continue;
        lcm=Lcm(lcm,LCM[v]);
        if(overflow)return;

    }
        LLI val=1<<30;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];

           if(v==par)continue;
           mini=min(mini,(sum[v]/lcm)*lcm);
        if(overflow)return;
    }

   LCM[u]=Mul(lcm,child);
   sum[u]=Mul(mini,child);
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
//    dfs_lcm(1,1);
    rec(1,1);
    if(overflow)
        cout<<ss<<"\n";
    else
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI      unsigned long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000005
LLI lcm_calc(LLI a,LLI b)
{

    LLI g=gcd(a,b);
    a=a/g;
    if(a>hh/b)

    {
        cout<<ss<<"\n";
        //  for(;;);
//            cout<<ss<<"\n";
        exit(0);
//            puts("")
    }
    return a*b;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    // exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
          //  for(;;);
//            cout<<ss<<"\n";
//            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    exit(0);
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

#define lcm(a,b) (a*b)/(__gcd(a,b))

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll Minx = LONG_MAX;
ll l = 1, sum = 0;

void dfs( ll x, ll p, ll d ) {
	l = lcm(d,l);
	if( d > sum || d < 0 ) { cout << sum; exit(0); }
	if( x != 1 && v[x].size() == 1 ) Minx = min( Minx, d*arr[x] );
	for( ll q : v[x] )
	 if( q != p )
	  dfs( q, x, d*( v[x].size() - ( x == 1 ? 0 : 1 ) ) );
}

int main() {

	ll n; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i], sum += arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1,1);
	ll k = (Minx/l)*l;
	if( sum < k )
	 cout << sum;
	else
	 cout << sum - k;
}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

#define lcm(a,b) (a*b)/(__gcd(a,b))

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll Minx = LONG_MAX;
ll l = 1, sum = 0;

void dfs( ll x, ll p, ll d ) {
	l = lcm(d,l);
	if( d > sum || d < 0 ) { cout << sum; exit(0); }
	if( x != 1 && v[x].size() == 1 ) Minx = min( Minx, d*arr[x] );
	for( ll q : v[x] )
	 if( q != p )
	  dfs( q, x, d*( v[x].size() - ( x == 1 ? 0 : 1 ) ) );
}

int main() {

	ll n; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i], sum += arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1,1);
	cout  << sum - (Minx/l)*l;
}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

#define lcm(a,b) (a*b)/(__gcd(a,b))

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll Minx = LONG_MAX;
ll l = 1;

void dfs( ll x, ll p, ll d ) {
	l = lcm(d,l);
	if( x != 1 && v[x].size() == 1 ) Minx = min( Minx, d*arr[x] );
	for( ll q : v[x] )
	 if( q != p )
	  dfs( q, x, d*( v[x].size() - ( x == 1 ? 0 : 1 ) ) );
}

int main() {

	ll n, sum = 0; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i], sum += arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1,1);
	ll k = (Minx/l)*l;
	if( sum < k )
	 cout << sum;
	else
	 cout << sum - k;
}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll gcd( ll a, ll b ) { return ( b == 0 ? a : gcd( b, a%b ) ); }

ll ans = 0;

pair<ll,ll> dfs( ll x, ll p ) {

	if( arr[x] != 0 )
	 return mp(arr[x],1);

	ll lcm = 1, val = LONG_MAX, sum = 0;

	for( ll q : v[x] )
	 if( q != p ) {
		auto it = dfs( q, x );
		lcm *= it.S;
		lcm /= gcd(lcm,it.S);
		val = min( val, it.F );
		sum += it.F;
	 }

	ll final = (val/lcm)*lcm;
	ans += sum - final*(v[x].size()-1);
	if( x == 1 )
	 ans -= final;

	cout << "REC: " << x << " = " << final << " - " << sum << "\n";

	return mp(final*(v[x].size()-1),v[x].size()-1);

}

int main() {

	ll n; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1);
	cout << ans;
}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll gcd( ll a, ll b ) { return ( b == 0 ? a : gcd( b, a%b ) ); }

ll ans = 0;

pair<ll,ll> dfs( ll x, ll p ) {

	if( arr[x] != 0 )
	 return mp(arr[x],1);

	ll lcm = 1, val = LONG_MAX, sum = 0;

	for( ll q : v[x] )
	 if( q != p ) {
		auto it = dfs( q, x );
		lcm *= it.S;
		lcm /= gcd(lcm,it.S);
		val = min( val, it.F );
		sum += it.F;
	 }

	ll final = (val/lcm)*lcm;
	ans += sum - final*(v[x].size()-1);
	if( x == 1 )
	 ans -= final;

//	cout << "REC: " << x << " = " << final << " - " << sum << "\n";

	return mp(final*(v[x].size()-1),v[x].size()-1);

}

int main() {

	ll n; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1);
	cout << ans;
}


#include <bits/stdc++.h>
typedef long long ll;
#define get(a) scanf("%ld", &a)
#define repVector(v)  for( auto it = v.begin(); it != v.end(); it++ )
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define ROF(i,a,b) for( ll i = a; i >= b; i-- )
#define debug(x) cerr << "[DEBUG] " << #x << " = " << x << endl
#define matrix vector< vector<ll> >
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define BOOST ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

#define lcm(a,b) (a*b)/(__gcd(a,b))

ll arr[2*100005] = {0};
vector<ll> v[2*100005];

ll Minx = LLONG_MAX;
ll l = 1, sum = 0;

void dfs( ll x, ll p, ll d ) {
	l = lcm(d,l);
	if( d > sum || d < 0 ) { cout << sum; exit(0); }
	if( x != 1 && v[x].size() == 1 ) Minx = min( Minx, d*arr[x] );
	for( ll q : v[x] )
	 if( q != p )
	  dfs( q, x, d*( v[x].size() - ( x == 1 ? 0 : 1 ) ) );
}

int main() {

	ll n; cin >> n;
	FOR(i,1,n)
	 cin >> arr[i], sum += arr[i];
	FOR(i,1,n-1) {
		ll x, y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}

	dfs(1,-1,1);
	cout  << sum - (Minx/l)*l;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<long long> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1e+17, lc=1, t=0;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
            t++;
        }
    }
    if(u == 0 || v[u].size() !=  1){
        long long z=1;
        gcd[u] = max(z,lc * t);
        sum[u] = (min1/lc)*lc*t;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<int> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1, t=0;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
            t++;
        }
    }
    if(u == 0 || v[u].size() > 1){
        gcd[u] = lc * t;
        if(gcd[u]<1)gcd[u]++;
        sum[u] = min1/lc*lc*t;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<int> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
        }
    }
    if(u == 0 || v[u].size() > 1){
        int j =  v[u].size()-1;
        if(u==0)j = v[0].size();
        gcd[u] = lc * j;
        if(gcd[u]==0)gcd[u]++;
        sum[u] = min1/lc*lc*j;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+10;
const int MOD = 1e9+7;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<int> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
        }
    }
    if(u == 0 || v[u].size() > 1){
        int j =  v[u].size()-1;
        if(u==0)j = v[u].size();
        gcd[u] = lc * j;
        sum[u] = min1/lc*lc*j;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<int> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1, t=0;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
            t++;
        }
    }
    if(u == 0 || v[u].size() > 1){
        if(gcd[u]<1)gcd[u]++;
        sum[u] = min1/lc*lc*t;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<long long> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1, t=0;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
            t++;
        }
    }
    if(u == 0 || v[u].size() !=  1){
        long long z=1;
        gcd[u] = max(z,lc * t);
        sum[u] = min1/lc*lc*t;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100*1000+100;
bool mark[N];
long long gcd[N];
long long sum[N];
vector<long long> v[N];
void dfs(int u)
{
    mark[u] = true;
    long long min1 = 1000*1000*1000*1000*1000*100, lc=1, t=0;
    for(int i=0;i< v[u].size(); i++){
        if(mark[v[u][i]] == false){
       //   cout << sum[u] << " ";
            dfs(v[u][i]);
            min1 = min(min1, sum[v[u][i]]);
            lc = lc /__gcd(lc, gcd[v[u][i]])* gcd[v[u][i]];
            t++;
        }
    }
    if(u == 0 || v[u].size() > 1){
        gcd[u] = lc * t;
        if(gcd[u]<1)gcd[u]=1;
        sum[u] = min1/lc*lc*t;
    }
}
main()
{

    long long n, s=0;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> sum[i];
        gcd[i] = 1;
        s+=sum[i];
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        cin >> a >> b;
        v[a-1].push_back(b-1);
        v[b-1].push_back(a-1);
    }
    dfs(0);
    cout << s - sum[0];
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 10000000000000LL
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

LL a[200005];
vector<LL> v[200004];
bool dfs(LL cnode, LL napp, LL par = -1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return a[cnode] >= napp;
    if (napp % ccnt != 0)
        assert(false);
    bool ret = true;
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par)
            ret &= dfs(v[cnode][i], napp / ccnt, cnode);
    return ret;
}
LL g = 0;
int n;
bool dfsLCM(LL cnode, LL par = -1, LL cpro = 1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return true;
    cpro *= ccnt;
    if (cpro > INF)
        return false;
    if (g == 0)
        g = cpro;
    else {
        g /= __gcd(cpro, g);
        if (g > INF / cpro) {
            if (n == 100000) {
                cout << cnode << "   " << cpro << "  " << ccnt << end;
                cout << g << " " << g * cpro << endl;
            }
            return false;
        }
        g *= cpro;
    }
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par) {
            if (!dfsLCM(v[cnode][i], cnode, cpro))
                return false;
        }

    return true;
}
int main() {
    LL i, x, y;
    cin >> n;
    LL s = 0;
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        v[x].PB(y);
        v[y].PB(x);
    }
    LL lb = 0, ub = s, mid, ans = s;
    if (!dfsLCM(1)) ub = -1;
    else ub /= g;
    if (n == 100000)
        cout << g << "  " << ub << endl;
    while (lb <= ub) {
        mid = (lb + ub) / 2;
        if (dfs(1, mid * g)) {
            ans = mid * g;
            lb = mid + 1;
        } else {
            ub = mid - 1;
        }
    }
    cout << s - ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 10000000000000LL
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

LL a[200005];
vector<LL> v[200004];
bool dfs(LL cnode, LL napp, LL par = -1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return a[cnode] >= napp;
    if (napp % ccnt != 0)
        assert(false);
    bool ret = true;
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par)
            ret &= dfs(v[cnode][i], napp / ccnt, cnode);
    return ret;
}
LL g = 0;
bool dfsLCM(LL cnode, LL par = -1, LL cpro = 1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return true;
    if (cpro * ccnt > INF) {
        return false;
    }
    cpro *= ccnt;
    if (g == 0)
        g = cpro;
    else {
        g /= __gcd(cpro, g);
        if (g > INF / cpro)
            return false;
        g *= cpro;
    }
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par) {
            if (!dfsLCM(v[cnode][i], cnode, cpro))
                return false;
        }

    return true;
}
int main() {
    LL i, n, x, y;
    cin >> n;
    LL s = 0;
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        v[x].PB(y);
        v[y].PB(x);
    }
    LL lb = 0, ub = s, mid, ans = s;
    if (!dfsLCM(1)) ub = -1;
    else ub /= g;
    while (lb <= ub) {
        mid = (lb + ub) / 2;
        if (dfs(1, mid * g)) {
            ans = mid * g;
            lb = mid + 1;
        } else {
            ub = mid - 1;
        }
    }
    cout << s - ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 10000000000000LL
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

int a[200005];
vector<int> v[200004];
bool dfs(int cnode, LL napp, int par = -1) {
    int ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return a[cnode] >= napp;
    if (napp % ccnt != 0)
        return false;
    bool ret = true;
    for (int i = 0; i < (int)v[cnode].size(); i++)
        if (v[cnode][i] != par)
            ret &= dfs(v[cnode][i], napp / ccnt, cnode);
    return ret;
}
LL g = 0;
bool dfsLCM(int cnode, int par = -1, LL cpro = 1) {
    int ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return true;
    if (cpro * ccnt > INF) {
        return false;
    }
    if (g == 0)
        g = ccnt;
    else {
        g = (g / __gcd((LL)ccnt, g));
        if (g * ccnt > INF)
            return false;
        g *= ccnt;
    }
    for (int i = 0; i < (int)v[cnode].size(); i++)
        if (v[cnode][i] != par) {
            if (!dfsLCM(v[cnode][i], cnode, cpro * ccnt))
                return false;
        }

    return true;
}
int main() {
    int i, n, x, y;
    cin >> n;
    LL s  = 0;
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        v[x].PB(y);
        v[y].PB(x);
    }
    LL lb = 0, ub = s, mid, ans = s;
    if (!dfsLCM(1)) ub = -1;
    else ub /= g;
    while (lb <= ub) {
        mid = (lb + ub) / 2;
        if (dfs(1, mid * g)) {
            ans = mid * g;
            lb = mid + 1;
        } else {
            ub = mid - 1;
        }
    }
    cout << s - ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 10000000000000LL
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

LL a[200005];
vector<LL> v[200004];
bool dfs(LL cnode, LL napp, LL par = -1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return a[cnode] >= napp;
    if (napp % ccnt != 0)
        assert(false);
    bool ret = true;
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par)
            ret &= dfs(v[cnode][i], napp / ccnt, cnode);
    return ret;
}
LL g = 0;
bool dfsLCM(LL cnode, LL par = -1, LL cpro = 1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return true;
    cpro *= ccnt;
    if (cpro > INF)
        return false;
    if (g == 0)
        g = cpro;
    else {
        g /= __gcd(cpro, g);
        if (g > INF / cpro)
            return false;
        g *= cpro;
    }
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par) {
            if (!dfsLCM(v[cnode][i], cnode, cpro))
                return false;
        }

    return true;
}
int main() {
    LL i, n, x, y;
    cin >> n;
    LL s = 0;
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        v[x].PB(y);
        v[y].PB(x);
    }
    LL lb = 0, ub = s, mid, ans = s;
    if (!dfsLCM(1)) ub = -1;
    else ub /= g;
    if (n == 100000)
        cout << g << endl;
    while (lb <= ub) {
        mid = (lb + ub) / 2;
        if (dfs(1, mid * g)) {
            ans = mid * g;
            lb = mid + 1;
        } else {
            ub = mid - 1;
        }
    }
    cout << s - ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define LET(x, a)  __typeof(a) x(a)
#define TR(v, it) for(LET(it, v.begin()); it != v.end(); it++)
#define si(x) scanf("%d",&x)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define INF 10000000000000LL
#define MOD 1000000007
#define SET(x,y) memset(x,y,sizeof(x));
#define LL long long int
#define ULL unsigned LL
#define PII pair<int, int>

LL a[200005];
vector<LL> v[200004];
bool dfs(LL cnode, LL napp, LL par = -1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return a[cnode] >= napp;
    if (napp % ccnt != 0)
        assert(false);
    bool ret = true;
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par)
            ret &= dfs(v[cnode][i], napp / ccnt, cnode);
    return ret;
}
LL g = 0;
bool dfsLCM(LL cnode, LL par = -1, LL cpro = 1) {
    LL ccnt = v[cnode].size();
    if (par != -1)
        ccnt --;
    if (ccnt == 0)
        return true;
    cpro *= ccnt;
    if (cpro > INF)
        return false;
    if (g == 0)
        g = cpro;
    else {
        g /= __gcd(cpro, g);
        if (g > INF / cpro) {
            g *= cpro;
            return false;
        }
        g *= cpro;
    }
    for (LL i = 0; i < (LL)v[cnode].size(); i++)
        if (v[cnode][i] != par) {
            if (!dfsLCM(v[cnode][i], cnode, cpro))
                return false;
        }

    return true;
}
int main() {
    LL i, n, x, y;
    cin >> n;
    LL s = 0;
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        s += a[i];
    }
    for (i = 1; i < n; i++) {
        cin >> x >> y;
        v[x].PB(y);
        v[y].PB(x);
    }
    LL lb = 0, ub = s, mid, ans = s;
    if (!dfsLCM(1)) ub = -1;
    else ub /= g;
    if (n == 100000)
        cout << g << "  " << ub << endl;
    while (lb <= ub) {
        mid = (lb + ub) / 2;
        if (dfs(1, mid * g)) {
            ans = mid * g;
            lb = mid + 1;
        } else {
            ub = mid - 1;
        }
    }
    cout << s - ans << endl;
    return 0;
}