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

using pii = pair<int, int>;

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

using edges = vector<edge>;
using graph = vector<edges>;

class union_find {
public:
    union_find(int n)
        : par_(n, -1)
    {}
    void init(int n) {
        par_.assign(n, -1);
    }

    int root(int x) {
        return par_[x] < 0 ? x : par_[x] = root(par_[x]);
    }

    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if(x == y) {
            return false;
        } else {
            if(par_[x] < par_[y]) {
                par_[x] += par_[y];
                par_[y] = x;
            } else {
                par_[y] += par_[x];
                par_[x] = y;
            }
            return true;
        }
    }

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

    int size(int x) {
        return -par_[root(x)];
    }

private:
    std::vector<int> par_;
};

constexpr int INF = 1e9;

int main() {
    int N, M;
    while(cin >> N >> M, N) {
        vector<pii> h(N);
        for(int i=0; i<N; ++i) {
            cin >> h[i].first;
            h[i].second = i;
        }
        sort(h.begin(), h.end());
        edges es(M);
        for(int i=0; i<M; ++i) {
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            es[i] = edge{a, b, c};
        }
        sort(es.begin(), es.end());
        {
            union_find uf(N);
            for(auto& e : es) {
                uf.unite(e.from, e.to);
            }
            if(uf.size(0) != N) {
                cout << 0 << endl;
                continue;
            }
        }
        int i = 0;
        int cnt = N;
        vector<bool> sink(N);
        vector<vector<int>> sinkv;
        while(i < N) {
            vector<int> v = {h[i].second};
            sink[h[i].second] = true;
            int j = i+1;
            for(; j<N && h[i].first == h[j].first; ++j) {
                v.push_back(h[j].second);
                sink[h[j].second] = true;
            }
            i = j;
            union_find uf(N);
            int sz = 1;
            for(auto& e : es) {
                if(!sink[e.from] && !sink[e.to]) {
                    uf.unite(e.from, e.to);
                    sz = max(sz, uf.size(e.from));
                }
            }
            if(sz != cnt - v.size()) {
                for(auto x : v) {
                    sink[x] = false;
                }
                break;
            }
            cnt -= v.size();
            sinkv.push_back(move(v));
        }

        int res = 0;
        union_find uf(N);
        for(auto& e : es) {
            if(!sink[e.from] && !sink[e.to] && !uf.same(e.from, e.to)) {
                res += e.cost;
                uf.unite(e.from, e.to);
            }
        }
        reverse(sinkv.begin(), sinkv.end());
        for(auto& vs : sinkv) {
            for(auto x : vs) {
                sink[x] = false;
            }
            for(auto& e : es) {
                if(!sink[e.from] && !sink[e.to] && !uf.same(e.from, e.to)) {
                    res += e.cost;
                    uf.unite(e.from, e.to);
                }
            }
        }
        cout << res << endl;
    }
}


Measured memory_bytes: 3400