#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int size;
bool *types;
void lenght() {
// starting vector
vector<pair<int,int>> web[size + 1];
int a, b, c;
for (int i=0; i < size - 1; i++) {
cin >> a >> b >> c;
web[a].push_back({c, b});
web[b].push_back({c, a});
}
int sum = 0;
// array looped
priority_queue<pair<int,pair<int,int>>> q;
pair<int,pair<int,int>> p;
int best;
for (int i=1; i <= size; i++) {
if (!types[i]) continue;
// individual vector
best = -1e9;
q.push({0, {i, 0}});
while (!q.empty()) {
p = q.top();
q.pop();
// best route found -> clearing queue
if (p.first < best) {
while (!q.empty()) q.pop();
break;
};
for (auto b : web[p.second.first]) {
// port
if (!types[b.second]) best = max(best, p.first - b.first);
// field
else if (b.second != p.second.second) {
q.push({p.first - b.first, {b.second, p.second.first}});
};
}
}
sum -= best;
}
cout << sum << "\n";
}
int main() {
cin >> size;
types = new bool[size + 1];
for (int i=1; i <= size; i++) {
cin >> types[i];
}
lenght();
}