#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
bool field[2*100000+1];
vector<pair<int, int>> adj[2*100000+1];
const int INF = INT_MAX-1;
int djikstra(int x);
int main() {
cin >> n;
for(int i=1; i<=n; i++) {
cin >> field[i];
}
for(int i=0; i<n-1; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int sum = 0;
for(int i=1; i<=n; i++) {
sum += djikstra(i);
}
cout << sum << endl;
}
int djikstra(int x) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
bool visited[2*100000+1] = {};
int distance[2*100000+1];
for (int i=0; i<n; i++) {
distance[i] = INF;
}
distance[x] = 0;
q.push({0, x});
int shortest = INF;
while (!q.empty()) {
int a = q.top().second, w = q.top().first; q.pop();
if(visited[a]) continue;
visited[a] = true;
if(!field[a]) shortest = w < shortest ? w : shortest;
bool allFar = true;
for (auto u : adj[a]) {
int b = u.first, c = u.second;
if(c + w < shortest) allFar = false;
if(distance[a]+c < distance[b]) {
distance[b] = distance[a]+c;
q.push({distance[b], b});
}
}
if (allFar) {
return shortest;
}
}
return shortest;
}