#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define ena long long
ena n, ans;
ena c[maxn], cost[maxn], par[maxn], rankk[maxn];
vector <tuple<ena,ena,ena>> a;
ena root(ena u)
{
return (u == par[u]) ? u : par[u] = root(par[u]);
}
int main()
{
freopen("gold.inp","r",stdin);
freopen("gold.out","w",stdout);
a.push_back(make_tuple(0,0,0));
cin >> n;
for(ena i=1; i<=n; i++)
{
cin >> c[i];
cost[i] = c[i];
par[i] = i;
}
for(ena i=1; i<=n-1; i++)
{
ena u, v;
cin >> u >> v;
a.push_back(make_tuple(max(c[u], c[v]), u, v));
}
sort(a.begin(), a.end());
for(ena i=1; i<=n-1; i++)
{
ena u = get<1>(a[i]);
ena v = get<2>(a[i]);
ena ru = root(u);
ena rv = root(v);
if(ru != rv)
{
if(rankk[ru] < rankk[rv]) swap(ru, rv);
if(rankk[ru] == rankk[rv]) rankk[ru]++;
ans += cost[ru] + cost[rv];
cost[ru] = max(cost[ru], cost[rv]);
par[rv] = ru;
}
}
cout << ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIG1heG4gMTAwMDEwCiNkZWZpbmUgZW5hIGxvbmcgbG9uZwoKZW5hIG4sIGFuczsKZW5hIGNbbWF4bl0sIGNvc3RbbWF4bl0sIHBhclttYXhuXSwgcmFua2tbbWF4bl07Cgp2ZWN0b3IgPHR1cGxlPGVuYSxlbmEsZW5hPj4gYTsKCmVuYSByb290KGVuYSB1KQp7CiAgICByZXR1cm4gKHUgPT0gcGFyW3VdKSA/IHUgOiBwYXJbdV0gPSByb290KHBhclt1XSk7Cn0KCmludCBtYWluKCkKewogICAgZnJlb3BlbigiZ29sZC5pbnAiLCJyIixzdGRpbik7CiAgICBmcmVvcGVuKCJnb2xkLm91dCIsInciLHN0ZG91dCk7CiAgICBhLnB1c2hfYmFjayhtYWtlX3R1cGxlKDAsMCwwKSk7CgogICAgY2luID4+IG47CiAgICBmb3IoZW5hIGk9MTsgaTw9bjsgaSsrKQogICAgewogICAgICAgIGNpbiA+PiBjW2ldOwogICAgICAgIGNvc3RbaV0gPSBjW2ldOwogICAgICAgIHBhcltpXSA9IGk7CiAgICB9CgogICAgZm9yKGVuYSBpPTE7IGk8PW4tMTsgaSsrKQogICAgewogICAgICAgIGVuYSB1LCB2OwogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgYS5wdXNoX2JhY2sobWFrZV90dXBsZShtYXgoY1t1XSwgY1t2XSksIHUsIHYpKTsKICAgIH0KCiAgICBzb3J0KGEuYmVnaW4oKSwgYS5lbmQoKSk7CgogICAgZm9yKGVuYSBpPTE7IGk8PW4tMTsgaSsrKQogICAgewogICAgICAgIGVuYSB1ID0gZ2V0PDE+KGFbaV0pOwogICAgICAgIGVuYSB2ID0gZ2V0PDI+KGFbaV0pOwogICAgICAgIGVuYSBydSA9IHJvb3QodSk7CiAgICAgICAgZW5hIHJ2ID0gcm9vdCh2KTsKCiAgICAgICAgaWYocnUgIT0gcnYpCiAgICAgICAgewogICAgICAgICAgICBpZihyYW5ra1tydV0gPCByYW5ra1tydl0pIHN3YXAocnUsIHJ2KTsKICAgICAgICAgICAgaWYocmFua2tbcnVdID09IHJhbmtrW3J2XSkgcmFua2tbcnVdKys7CgogICAgICAgICAgICBhbnMgKz0gY29zdFtydV0gKyBjb3N0W3J2XTsKICAgICAgICAgICAgY29zdFtydV0gPSBtYXgoY29zdFtydV0sIGNvc3RbcnZdKTsKICAgICAgICAgICAgcGFyW3J2XSA9IHJ1OwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgYW5zOwogICAgcmV0dXJuIDA7Cn0K