#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

class UnionFindTree
{
    int n;
    vector<int> parent; // 親ノード
    vector<int> rank;   // 木の高さの上限
    vector<int> num;    // グループの要素数
    int find(int x){
        if(parent[x] == x)
            return x;
        return parent[x] = find(parent[x]);
    }
public:
    UnionFindTree(int n0){ // コンストラクタ
        n = n0;
        parent.resize(n);
        for(int i=0; i<n; ++i)
            parent[i] = i;
        rank.assign(n, 0);
        num.assign(n, 1);
    }
    void unite(int x, int y){ // xとyのグループを併合
        if((x = find(x)) != (y = find(y))){
            if(rank[x] < rank[y]){
                parent[x] = y;
                num[y] += num[x];
            }else{
                parent[y] = x;
                if(rank[x] == rank[y])
                    ++ rank[x];
                num[x] += num[y];
            }
            -- n;
        }
    }
    bool same(int x, int y){ // xとyのグループが同じかを調べる
        return find(x) == find(y);
    }
    int getNum(){ // グループの数を返す
        return n;
    }
    int getNum(int x){ // xのグループの要素数を返す
        return num[find(x)];
    }
};

class Bridge
{
public:
    int a, b, cost;
    bool operator<(const Bridge& x) const{
        return cost < x.cost;
    }
};

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<pair<int, int> > sink(n);
        for(int i=0; i<n; ++i){
            cin >> sink[i].first;
            sink[i].second = i;
        }
        sort(sink.rbegin(), sink.rend());

        vector<Bridge> bridge(m);
        for(int i=0; i<m; ++i){
            cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
            -- bridge[i].a;
            -- bridge[i].b;
        }
        sort(bridge.begin(), bridge.end());

        int k = n;
        vector<bool> isActive(n, true);
        while(k >= 0){
            UnionFindTree uft(n);
            for(int i=0; i<m; ++i){
                if(isActive[bridge[i].a] && isActive[bridge[i].b])
                    uft.unite(bridge[i].a, bridge[i].b);
            }
            if(uft.getNum() != n - k + 1)
                break;

            do{
                -- k;
                isActive[sink[k].second] = false;
            }while(k > 0 && sink[k].first == sink[k-1].first);
        }
        
        UnionFindTree uft(n);
        int ret = 0;
        while(k < n){
            do{
                isActive[sink[k].second] = true;
                ++ k;
            }while(k < n && sink[k].first == sink[k-1].first);

            for(int i=0; i<m; ++i){
                if(isActive[bridge[i].a] && isActive[bridge[i].b] && !uft.same(bridge[i].a, bridge[i].b)){
                    ret += bridge[i].cost;
                    uft.unite(bridge[i].a, bridge[i].b);
                }
            }
        }
        cout << ret << endl;
    }
}

Measured memory_bytes: 1460