#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)

class UF{
private:
    vector<int> par,rank,elm;
    vector<vector<int>> elmList;	//don't be validated
public:
    int groups;
    
    UF(int __size):par(__size) , rank(__size,0) , elm(__size,1) , groups(__size), elmList(__size){
        rep(i,__size)elmList[i] = vector<int>(1,i);
        for(int i=0;i<__size;i++)par[i]=i;
    }
    
    int getElements(int n){ return elm[find(n)]; }
    vector<int> getElementsList(int n){ return elmList[find(n)]; }
    
    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;
        
        groups--;
        if(rank[x]<rank[y]){
            par[x]=y;
            elm[y]+=elm[x];
            for(auto &e:elmList[x])elmList[y].pb(e);
            
        }else{
            par[y]=x;
            elm[x]+=elm[y];
            for(auto &e:elmList[y])elmList[x].pb(e);
            if(rank[x]==rank[y])rank[x]++;
        }
    }
    
    bool isSame(int x,int y){
        return find(x)==find(y);
    }
};


#define MAX_V 200
struct edge{int to,cost;};
int v,e;


int main(){
    while(cin>>v>>e&&(v||e)){
        vector<edge> G[MAX_V];
        vector<int> h(v);
        map<int,int> zip;
        set<int> nums;
        rep(i,v){
            cin>>h[i];
            nums.insert(h[i]);
        }
        
        rep(i,e){
            int s,t,c;
            cin>>s>>t>>c;
            s--,t--;
            G[s].pb(edge{t,c});
            G[t].pb(edge{s,c});
        }
        
        int count=0;
        for(auto e:nums){
            zip[e] = count++;
        }
        rep(i,v)h[i] = zip[h[i]];
        
        auto connected = [&](int d){
            UF uf(v);
            rep(i,v){
                for(auto u:G[i]){
                    if( h[u.to]>=d && h[i]>=d ){
                        uf.unite(u.to,i);
                    }
                }
            }
            int val = -1;
            rep(i,v){
                if(h[i]>=d){
                    if(val==-1)val = uf.find(i);
                    else{
                        if(uf.find(i)!=val)return false;
                    }
                }
            }
            return true;
        };
        
        int ng_point = -1;
        for(auto d:nums){
            if( !connected(zip[d]) ){
                ng_point = zip[d];
                break;
            }
        }
        
        if(ng_point==-1)ng_point = nums.size();
        
        
        auto kruskal = [&](){
            int ret=0;
            vector<pair<int,pii>> edges;
            
            rep(i,MAX_V)for(auto &e:G[i])edges.pb(make_pair(e.cost,pii(i,e.to)));
            
            sort(all(edges));
            
            UF uf(v);
            
            for(int k=ng_point-1;k>=0;k--){
                rep(i,edges.size()){
                    int c,u,v;
                    c = edges[i].first;
                    tie(u,v) = edges[i].second;
                    
                    if( h[u]>=k && h[v]>=k && !uf.isSame(u,v)){
                        //                    cout<<h[u]<<" "<<h[v]<<" "<<k<<endl;
                        //                    cout<<"unite "<<u<<" "<<v<<endl;
                        uf.unite(u,v);
                        ret+=c;
                    }
                }
                
                assert(connected(k));
            }
            return ret;
        };
        
        cout<<kruskal()<<endl;
    }
}

Measured memory_bytes: 4620