#include <bits/stdc++.h>   const long long inf = std::numeric_limits<long long>::max();   const int infint = std::numeric_limits<int>::max();   const long long mod = 1e9 + 7;   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   vector<int> g[500100];   long long a[500100];   long long par[500100][21];   void dfs(int v, int p) {     for (auto i : g[v]) {       if (i == p) continue;       dfs(i, v);       par[i][0] = v;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     long long mn = inf;     for (int i = 0; i < n; i++) {       cin >> a[i];       mn = min(a[i], mn);     }     for (int i = 0; i < n - 1; i++) {       int x, y;       cin >> x >> y;       x--, y--;       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 0; i < n; i++) {       if (a[i] == mn) {         par[i][0] = i;         dfs(i, -1);         break;       }     }     long long w = 0;     for (int i = 1; i < 21; i++)       for (int j = 0; j < n; j++) par[j][i] = par[par[j][i - 1]][i - 1];     for (int i = 0; i < n; i++) {       if (a[i] == mn) continue;       long long now = inf;       for (int j = 0; j < 21; j++) {         now = min(now, a[par[i][j]] * (j + 1) + a[i]);       }       w += now;     }     cout << w;     return 0;   }   