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

#define all(x)  x.begin(), x.end()
#define mp      make_pair
#define pii     pair<int, int>
#define pll     pair<long long, long long>
#define ll      long long

struct edge {
        int a, b, cost;
        bool operator < (const edge &r) const { return cost > r.cost; }
};

struct UnionFind {
        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]);
        }
        UnionFind(int n_) {
                n = n_;
                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) {
                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) { return find(x) == find(y); }
        int get() { return n; }
        int get(int x) { return num[find(x)]; }
};
vector<edge> g[202];
int used[202];

void visit(int v, int t, const vector<int> &h) {
        used[v] = true;
        for (auto e : g[v]) if (!used[e.b]) {
                if (h[e.b] < t) continue;
                visit(e.b, t, h);
        }
}

int main() {
        int n, m;
        while (scanf("%d%d", &n, &m) && n) {
                for (int i = 0; i < n; i ++) g[i].clear();
                vector<int> h(n);
                for (int i = 0; i < n; i ++) scanf("%d", &h[i]);
                vector<int> hh = h;
                sort(all(hh));
                map<int, int> zip;
                int cnt = 1;
                for (int i = 0; i < n; i ++) zip[hh[i]] = cnt ++;
                for (int i = 0; i < n; i ++) h[i] = zip[h[i]];

                //for (int i = 0; i < n; i ++) cerr << "h = " << h[i] << endl;
                //priority_queue<int> es;
                for (int i = 0; i < m; i ++) {
                        int a, b, c;
                        scanf("%d%d%d", &a, &b, &c);
                        a --, b --;
                        //es.push_back((edge) { a, b, c} );
                        g[a].push_back({a, b, c});
                        g[b].push_back({b, a, c});
                }
                cnt --;
                //cerr << cnt << endl;
                int end_t = cnt + 1;
                //auto get_edge = [&] (int i) {
                //}
                for (int t = 1; t <= cnt; t ++) {
                        memset(used, 0, sizeof used);
                        for (int i = 0; i < n; i ++) {
                                if (h[i] >= t) {
                                        visit(i, t, h);
                                        break;
                                }
                        }
                        //for (int i = 0; i < n; i ++) cerr << used[i] << endl;
                        for (int i = 0; i < n; i ++) {
                                if (!used[i] && h[i] >= t) {
                                        end_t = t;
                                        goto end;
                                }
                        }
                }
                end:;
                long long ans = 0;
                end_t --;
                //cerr << "end_t = " << end_t << endl;
                UnionFind uf(n);
                priority_queue<edge> pq;
                for (int i = 0; i < n; i ++) {
                        if (end_t < h[i]) {
                                for (auto e : g[i]) {
                                        if (end_t < h[e.b]) {
                                                pq.push(e);
                                        }
                                }
                        }
                }
                for (int t = end_t; t >= 1; t --) {
                        for (int i = 0; i < n; i ++) {
                                if (t == h[i]) {
                                        for (auto e : g[i]) {
                                                if (t <= h[e.b]) {
                                                        pq.push(e);
                                                }
                                        }
                                }
                        }
                        while (!pq.empty()) {
                                edge e = pq.top(); pq.pop();
                                if (!uf.same(e.a, e.b)) {
                                        uf.unite(e.a, e.b);
                                        //cerr << "e = " << e.a << ' ' << e.b << ' ' << e.cost << endl;
                                        ans += e.cost;
                                }
                        }
                }
                cout << ans << endl;
        }
        return 0;
}

Measured memory_bytes: 4956