#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}


struct UnionFind{
    vector<int> par;
    int count;
    UnionFind(int n) : par(n, -1), count(0){}
    int Find(int x){return par[x] < 0 ? x : Find(par[x]);}
    int Size(int x){return par[x] < 0 ? -par[x] : Size(par[x]);}
    bool Unite(int x, int y){
        x = Find(x);
        y = Find(y);
        if(x == y)
            return false;
        if(par[x] > par[y])
            swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return ++count;
    }
};


bool solve(){
    int n, m;
    cin >> n >> m;
    if(!n)
        return false;
    vector<int> h(n);
    for(int i = 0; i < n; ++i)
        cin >> h[i];
    vector<int> a(m), b(m), c(m);
    vector<vector<pair<int,int>>> edges(n);
    for(int i = 0; i < m; ++i){
        cin >> a[i] >> b[i] >> c[i];
        --a[i], --b[i];
        edges[a[i]].emplace_back(b[i], c[i]);
        edges[b[i]].emplace_back(a[i], c[i]);
    }
    map<int, vector<int>> ts;
    for(int i = 0; i < n; ++i)
        ts[h[i]].push_back(i);

    bitset<200> bs, fl;
    for(int i = 0; i < n; ++i)
        bs.set(i);

    function<void(int)> f = [&](int x){
        for(auto& ed : edges[x]){
            if(!bs[ed.first] || fl[ed.first])
                continue;
            fl.set(ed.first);
            f(ed.first);
        }
    };

    int border = -1;
    for(auto& elm : ts){
        int idx = -1;
        for(int i = 0; i < n; ++i)
            if(bs[i]){
                idx = i;
                break;
            }
        if(idx == -1){
            border = elm.first;
            break;
        }
        fl.reset();
        fl.set(idx);
        f(idx);
        if(fl != bs){
            border = elm.first;
            break;
        }
        for(auto& x : elm.second)
            bs.reset(x);
    }
    fl.reset();
    vector<pair<int, vector<int>>> v;
    for(auto& elm : ts){
        if(elm.first == border)
            break;
        v.emplace_back(elm);
        for(auto& x : elm.second)
            fl.set(x);
    }

    bs.reset();

    for(int i = 0; i < n; ++i)
        if(!fl[i]){
            bs.set(i);
        }
    reverse(v.begin(), v.end());
    bitset<100000> used;

    UnionFind u(n);
    int ans = 0;

    if(v.empty()){
        cout << 0 << endl;
        return true;
    }
    for(auto& elm : v){
        auto& w = elm.second;
        for(auto& x : w)
            bs.set(x);
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> que;
        for(int i = 0; i < m; ++i){
            if(bs[a[i]] && bs[b[i]] && !used[i])
                que.emplace(c[i], i);
        }
        while(!que.empty()){
            int cost, idx;
            tie(cost, idx) = que.top();
            que.pop();
            if(u.Unite(a[idx], b[idx])){
                used.set(idx);
                ans += cost;
                if(u.Size(a[idx]) == bs.count())
                    break;
            }
        }
    }

    assert(u.count == n - 1);
    assert(bs.count() == n);
    assert(used.count() == n - 1);
    cout << ans << endl;

    return true;
}

signed main(){
    while(solve());
}



Measured memory_bytes: 4140