#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <regex>
#include <numeric>
#include <algorithm>
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;
unsigned int j = 0;
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;
unsigned int i = 0;
while (i < l[currNode].size()) {
auto nbr = l[currNode][i];
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 });
}
++i;
}
}
unsigned int i = 0;
while (i < dist.size()) {
auto x = dist[i];
if (scores[i] != 0) {
if (scores[i] > x.second) {
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;
std::string::difference_type rl = std::count(roles.begin(), roles.end(), '0');
while (j < roles.length() + 1) {
if (roles[j - 1] == '0') {
g.addEdge(0, j, 0);
}
++j;
}
vector<int> scores(V + rl, 0);
g.djikstraSSSP(0, scores);
int totalLength = std::reduce(scores.begin(), scores.end());
std::cout << totalLength << endl;
}