#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<int> vcost;
vector<vector<pair<int, int> > > edges;
vector<pair<pair<int,int>, int> > order;
void dfs(int v, int p) {
for (size_t j = 0; j < edges[v].size(); j++) {
int w = edges[v][j].first;
if (w == p) continue;
order.push_back(make_pair(make_pair(w, v), edges[v][j].second));
dfs(w, v);
}
}
vector<vector<pair<ll, int> > > best;
void prop_edge(int v, int w, int c) {
// 保证两个末端不同
for (int j = 0; j < 2; j++) {
if (best[v][j].second == best[w][0].second) {
swap(best[w][0], best[w][1]);
}
best[w][1] = min(best[w][1], make_pair(best[v][j].first + c, best[v][j].second));
if (best[w][0] > best[w][1]) {
swap(best[w][0], best[w][1]);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
vcost.resize(n);
for (int i = 0; i < n; i++) cin >> vcost[i];
edges.resize(n);
for (int i = 0; i < n-1; i++) {
int a, b, c;
cin >> a >> b >> c;
a--; b--;
edges[a].push_back(make_pair(b, c));
edges[b].push_back(make_pair(a, c));
}
order.reserve(n-1);
dfs(0, -1);
ll total = 0;
vector<vector<int> > cc_edges(n);
vector<int> vis(n);
vector<int> which_cc(n);
vector<int> cc;
cc.reserve(n);
best.assign(n, vector<pair<ll, int> >(2));
vector<pair<ll, int> > cc_best;
cc_best.reserve(n);
vector<vector<int> > ccs;
ccs.reserve(n);
while (true) {
fill(vis.begin(), vis.end(), 0);
fill(which_cc.begin(), which_cc.end(), -1);
ccs.clear();
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
cc.clear();
cc.push_back(i);
vis[i] = 1;
for (int s = 0; s < int(cc.size()); s++) {
int v = cc[s];
for (size_t j = 0; j < cc_edges[v].size(); j++) {
int w = cc_edges[v][j];
if (!vis[w]) {
vis[w] = 1;
cc.push_back(w);
}
}
}
for (size_t j = 0; j < cc.size(); j++) {
which_cc[cc[j]] = int(ccs.size());
}
ccs.push_back(cc);
}
if (ccs.size() == 1) break;
for (int v = 0; v < n; v++) {
best[v][0] = make_pair(vcost[v], which_cc[v]);
best[v][1] = make_pair(1e18, -1);
}
for (int i = n-2; i >= 0; i--) {
pair<pair<int, int>, int>& e = order[i];
prop_edge(e.first.first, e.first.second, e.second);
}
for (int i = 0; i < n-1; i++) {
pair<pair<int, int>, int>& e = order[i];
prop_edge(e.first.second, e.first.first, e.second);
}
cc_best.resize(ccs.size());
fill(cc_best.begin(), cc_best.end(), make_pair(1e18, -1));
for (int v = 0; v < n; v++) {
for (int j = 0; j < 2; j++) {
if (best[v][j].second != which_cc[v]) {
cc_best[which_cc[v]] = min(cc_best[which_cc[v]],
make_pair(best[v][j].first + vcost[v], best[v][j].second));
}
}
}
for (int k = 0; k < int(ccs.size()); k++) {
int l = cc_best[k].second;
if (cc_best[l].second == k && l < k) continue;
total += cc_best[k].first;
cc_edges[ccs[k].front()].push_back(ccs[l].front());
cc_edges[ccs[l].front()].push_back(ccs[k].front());
}
}
cout << total << '\n';
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCmludCBuOwp2ZWN0b3I8aW50PiB2Y29zdDsKdmVjdG9yPHZlY3RvcjxwYWlyPGludCwgaW50PiA+ID4gZWRnZXM7Cgp2ZWN0b3I8cGFpcjxwYWlyPGludCxpbnQ+LCBpbnQ+ID4gb3JkZXI7Cgp2b2lkIGRmcyhpbnQgdiwgaW50IHApIHsKCWZvciAoc2l6ZV90IGogPSAwOyBqIDwgZWRnZXNbdl0uc2l6ZSgpOyBqKyspIHsKCQlpbnQgdyA9IGVkZ2VzW3ZdW2pdLmZpcnN0OwoJCWlmICh3ID09IHApIGNvbnRpbnVlOwoJCW9yZGVyLnB1c2hfYmFjayhtYWtlX3BhaXIobWFrZV9wYWlyKHcsIHYpLCBlZGdlc1t2XVtqXS5zZWNvbmQpKTsKCQlkZnModywgdik7Cgl9Cn0KCnZlY3Rvcjx2ZWN0b3I8cGF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