#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
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;
int distInBetween = nbr.second;
int nbrNodeDist = dist[nbrNode];
if (currNodeDist + distInBetween < nbrNodeDist) {
if (s.find({dist[nbrNode], nbrNode}) != s.end()) {
s.erase(pr);
}
dist[nbrNode] = currNodeDist + distInBetween;
s.insert({dist[nbrNode], nbrNode});
}
}
}
int i = 0;
for (auto x : dist) {
auto score = scores[i];
if (score != -1) {
if (score > x.second) {
scores[i] = x.second;
}
} else {
scores[i] = x.second;
}
++i;
}
}
};
int main() {
Graph<string> g;
int V = 0;
cin >> V;
int i = 0;
int paths = V-1;
string roles;
std::getline(cin >> std::ws, roles);
vector<int> scores(V, -1);
while (i < paths) {
string a,b;
int c;
cin >> a >> b >> c;
g.addEdge(a,b,c);
++i;
}
int j = 1;
for (char& c : roles) {
string letter = string(1,c);
if (letter == "0") {
g.djikstraSSSP(std::to_string(j), scores);
++j;
}
if (letter == "1") {
++j;
}
}
int totalLength = 0;
for (int num : scores) {
totalLength += num;
}
cout << totalLength;
}