#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;

struct bridge {
	int from;
	int to;
	int cost;
};
class Compare {
public:
	//aa?????????????????¶
	bool operator()(const bridge&l, const bridge &r) {
		return l.cost> r.cost;
	}
};
int main() {
	while (1) {
		int N, M; cin >> N >> M;
		if (!N)break;
		vector<pair<int,int>>hs;
		for (int i = 0; i < N; ++i) {
			int h; cin >> h;
			hs.emplace_back(h, i);
		}
		vector<vector<bridge>>bs(N);
		sort(hs.begin(), hs.end());
		for (int i = 0; i < M; ++i) {
			int a, b, c; cin >> a >> b >> c;
			a--; b--;
			bs[a].push_back(bridge{ a,b,c });
			bs[b].push_back(bridge{ b,a,c });
		}
		vector<vector<int>>sinks;
		{
			int now = hs[0].first;
			vector<int>vs(1, hs[0].second);
			for (int i = 1; i < hs.size(); ++i) {
				if (hs[i].first == now) {
					vs.push_back(hs[i].second);
				}
				else {
					sinks.push_back(vs);
					vs.clear();
					vs.push_back(hs[i].second);
					now = hs[i].first;
				}
			}
			sinks.push_back(vs);
			
		}
		reverse(sinks.begin(), sinks.end());
		for (int from = 0; from <= sinks.size(); ++from) {
			if (from == sinks.size()) {
				cout << 0 << endl;
				break;
			}
			bool ok = true;
			int costsum = 0;
			vector<int>status(N);
			{
				priority_queue <bridge, vector<bridge>,Compare>que;
				vector<int>waits;

				for (int i = 0; i <= from; ++i) {
					for (auto island : sinks[i]) {
						waits.push_back(island);
					}
				}
				for (int i = 0; i < waits.size(); ++i) {
					for (auto b : bs[waits[i]]) {
						if (find(waits.begin(), waits.end(), b.to) != waits.end()) {
							que.push(b);
						}
					}
				}
				if (que.empty())continue;
				{
					bridge atop(que.top());
					que.pop();
					status[atop.from] = 1;
					status[atop.to] = 1;
					waits.erase(find(waits.begin(), waits.end(), atop.from));
					waits.erase(find(waits.begin(), waits.end(), atop.to));
					costsum += atop.cost;
				}

				while (!waits.empty() && !que.empty()) {
					bridge atop(que.top());
					que.pop();
					if (status[atop.from] == 0 && status[atop.to] ==1) {
						status[atop.from] = 1;
						waits.erase(find(waits.begin(), waits.end(), atop.from));
						for (auto b : bs[atop.from]) {
							if (find(waits.begin(), waits.end(), b.to) != waits.end()) {
								que.push(bridge{ b.to,b.from,b.cost });
							}
						}
						costsum += atop.cost;
					}
				}
				if (!waits.empty())continue;
			}
			for (int time = from+1; time < sinks.size(); ++time) {
				priority_queue <bridge, vector<bridge>,Compare>que;
				vector<int>waits;

				
				for (auto island : sinks[time]) {
					waits.push_back(island);
				}
				for (int i = 0; i < waits.size(); ++i) {
					for (auto b : bs[waits[i]]) {
						if (status[b.to]) {
							que.push(b);
						}
					}
				}
				
				while (!waits.empty() && !que.empty()) {
					bridge atop(que.top());
					que.pop();
					if (status[atop.from] == 0 && status[atop.to] == 1) {
						status[atop.from] = 1;
						waits.erase(find(waits.begin(), waits.end(), atop.from));
						for (auto b : bs[atop.from]) {
							if (find(waits.begin(), waits.end(), b.to) != waits.end()) {
								que.push(bridge{ b.to,b.from,b.cost });
							}
						}
						costsum += atop.cost;
					}
				}
				if (!waits.empty()) {
					ok = false;
					break;
				}
			}
			if (!ok)continue;
			else {
				cout << costsum << endl;
				break;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 5136