#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

/***** Union-Find *****/

const int MAX_N = 10000;
int Par[MAX_N];
int Rank[MAX_N];

void union_find_init(int n){
	for(int i=0; i<n; ++i){
		Par[i] = i;
		Rank[i] = 0;
	}
}

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;
	} else{
		Par[y] = x;
		if(Rank[x] == Rank[y]){
			++Rank[x];
		}
	}
}

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

/***** /Union-Find *****/

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

vector<edge> es;
int V, E;

// time, vs
vector<pair<int, VI>> falls;

VB falled;
VB linked;

// ????????????????????¶????????????????????£????????????
bool all_unite() {
    int v = -1;
    REP(i, 0, V) {
        if (falled[i]) continue;

        if (v == -1) {
            v = i;
        } else {
            if (!same(v, i)) return false;
        }
    }
    return true;
}

// ??¨??????????????£????????£????????¨?????????
void link_new_edge() {
    rep(i, E) {
        edge &e = es[i];
        if (linked[i]) continue;
        if (falled[e.from] || falled[e.to]) continue;
        if (!same(e.from, e.to)){
            unite(e.from, e.to);
            linked[i] = true;
        }
    }
}

// ??¨??????????????????
void link_edge() {
    rep(i, E) {
        edge &e = es[i];
        if (falled[e.from] || falled[e.to]) continue;
        unite(e.from, e.to);
    }
}

// ????¨??????????????±???????
int sum_cost() {
    int cost = 0;
    rep(i, E) {
        if (linked[i]) {
            cost += es[i].cost;
        }
    }
    return cost;
}

int solve() {
    falled = VB(V);

    sort(ALL(es));
    sort(ALL(falls));

    // ?????????????°???????????±???????
    int end_i = 0;
    for (; end_i < (int)falls.size(); end_i++) {
        // ?????????
        for (auto v : falls[end_i].snd) { falled[v] = true; }

        union_find_init(V);
        link_edge();

        if (!all_unite()) { break; }
    }

    // ??¨?????¨?????§?????????
    if (end_i == 0) return 0;

    // ????????????????????????
    linked = VB(E);
    union_find_init(V);
    for (int i = min(end_i, (int)falls.size() - 1); i >= 0; i--) {
        // ??????
        for (auto v : falls[i].snd) { falled[v] = false; }

        link_new_edge();
    }
    
    return sum_cost();
}

int main(void) {
    while (cin >> V >> E, V) {
        // ??\???
        falls.clear();
        falls.EB(0, VI());
        rep(i, V) {
            int h;
            cin >> h;

            auto it = find_if(ALL(falls), [h](const pair<int, VI> &x) {
                    return h == x.fst; });
            if (it == falls.end()) {
                falls.EB(h, VI({i}));
            } else {
                falls[it - falls.begin()].snd.PB(i);
            }
        }

        es.clear(); es.resize(E);
        rep(i, E) {
            int a, b, c;
            cin >> a >> b >> c;
            --a; --b;
            es[i].to = a;
            es[i].from = b;
            es[i].cost = c;
        }
        // ??\??????????????§
        cout << solve() << endl;
    }

	return 0;
}

Measured memory_bytes: 1272