#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const long long int MOD = 1000000007;
const int INF = 1000000000;

struct UF{
	int par[200];
	int rank[200];
	int si[200];

	void init(int n){
		rep(i,n){
			par[i] = i;
			rank[i] = 0;
			si[i] = 1;
		}
	}

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

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;
		
		if(rank[x] < rank[y]){
			par[x] = y;
			si[y] += si[x];
		} else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
			si[x] += si[y];
		}
	}

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

struct edge{
    int t;
    int a, b, c;
    bool operator<( const edge& right) const {
        if(c < right.c) return true;
        else return false;
    }
};

UF uf;
int n, m;
int h[200];
vector<int> ho;
vector<edge> vec;

void solve(){
    ho.clear();
    vec.clear();
    rep(i,n) cin >> h[i];
    rep(i,n) ho.push_back(h[i]);
    sort(ho.begin(),ho.end());
    ho.erase(unique(ho.begin(),ho.end()),ho.end());
    sort(ho.begin(),ho.end(),greater<int>());
    rep(i,m){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        edge e;
        e.t = min(h[a],h[b]);
        e.a = a; e.b = b; e.c = c;
        vec.push_back(e);
    }
    sort(vec.begin(),vec.end());
    ll ans = 0;
    int ss = 0, ee = ho.size(), mid;
    while(ee-ss > 1){
        mid = (ee+ss)/2;
        uf.init(n);
        ll preans = 0;
        bool ok = true;
        for(int u = mid; u < ho.size(); u++){
            UF tmp = uf;
            ll cnt = 0;
            rep(i,vec.size()){
                if(ho[u] > vec[i].t) continue;
                if(!tmp.same(vec[i].a,vec[i].b)){
                    tmp.unite(vec[i].a,vec[i].b);
                    cnt += vec[i].c;
                }
            }
            int huga = 0;
            rep(i,n){
                if(h[i] < ho[u]) continue;
                if(i == tmp.find(i)) huga++;
            }
            if(huga == 1){
                uf = tmp;
                preans += cnt;
            }
            if(huga > 1){
                ss = mid;
                ok = false;
                break;
            }
        }
        if(ok){
            ee = mid;
            ans = preans;
        }
    }
    {
        int v = ss; 
        uf.init(n);
        ll preans = 0;
        bool ok = true;
        for(int u = v; u < ho.size(); u++){
            UF tmp = uf;
            ll cnt = 0;
            rep(i,vec.size()){
                if(ho[u] > vec[i].t) continue;
                if(!tmp.same(vec[i].a,vec[i].b)){
                    tmp.unite(vec[i].a,vec[i].b);
                    cnt += vec[i].c;
                }
            }
            int huga = 0;
            rep(i,n){
                if(h[i] < ho[u]) continue;
                if(i == tmp.find(i)) huga++;
            }
            if(huga == 1){
                uf = tmp;
                preans += cnt;
            }
            if(huga > 1){
                ss = mid;
                ok = false;
                break;
            }
        }
        if(ok){
            ee = mid;
            ans = preans;
        }
    }

    cout << ans << endl;
}

int main(){
    while(cin >> n >> m){
        if(n == 0 && m == 0) break;
        solve();
    }
}

Measured memory_bytes: 3500