#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <functional>
#include <stack>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <array>
#include <climits>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

#define MAX_E 50000

struct edge {
    int u, v, cost;
};

int n, m, nalive;
vector<edge> path[201];
vector<P> h;
bool is_sinked[201];
vector<int> ok;

class UnionFindTree {
    static const int MAX_V = 100000;
    int par[MAX_V];
    int rank[MAX_V];

   public:
    void init(int n) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

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

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

vector<edge> es;

int kruskal() {
    sort(es.begin(), es.end(), comp);
    UnionFindTree a;
    a.init(n);
    for (int i = 1; i < ok.size(); i++) {
        a.unite(ok[i - 1], ok[i]);
    }
    int res = 0;
    for (int i = 0; i < es.size(); i++) {
        edge e = es[i];
        if (!a.same(e.u, e.v) && !is_sinked[e.v]) {
            a.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

bool is_connected() {
    int start = 0, cnt = 0;
    for (; start < n; start++)
        if (!is_sinked[start]) break;
    queue<int> que;
    vector<bool> visited(n + 1);
    for (int i = 0; i <= n; i++) visited[i] = false;

    visited[start] = true;
    que.push(start);

    while (!que.empty()) {
        int cur = que.front();
        que.pop();
        for (auto next : path[cur]) {
            if (!visited[next.v] && !is_sinked[next.v]) {
                visited[next.v] = true;
                que.push(next.v);
            }
        }
        cnt++;
    }
    return cnt == nalive;
}

int main() {
    while (1) {
        cin >> n >> m;
        int ans = 0;
        ok.clear();
        for (int i=0;i<n;i++) path[i].clear();
        fill(is_sinked, is_sinked + n, false);
        h.clear();
        if (n == 0) break;
        REP(i, n) {
            int tmp;
            cin >> tmp;
            h.push_back({tmp, i});
        }
        REP(i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--;
            b--;
            path[a].push_back({a, b, c});
            path[b].push_back({b, a, c});
        }
        sort(h.begin(), h.end());

        int inum = 0;
        nalive = n;
        while (is_connected()) {
            P tmp = h[inum];
            while (inum < n && tmp.first == h[inum].first) {
                is_sinked[h[inum].second] = true;
                nalive--;
                inum++;
            }
        }

        es.clear();
        inum--;
        if(nalive==n){
            cout << 0 << endl;
            continue;
        }
        if (inum >= 0) {
            P tmp = h[inum];
            while (inum >= 0 && tmp.first == h[inum].first) {
                is_sinked[h[inum].second] = false;
                nalive++;
                inum--;
            }
        }
        REP(i, n) {
            if (!is_sinked[i]) {
                for (auto tmp : path[i]) {
                    es.push_back(tmp);
                }
            }
        }
        ans += kruskal();
        REP(i, n) {
            if (!is_sinked[i]) ok.push_back(i);
        }

        while (inum >= 0) {
            es.clear();
            vector<int> ls;
            if (inum >= 0) {
                P tmp = h[inum];
                while (inum >= 0 && tmp.first == h[inum].first) {
                    is_sinked[h[inum].second] = false;
                    nalive++;
                    ls.push_back(h[inum].second);
                    inum--;
                }
            }
            for (int i = 0; i < ls.size(); i++) {
                if (!is_sinked[ls[i]]) {
                    for (auto tmp : path[ls[i]]) {
                        es.push_back(tmp);
                    }
                }
            }
            ans += kruskal();
            for (int i = 0; i < ls.size(); i++) {
                if (!is_sinked[ls[i]]) 
                    ok.push_back(ls[i]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 4556