#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
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 print() {
		for (auto p : l) {
			T node = p.first;
			//cout << node << " -> ";
			for (auto nbr : l[node]) {
				//cout << "(" << nbr.first << "," << nbr.second << ") ";
			} //cout << endl;
		}
	}
	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) {
					auto pr = s.find({dist[nbrNode], nbrNode});
					if (pr != s.end()) {
						s.erase(pr);
					}
					dist[nbrNode] = currNodeDist + distInBetween;
					s.insert({dist[nbrNode], nbrNode});
				}
			}
		}
        int i = 0;
		for (auto x : dist) {
			if (scores[i] != -1) {
				if (scores[i] > x.second) {
					scores[i] = x.second;
				}
			} else {
				scores[i] = x.second;
            }
			//cout << x.first << " is at distance " << x.second << " from source" << endl;
		++i;
		}
	}
};
int main() {
	Graph<string> g;
	int V = 0;
	cin >> V;
	int i = 0;
	int paths = V-1;
	string roles;
	cin >> roles;
	vector<int> scores(V, -1);
	while (i < paths) {
		string a,b;
		int c;
		cin >> a >> b >> c;
		g.addEdge(a,b,c);
		++i;
	}
	g.print();
	int j = 1;
	for (char& c : roles) {
		if (string(1,c) == "0") {
			g.djikstraSSSP(std::to_string(j), scores);
		}
        ++j;
	}
    int totalLength = 0;
	for (int num : scores) {
    	//cout << num << endl;
        totalLength += num;
	}
	cout << totalLength << endl;
	std::this_thread::sleep_for(std::chrono::seconds(1000));
}