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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)

using Edge = tuple<int,int,int>; // c, a, b
using Graph = vector<vector<int>>;

class UnionFind {
private:
    const int n;
    vector<int> uni;
public:
    UnionFind(int _n) : n(_n), uni(_n, -1) {}
    int root(int x) {
        if (uni[x] < 0) return x;
        return uni[x] = root(uni[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (uni[x] > uni[y]) swap(x, y);
        uni[x] += uni[y];
        uni[y] = x;
        return true;
    }
    int getSize(int x) {
        return -uni[root(x)];
    }
    void print() {
        for (auto x : uni) cout << x << " ";
        cout << endl;
    }
};

bool isConnected(const vector<Edge>& edges, const vector<bool>& alive) {
    int n = alive.size();
    UnionFind uf(n);
    for (auto e : edges) {
        int c, a, b;
        tie(c, a, b) = e;
        if (alive[a] && alive[b]) {
            uf.unite(a, b);
        }
    }
    set<int> groups;
    rep(i, n) {
        if (alive[i]) {
            groups.insert(uf.root(i));
        }
    }
    return groups.size() == 1;
}

int testcase = 0;

bool solve() {
    testcase++;

    int n, m;
    cin >> n >> m;
    if (n == 0) return false;

    map<int, vector<int>> day2islands;
    rep(i, n) {
        int h;
        cin >> h;
        day2islands[h].emplace_back(i);
    }

    vector<vector<int>> queries;
    for (auto p : day2islands) {
        queries.emplace_back(p.second);
    }
    int Q = queries.size();

    vector<Edge> edges;
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        edges.emplace_back(c, a, b);
    }

    sort(edges.begin(), edges.end());

    Graph G(n);
    for (auto e : edges) {
        int c, a, b;
        tie(c, a, b) = e;
        G[min(a, b)].emplace_back(max(a, b));
    }

    /*
    if (testcase == 4) {
        cerr << n << " " << m << endl;
        rep(i, Q) {
            cerr << i << ":";
            for (auto v : queries[i]) {
                cerr << " " << v;
            }
            cerr << endl;
        }
        for (auto e : edges) {
            int c, a, b;
            tie(c, a, b) = e;
            cerr << c << " " << a << " " << b << endl;
        }
        rep(i, n) {
            cerr << i << ":";
            for (auto v : G[i]) {
                cerr << " " << v;
            }
            cerr << endl;
        }
    }
    */

    int id_first_disconnected = Q;
    vector<bool> alive(n, true);
    rep(i, Q) {
        if (!isConnected(edges, alive)) {
            id_first_disconnected = i;
            break;
        }
        for (auto v : queries[i]) {
            alive[v] = false;
        }
    }

    id_first_disconnected--;
    // cerr << "id_first_disconnected = " << id_first_disconnected << endl;

    alive.clear();
    alive.assign(n, true);
    rep(i, id_first_disconnected) {
        for (auto v : queries[i]) {
            alive[v] = false;
        }
    }

    UnionFind uf(n);
    vector<Edge> adopted_edges;
    for (int i = id_first_disconnected - 1; i >= -1; i--) {
        for (auto e : edges) {
            int c, a, b;
            tie(c, a, b) = e;
            if (alive[a] && alive[b] && !uf.same(a, b)) {
                adopted_edges.push_back(e);
                uf.unite(a, b);
            }
        }
        if (i == -1) break;
        for (auto v : queries[i]) {
            alive[v] = true;
        }
    }

    int sm = 0;
    for (auto e : adopted_edges) {
        int c, a, b;
        tie(c, a, b) = e;
        sm += c;
    }

    cout << sm << endl;

    return true;
}

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

Measured memory_bytes: 3552