#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}

class UnionFind{

private:
    vector<int> data;
    int num;
  public:
      UnionFind(){};
      UnionFind(int size){
          data = vector<int>(size, -1);
          num = size;
      }

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

    bool unite(int x, int y){
        if(same(x,y)) return(false);
        x = find(x);
        y = find(y);
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        num--;
        return(true);
    }

    int find(int x){
        if(data[x] < 0) return(x);
        return(data[x] = find(data[x]));
    }

    int size(int x){
        return(-data[find(x)]);
    }

    int count(){
        return num;
    }
};

struct edge{
    int from,to,cost;
    bool operator<(const edge& e) const {
        return cost < e.cost;
    }
};

signed main(void) {
    int n,m;
    while(cin >> n >> m, n){
        vp h(n);
        rep(i,n){
            cin >> h[i].first;
            h[i].second = i;
        }
        vector<vector<edge> > v(n);
        rep(i,m){
            int a,b,c;
            cin >> a >> b >> c;
            a--,b--;
            v[a].push_back((edge){a,b,c});
            v[b].push_back((edge){a,b,c});
        }
        sort(all(h));
        reverse(all(h));
        UnionFind uf(n);
        vi used(n,0);
        int tim = INF;
        rep(i,n){
            vector<edge> ve;
            int nxt;
            loop(j,i,n)if(h[i].first == h[j].first){
                nxt = j;
                int ind = h[j].second;
                used[ind]++;
                rep(k,v[ind].size())ve.push_back(v[ind][k]);
            }
            sort(all(ve));
            rep(j,ve.size()){
                int u = ve[j].from;
                int v = ve[j].to;
                int cost = ve[j].cost;
                if(used[u] && used[v] && !uf.same(u,v)){
                    uf.unite(u,v);
                }
            }
            rep(j,n)if(used[j])rep(k,j)if(used[k]){
                if(!uf.same(j,k)){
                    tim = min(tim, h[i].first);
                }
            }
            i = nxt;
        }
        if(h[n-1].first == tim){
            cout << 0 << endl;
            continue;
        }
        uf = UnionFind (n);
        used = vi(n,0);
        int ans = 0;
        vector<edge> ve;
        rep(i,n){
            int nxt;
            loop(j,i,n)if(h[i].first == h[j].first){
                nxt = j;
                int ind = h[j].second;
                used[ind]++;
                rep(k,v[ind].size())ve.push_back(v[ind][k]);
            }
            if(h[nxt].first < tim){
                sort(all(ve));
                rep(j,ve.size()){
                    int u = ve[j].from;
                    int v = ve[j].to;
                    int cost = ve[j].cost;
                    if(used[u] && used[v] && !uf.same(u,v)){
                        uf.unite(u,v);
                        ans += cost;
                    }
                }
                ve.resize(0);
            }
            i = nxt;
        }
        //cout << tim << endl;
        cout << ans << endl;
    }
}



Measured memory_bytes: 6560