// #define DEBUGGING
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) begin(V), end(V)
#define ALLR(V) rbegin(V), rend(V)
using ll = std::int64_t;
using ull = std::uint64_t;
using PLL = std::pair<ll, ll>;
using TLL = std::tuple<ll, ll, ll>;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return V<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec
using namespace std;

#ifdef DEBUGGING
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

struct UnionFind {
    V<ll> rank, par;

    UnionFind(ll N) : rank(N, 1), par(N) {
        iota(ALL(par), 0ll);
    }

    ll find(ll x) {
        return par[x] == x ? x : par[x] = find(par[x]);
    }

    bool unit(ll x, ll y) {
        ll px = find(x), py = find(y);
        if (px == py) return false;
        if (rank[px] < rank[py]) swap(px, py);
        rank[px] += (rank[px] == rank[py]);
        par[py] = par[px];
        return true;
    }

    bool is_same(ll x, ll y) {
        return find(x) == find(y);
    }
};

V<ll> make_sh(V<ll> H) {
    sort(ALL(H));
    auto ite = unique(ALL(H));
    H.erase(ite, H.end());
    return H;
}

const ll inf = 5e15;

bool solve() {
    ll N, M;
    cin >> N >> M;
    if (!(N + M)) return false;
    V<ll> H(N);
    V<TLL> edges(M);
    for (auto &&e : H) cin >> e;
    for (auto &&e : edges) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        e = TLL(a, b, c);
    }

    auto SH = make_sh(H);
    SH.insert(SH.begin(), 0);

    ll maxh = -1;
    for (ll bh : SH) {
        ll cnt = 0;
        for (ll h : H) cnt += (bh <= h);
        UnionFind uf(N);
        ll united = 0;
        for (auto &&e : edges) {
            ll a, b;
            tie(a, b, ignore) = e;
            if (min(H[a], H[b]) < bh) continue;
            if (uf.unit(a, b)) united++;
        }
        if (united < cnt - 1) break;
        maxh = bh;
    }

    if (maxh == -1) {
        cout << 0 << endl;
        return true;
    }

    /*
    {
        auto tmp = move(edges);
        edges.clear();
        for (auto &&e : tmp) {
            ll a, b, c;
            tie(a, b, c) = e;
            if (maxh < max(H[a], H[b])) continue;
            edges.emplace_back(a, b, c);
        }
    }
    */

    sort(ALL(edges), [&](const TLL &p, const TLL &q) {
        ll a, b, c, d, e, f;
        tie(a, b, c) = p;
        tie(d, e, f) = q;
        ll hp = var_min(H[a], H[b], maxh);
        ll hq = var_min(H[d], H[e], maxh);
        if (hp != hq) return hp > hq;
        else return c < f;
    });
    
    DEBUG(make_tuple(edges, maxh));
    ll ans = 0;
    UnionFind uf(N);
    for (auto &&e : edges) {
        ll a, b, c;
        tie(a, b, c) = e;
        if (uf.unit(a, b)) ans += c;
    }
    cout << ans << endl;
    return true;
}

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


Measured memory_bytes: 3668