#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <regex>
using namespace std;
template<typename T>
class Graph {
map<T, list<pair<T, int>>> l;
public:
void addEdge(T x, T y, int wt) {
l[x].push_back({ y, wt });
l[y].push_back({ x, wt });
}
void djikstraSSSP(T src, vector<int>& scores) {
map<T, int> dist;
for (auto p : l) {
T node = p.first;
dist[node] = INT_MAX;
}
dist[src] = 0;
set<pair<int, T>> s;
s.insert({ dist[src], src });
while (!s.empty()) {
pair<int, T> p = *s.begin();
s.erase(s.begin());
T currNode = p.second;
int currNodeDist = p.first;
for (auto nbr : l[currNode]) {
T nbrNode = nbr.first;
if (currNodeDist + nbr.second < dist[nbrNode]) {
auto pr = s.find({ dist[nbrNode], nbrNode });
if (pr != s.end()) {
s.erase(pr);
}
dist[nbrNode] = currNodeDist + nbr.second;
s.insert({ dist[nbrNode], nbrNode });
}
}
}
int i = 0;
for (auto x : dist) {
if (scores[i] != -1) {
if (scores[i] > x.second) {
if (x.second == -1) {
scores[i] = 0;
}
else {
scores[i] = x.second
}
}
}
else {
scores[i] = x.second;
}
++i;
}
}
};
int main() {
Graph<int> g;
unsigned int V = 0;
cin >> V;
int i = 0;
int paths = V - 1;
string roles;
getline(cin >> ws, roles);
std::regex r("\\s+");
roles = std::regex_replace(roles, r, "");
while (i < paths) {
int a, b, c;
cin >> a >> b >> c;
g.addEdge(a, b, c);
++i;
}
unsigned int j = 1;
vector<int> srcs;
while (j < roles.length() + 1) {
if (roles[j - 1] == '0') {
srcs.push_back(j);
}
++j;
}
for (int point : srcs) {
g.addEdge(0, point, 0);
}
vector<int> scores(V+srcs.size(), -1);
g.djikstraSSSP(0, scores);
int totalLength = 0;
for (int num : scores) {
totalLength += num;
}
cout << totalLength << endl;
}