#include<iostream>
#define MAX 10
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef char E;
typedef struct Edge{
E start;
E end;
int weight;
}*Edges;
typedef struct GraphMatrix{
int vex,edge;
int matrix[MAX][MAX];
E data[MAX];
}*Graph;
Graph Create(){
Graph g = (Graph)malloc(sizeof(GraphMatrix));
g->vex=g->edge=0;
memset(g->matrix,0,sizeof(g->matrix));
return g;
}
void addVex(Graph g,E e){
g->data[g->vex++]=e;
}
int getIndex(Graph g,E e){
for(int i=0;i<g->vex;i++)
if(g->data[i]==e)
return i;
return -1;
}
void addEdge(Graph g,E v1,E v2,int w,Edges e){
int a=getIndex(g,v1);
int b=getIndex(g,v2);
g->matrix[a][b]=w;
g->matrix[b][a]=w;
e[g->edge].start=v1;
e[g->edge].end=v2;
e[g->edge].weight=w;
g->edge++;
}
void Kruscal(Graph g){
int vLen=g->vex;
}
int getEnds(int ends[],int i){
while(ends[i]!=0){
i=ends[i];
}
return i;
}
void Kruskal(Graph g,Edges e){
int index=0;
int ends[g->edge];//保存最小生成树中每个顶点的终点
for(int i=0;i<g->edge;i++)
ends[i]=0;
//创建结果数组 ,保存最终的最小生成树 Res 为Result的缩写
Edges edgeRes =(Edges) malloc(g->edge*sizeof(struct Edge));
//按照边的权值大小排序,我已经在main里排序过了
//遍历edges数组从最小边开始加,加一次判回路一次,没回路就加进来(edgeRes),有回路就换下一条
for(int i =0;i<g->edge;i++) {
//拿到第i条边的顶点的下标start
int p1= getIndex(g,e[i].start);
//拿到第i条边的顶点的下标start
int p2= getIndex(g,e[i].end);
//获取p1顶点的在已有的最小生成树中终点是?
int m =getEnds(ends,p1);
//获取p2顶点的在已有的最小生成树中终点是?
int n =getEnds(ends,p2);
//是否构成回路
if(m!=n) {//不构成回路
ends[m] =n; //设置m 在已有最小生成树的终点
edgeRes[index++]=e[i];
}
}
//统计并打印最小生成树
int temp=0;
for(int i =0;i<index;i++){
temp+=edgeRes[i].weight;
}
cout<<temp<<endl;
}
int cmp(const void *a, const void *b){
return ((struct Edge *)a)->weight - ((struct Edge *)b)->weight;
}
int main(){
int n;
cin>>n;
Graph graph=Create();
Edges edges;
edges=( Edges)malloc((n*(n-1)/2)*sizeof(struct Edge));
for(int i=0;i<n;i++){
char e;cin>>e;
addVex(graph,e);
}
E v1,v2;int w;
while(cin>>v1>>v2){
cin>>w;
addEdge(graph,v1,v2,w,edges);
}
qsort(edges,graph->edge, sizeof(Edge), cmp);
Kruskal(graph,edges);
}