#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	Info(){
		from = to = cost = erase_day = 0;
	}
	Info(int arg_from,int arg_to,int arg_cost,int arg_erase_day){
		from = arg_from;
		to = arg_to;
		cost = arg_cost;
		erase_day = arg_erase_day;
	}

	bool operator<(const struct Info &arg) const{
		return cost < arg.cost;
	};
	int from,to,cost,erase_day;
};

struct Data{
	bool operator<(const struct Data &arg) const{
		return day < arg.day;
	};
	int id,day;
};

Info* info;

int parent[200],height[200],N,M;

int find(int x){
	if(x == parent[x])return x;
	else{
		return parent[x] = find(parent[x]);
	}
}

bool isSame(int x,int y){
	return find(x) == find(y);
}

void unite(int x,int y){
	x = find(x);
	y = find(y);

	if(x == y)return;

	if(height[x] < height[y]){
		parent[x] = y;
	}else{
		parent[y] = x;
		if(height[x] == height[y])height[x]++;
	}
}

void init(){
	for(int i = 0; i < N; i++){
		parent[i] = i;
		height[i] = 0;
	}
}

void func(){

	bool isAlive[N];
	for(int i = 0; i < N; i++)isAlive[i] = true;

	Data data[N+1];

	for(int i = 0; i < N; i++){
		scanf("%d",&data[i].day);
		data[i].id = i;
	}
	data[N].day = BIG_NUM;

	for(int i = 0; i < M;i++){
		scanf("%d %d %d",&info[i].from,&info[i].to,&info[i].cost);
		info[i].from--;
		info[i].to--;
		info[i].erase_day = min(data[info[i].from].day,data[info[i].to].day);
	}

	vector<Info> V;

	sort(data,data+(N+1));
	sort(info,info+M);


	init();

	ll first = 0;

	for(int i = 0; i < M; i++){
		if(!isSame(info[i].from,info[i].to)){
			unite(info[i].from,info[i].to);
			V.push_back(Info(info[i].from,info[i].to,info[i].cost,0));
			first += info[i].cost;
		}
	}

	int num = 0;
	for(int i = 0;i < N; i++){
		if(parent[i] == i)num++;
	}

	if(num > 1){
		printf("0\n");
		return;
	}

	stack<int> S;

	int tmp_day,last_day = data[0].day;
	int count = N;

	for(int sche = 0; sche < N; ){

		tmp_day = data[sche].day;
		S.push(data[sche].id);
		sche++;
		while(data[sche].day == tmp_day && sche < N){
			S.push(data[sche].id);
			sche++;
		}

		while(!S.empty()){
			isAlive[S.top()] = false;
			count--;
			S.pop();
		}

		if(count <= 1)break;
		init();

		for(int i = 0; i < V.size(); i++){
			if(isAlive[V[i].from] == true && isAlive[V[i].to] == true){
				unite(V[i].from,V[i].to);
			}
		}

		for(int i = 0; i < M; i++){
			if(isSame(info[i].from,info[i].to) == false && isAlive[info[i].from] == true && isAlive[info[i].to] == true){
				unite(info[i].from,info[i].to);
				V.push_back(Info(info[i].from,info[i].to,info[i].cost,0));
			}
		}

		num = 0;
		for(int i = 0;i < N; i++){
			if(isAlive[i] == true && parent[i] == i)num++;
		}

		if(num > 1){
			break;
		}

		last_day = data[sche].day;

	}

	ll ans = 0,tmp = 0;

	if(last_day == 0){
		ans = first;
	}else{

		int index;
		for(index = N-1; data[index].day > last_day; index--);

		init();
		for(;index >= 0; index--){
			tmp_day = data[index].day;

			tmp = 0;
			for(int i = 0; i < M; i++){
				if(isSame(info[i].from,info[i].to) == false && info[i].erase_day >= tmp_day){
					unite(info[i].from,info[i].to);
					tmp += info[i].cost;
				}
			}

			ans += tmp;
		}
	}

	printf("%lld\n",ans);
}

int main(){

	info = (Info*)malloc(sizeof(Info)*20000);

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3500