#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

// Union-Find
struct UF {
	int c;
	vector<int> p;
	UF() = default;
	explicit UF(int n): c(n), p(n, -1) {}
	bool unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return false;
		if (p[x] > p[y]) swap(x, y);
		p[x] += p[y];
		p[y] = x;
		c--;
		return true;
	}
	int find(int x) { return (p[x] < 0 ? x : p[x] = find(p[x])); }
	bool same(int x, int y) { return find(x) == find(y); }
	int size(int x) { return -p[find(x)]; }
	int count() { return c; }
};

template <typename T> map<T, int> coordinate_compression(vector<T> x) {
    int n = x.size();
    vector<int> y(n);
    iota(y.begin(), y.end(), 0);
    sort(y.begin(), y.end(), [&](int i, int j) { return x[i] < x[j]; });
    map<T, int> res;
    for (int i : y) if (!res.count(x[i])) {
        int j = res.size();
        res[x[i]] = j;
    }
    return res;
}

using P = pair<int, int>;
int N, M;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (cin >> N >> M, N) {
        vector<int> h(N), a(M), b(M), c(M);
        vector<vector<P>> g(N);
        REP (i, N) cin >> h[i];
        REP (i, M) {
            cin >> a[i] >> b[i] >> c[i];
            a[i]--, b[i]--;
            g[a[i]].emplace_back(b[i], c[i]);
            g[b[i]].emplace_back(a[i], c[i]);
        }

        auto mp = coordinate_compression<int>(h);
        vector<vector<int>> group(mp.size());
        REP (i, N) group[mp[h[i]]].push_back(i);
        reverse(group.begin(), group.end());

        vector<bool> used(N, false), is_connect(mp.size(), false);
        UF uf(N);
        int sz = 0;
        REP (i, group.size()) {
            for (int v : group[i]) used[v] = true, sz++;
            for (int v : group[i]) for (P e : g[v]) if (used[e.first]) {
                uf.unite(v, e.first);
            }
            is_connect[i] = (uf.size(group[i][0]) == sz);
        }

        int disconnect = -1;
        REP(i, group.size()) if (!is_connect[i]) {
            disconnect = i;
        }
        if (disconnect == group.size() - 1) {
            cout << 0 << endl;
            continue;
        }

        set<int> st;
        REP (i, disconnect + 2) for (int v : group[i]) st.insert(v);

        priority_queue<P, vector<P>, greater<P>> que;
        used.assign(N, false);
        que.emplace(0, group[0][0]);
        int ans = 0;
        while (!que.empty()) {
            P p = que.top();
            que.pop();
            if (used[p.second]) continue;
            used[p.second] = true;
            ans += p.first;
            for (P& e : g[p.second]) {
                if (st.count(e.first)) {
                    que.emplace(e.second, e.first);
                }
            }
        }

        FOR (i, disconnect + 2, group.size()) {
            for (int v : group[i]) st.insert(v);
            REP (i, N) if (used[i]) for (P& e : g[i]) if (st.count(e.first) && !used[e.first]) {
                que.emplace(e.second, e.first);
            }
            while (!que.empty()) {
                P p = que.top();
                que.pop();
                if (used[p.second]) continue;
                used[p.second] = true;
                ans += p.first;
                for (P& e : g[p.second]) {
                    if (st.count(e.first) && !used[e.first]) {
                        que.emplace(e.second, e.first);
                    }
                }
            }
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 5324