#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

typedef pair<ll, l_l> lll;
int N, M;
int h[200];
int a[50000], b[50000], c[50000];
map<int, vector<lll>> mp;
map<int, int> num;

void solve() {
    mp.clear();
    num.clear();
    int Last = 0;
    for(int i = 0; i < N; i++) {
        cin >> h[i];
        num[h[i]]++;
        mp[-h[i]].push_back({-1, {i, i}});
        if(h[i] > h[Last]) Last = i;
    }
    for(int i = 0; i < M; i++) {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
        int day = min(h[a[i]], h[b[i]]);
        mp[-day].push_back({c[i], {a[i], b[i]}});
    }
    UnionFind uni(N);
    ll ans = 0;
    ll nownum = 0;
    int DeadLine = 1e9;
    for(auto tmp : mp) {
        //cerr << "----day: " << -tmp.first << "------" << endl;
        auto v = tmp.second;
        //nownum += num[-tmp.first];
        sort(v.begin(), v.end());
        for(auto tmp2 : v) {
            if(tmp2.first == -1) {
                nownum++;
                continue;
            }
            if(uni.merge(tmp2.second.first, tmp2.second.second)) {
                ans += tmp2.first;
            }
        }
        //cerr << uni.size(Last) << " " << nownum << endl;
        if(nownum != uni.size(Last)) DeadLine = -tmp.first;
    }
    //cerr << DeadLine << endl;
    ans = 0;
    uni = UnionFind(N);
    vector<lll> Query;
    for(auto tmp : mp) {
        auto v = tmp.second;
        nownum += num[-tmp.first];
        sort(v.begin(), v.end());
        for(auto tmp2 : v) {
            if(tmp2.first == -1) {
                continue;
            }
            Query.push_back(tmp2);
            continue;
            /*
            if(uni.merge(tmp2.second.first, tmp2.second.second)) {
                ans += tmp2.first;
            }
            */
        }
        if(-tmp.first >= DeadLine) continue;
        sort(Query.begin(), Query.end());
        for(auto tmp:Query) {
            if(uni.merge(tmp.second.first, tmp.second.second)) {
                ans += tmp.first;
            }
        }
        //cerr << uni.size(Last) << " " << nownum << endl;
    }
    if(uni.size(0) != N) ans = 0;
    cout << ans << endl;
}

int main() {
    while(true) {
        cin >> N >> M;
        if(N == 0 and M == 0) break;
        solve();
    }
    return 0;
}


Measured memory_bytes: 5868