#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;


struct UnionFind{
    
    vector<int> M_par;
    vector<int> M_rank;
    vector<int> M_size;

private:
    void init(int n = 1){
        M_par.resize(n);
        M_rank.resize(n);
        M_size.resize(n);
        for(int i = 0; i < n; i++){
            M_par[i] = i;
            M_rank[i] = 0;
            M_size[i] = 1;
        }
    }

public:

    UnionFind(size_t n = 1){
        init(n);
    }

    size_t find(size_t x){
        if(M_par[x] == x) return x;
        size_t r = find(M_par[x]);
        return M_par[x] = r;
    }
    
    bool same(size_t x, size_t y){
        return find(x) == find(y);
    }

    bool unite(size_t x, size_t y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        if(M_rank[x] < M_rank[y]) swap(x, y);
        if(M_rank[x] == M_rank[y]) M_rank[x]++;
        M_par[y] = x;
        M_size[x] = M_size[x] + M_size[y];
        return true;
    }

    size_t size(size_t x){
        return M_size[find(x)];
    }

};

struct edge{
    
    int x, y, c;
    edge(int x, int y, int c):x(x), y(y), c(c){}
    edge(){}
    bool operator<(const edge &a) const{
        return c < a.c;
    }
};


signed main(){

    while(1){
        int n, m; cin >> n >> m;
        if(n == 0) break;

        vector<pair<int, int>> time(n);
        for(int i = 0; i < n; i++){
            int h; cin >> h;
            time[i] = {h, i};
        }

        sort(time.rbegin(), time.rend());
        
        vector<edge> bridge(m);
        for(int i = 0; i < m; i++){
            int a, b, c; cin >> a >> b >> c;
            a--; b--;
            bridge[i] = edge(a, b, c);
        }
        sort(bridge.begin(), bridge.end());

        UnionFind uf(n);
        vector<bool> usable(n, false);
        int ans = 0;

        for(int i = 0; i < n; i++){
            usable[time[i].second] = true;
            while(i + 1 < n && time[i].first == time[i + 1].first){
                i++;
                usable[time[i].second] = true;
            }
            for(int j = 0; j < m; j++){
                edge e = bridge[j];
                if(usable[e.x] && usable[e.y] && uf.find(e.x) != uf.find(e.y)){
                    ans += e.c;
                    uf.unite(e.x, e.y);
                }
            }
            int root = -1;
            for(int j = 0; j < n; j++){
                if(usable[j]){
                    if(root != -1 && root != uf.find(j)){
                        ans = 0;
                        uf = UnionFind(n);
                    }
                    if(root == -1) root = uf.find(j);
                }
            }
        }

        cout << ans << endl;

    }

    return 0;
}



Measured memory_bytes: 3564