#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

struct UnionFind {
    vector<int> par;
    int n, cnt;
    UnionFind(const int& x = 0) {init(x);}
    void init(const int& x) {par.assign(cnt=n=x, -1);}
    inline int find(const int& x) {return par[x] < 0 ? x : par[x] = find(par[x]);}
    inline bool same(const int& x, const int& y) {return find(x) == find(y);}
    inline bool unite(int x, int y) {
        if ((x = find(x)) == (y = find(y))) return false;
        --cnt;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    inline int count() const {return cnt;}
    inline int count(int x) {return -par[find(x)];}
};



struct Edge
{	
	int u;
	int v;
	int cost;
	Edge() {}	
	Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}	
	bool operator<(const Edge& rhs) const {return cost < rhs.cost;}
};

const int MAXN = 222;
const int MAX = 1000100;
int N, M;
vector<int> vanish[MAX];
bool done[MAXN];
vector<Edge> G;

ll solve() {
	// ??£????????????????????????????????? 1
	{
		UnionFind uf(N);
		for (Edge e : G)
			uf.unite(e.u, e.v);
		if (uf.count() > 1) return 0;
	}
	// ????????????????????°??§??£?????§???????????????????????????
	// t=last ??§??£?????§??????????????´???, t=last+1 ??\????????£??????????????????
	// t=last, last-1, ... ?????????????????£????????????????????°??????
	int last = -1;
	memset(done, false, sizeof(done));
	for (int t = 0; t < MAX; t++) {
		if (vanish[t].empty()) continue;
		// ????????????????¶???????
		for (int v : vanish[t]) {
			done[v] = true;
		}
		// ?????£??????????????£???????????§??????
		UnionFind uf(N);
		for (Edge e : G) {
			if (done[e.u] || done[e.v]) continue;
			uf.unite(e.u, e.v);
		}
		int sz = 1;
		for (int i = 0; i < N; i++) 
			if (done[i]) sz++;
		if (uf.count() != sz) {
			last = t;
			break;
		}
	}
	UnionFind uf(N);
	ll ans = 0;
	memset(done, false, sizeof(done));
	for (int t = last; t < MAX; t++) {
		for (int v : vanish[t]) {
			done[v] = true;
		}
	}
	for (Edge e : G) {
		if (!done[e.u] || !done[e.v]) continue;
		if (uf.same(e.u, e.v)) continue;
		ans += e.cost;
		uf.unite(e.u, e.v);
	}
	for (int t = last-1; t >= 0; t--) {
		if (vanish[t].empty()) continue;
		for (int v : vanish[t]) 
			done[v] = true;
		for (Edge e : G) {
			if (!done[e.u] || !done[e.v]) continue;
			if (uf.same(e.u, e.v)) continue;
			ans += e.cost;
			uf.unite(e.u, e.v);
		}
	}
	return ans;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> N >> M) {
		if (N==0 && M==0) break;
		for (int i = 0; i < MAX; i++) 
			vanish[i].clear();
		for (int i = 0; i < N; i++) {
			int h;
			cin >> h;
			vanish[h].push_back(i);
		}
		G.clear();
		for (int i = 0; i < M; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			a--; b--;
			G.emplace_back(a, b, c);
		}
		sort(G.begin(), G.end());
		cout << solve() << endl;
	}
	return 0;
}

Measured memory_bytes: 27044