#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
using tpl = tuple<int, int, ll>;
vector<int> visited;
class Compare {
public:
bool operator() (tpl a, tpl b) {return (get<2>(a) < get<2>(b) && !visited[get<1>(a)]) || visited[get<1>(b)];}
};
using pqueue = priority_queue<tpl, vector<tpl>, Compare>;
vector<int> harbors;
int n;
map<int, pqueue> reachableFrom;
map<int, vector<tpl>> network;
map<int,ll> roadLengths;
tpl next() {
ll m = LLONG_MAX;
map<int,pqueue>::iterator best;
for (auto it = reachableFrom.begin(); it!= reachableFrom.end(); it++) {
auto x = (it->second).top();
// ll t = get<2>((it->second).top());
while (visited[get<1>(x)]) {
(it->second).pop();
if ((it->second).empty())
break;
x = (it->second).top();
}
if (get<2>(x) < m && !visited[get<1>(x)]) {
m = get<2>(x);
best = it;
}
}
tpl return_val = (best->second).top();
(best->second).pop();
roadLengths[get<1>(return_val)] = get<2>(return_val);
for (auto x : network[get<1>(return_val)]) {
if (get<1>(x) != get<0>(return_val)) {
reachableFrom[best->first].push({get<0>(x), get<1>(x), roadLengths[get<0>(x)] + get<2>(x)});
}
}
return return_val;
}
int main() {
int a,b,t;
ll c;
cin >> n;
visited = vector<int>(n, 0);
for (int i=0; i<n; i++) {
cin >> t;
network[i] = vector<tpl>();
roadLengths[i] = 0;
if (t==0) {
harbors.push_back(i);
reachableFrom[i] = pqueue();
visited[i] = 1;
}
}
for (int i=0; i<n-1; i++) {
cin >> a >> b >> c; a--; b--;
network[a].push_back({a,b,c});
network[b].push_back({b,a,c});
if (reachableFrom.count(a)) {
reachableFrom[a].push({a,b,c});}
if (reachableFrom.count(b)) {
reachableFrom[b].push({b,a,c});}
}
tpl T;
ll total = 0;
for (int i=0; i<n-harbors.size(); i++) {
T = next();
visited[get<1>(T)] = 1;
}
for (int i=0; i<n; i++) {
total += roadLengths[i];
}
cout << total;
}