#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

using Weight = int;
struct Edge {
	int s, d; Weight w;
	Edge() {};
	Edge(int s, int d, Weight w) : s(s), d(d), w(w) {};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) { return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')'); }

using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

void addArc(Graph &g, int s, int d, Weight w = 1) {
	g[s].emplace_back(s, d, w);
}
void addEdge(Graph &g, int a, int b, Weight w = 1) {
	addArc(g, a, b, w);
	addArc(g, b, a, w);
}

struct UnionFind {
	vector<int> parent;
	int size;
	UnionFind(int n) :parent(n, -1), size(n) {}
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x == y)return false;
		if (sizeOf(x) < sizeOf(y))swap(x, y);
		parent[x] += parent[y]; parent[y] = x; size--;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
	int sizeOf(int x) { return -parent[root(x)]; }
};

template<class T, class ...Tail>
void tiedSort(vector<T> &a, vector<Tail>&... tail) {
	int n = a.size();
	using S = tuple<T, Tail...>;
	vector<S> s(n);
	for (int i = 0; i < n; i++)
		s[i] = make_tuple(a[i], tail[i]...);
	sort(s.begin(), s.end());
	for (int i = 0; i < n; i++)
		tie(a[i], tail[i]...) = s[i];
}

pair<Weight, Edges> kruskal(const Graph &g) {
	int n = g.size();
	UnionFind uf(n);
	Edges es;
	for (int i = 0; i < n; i++)
		for (auto &e : g[i])
			es.push_back(e);
	sort(es.begin(), es.end());
	Weight total = 0;
	Edges mst;
	for (auto &e : es)
		if (!uf.same(e.s, e.d)) {
			uf.unite(e.s, e.d);
			mst.push_back(e);
			total += e.w;
		}
	return make_pair(total, mst);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	for (int N, M; cin >> N >> M && N;) {
		vector<int> h(N); rep(i, 0, N) {
			cin >> h[i];
		}
		vector<int> a(M), b(M), c(M); rep(i, 0, M) {
			cin >> a[i] >> b[i] >> c[i];
			a[i]--, b[i]--;
		}
		tiedSort(c, a, b);

		vector<int> id(N); iota(all(id), 0);

		tiedSort(h, id);
		dump(id);


		UnionFind uff(N);
		rep(i, 0, M)
			uff.unite(a[i], b[i]);
		if (uff.size != 1) {
			cout << 0 << endl;
			continue;
		}

		vector<vector<int>> v;
		vector<int> t;
		rep(i, 0, N) {
			if (t.size() == 0 || h[t.back()] == h[i]) {
				t.push_back(i);
			}
			else {
				v.push_back(t);
				t.clear();
				t.push_back(i);
			}
		}
		if (t.size())v.push_back(t);
		dump(v);

		unordered_set<int> st;
		int x;
		for (x = 0; x < v.size() - 1; x++) {
			UnionFind uf(N);
			for (auto u : v[x]) {
				st.emplace(id[u]);
			}
			rep(j, 0, M) {
				if (st.count(a[j]) || st.count(b[j]))
					continue;
				uf.unite(a[j], b[j]);
			}
			dump(uf.parent);
			if (uf.sizeOf(id[v.back().front()]) != N - st.size())
				break;
		}
		dump(x);
		for (auto u : v[x])
			st.erase(id[u]);
		dump(st);

		Graph g(N);
		rep(i, 0, M) {
			if (st.count(a[i]) || st.count(b[i]))
				continue;
			addEdge(g, a[i], b[i], c[i]);
		}
		auto res = kruskal(g);
		dump(res);

		UnionFind uf(N);
		for (auto e : res.second) {
			uf.unite(e.d, e.s);
		}

		int cost = res.first;
		dump(cost);

		rrep(y, 0, x) {
			for (auto u : v[y])
				st.erase(id[u]);
			int add = 0;
			rep(j, 0, M) {
				if (st.count(a[j]) || st.count(b[j]))continue;
				if (!uf.same(a[j], b[j])) {
					uf.unite(a[j], b[j]);
					add += c[j];
				}
			}
			cost += add;
			dump(uf.parent);
			dump(cost);
		}

		cout << cost << endl;

	}
	return 0;
}


Measured memory_bytes: 7220