#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
using V = complex<ld>;
using L = pair<V, V>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;
constexpr ld PI = static_cast<ld>(3.1415926535898);
constexpr ld EPS = static_cast<ld>(1e-12);

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;

struct Edge {
    int from;
    int to;
    ll cost;
    bool operator<(const Edge& e) const
    {
        return cost < e.cost;
    }
};

class DisjointSets
{
public:
    DisjointSets(const int v)
    {
        m_parent.resize(v);
        m_rank.resize(v);
        m_size.resize(v);
        for (int i = 0; i < v; i++) {
            m_parent[i] = i;
            m_rank[i] = 0;
            m_size[i] = 1;
        }
    }

    bool same(const int a, const int b)
    {
        return find(a) == find(b);
    }


    int find(const int a)
    {
        if (m_parent[a] == a) {
            return a;
        } else {
            return m_parent[a] = find(m_parent[a]);
        }
    }

    void unite(const int a_, const int b_)
    {
        const int a = find(a_);
        const int b = find(b_);
        if (a == b) {
            return;
        }
        if (m_rank[a] > m_rank[b]) {
            m_parent[b] = a;
            m_size[a] += m_size[b];
        } else {
            m_parent[a] = b;
            m_size[b] += m_size[a];
        }
        if (m_rank[a] == m_rank[b]) {
            m_rank[b]++;
        }
    }

    int getSize(const int a)
    {
        return m_size[m_parent[a]];
    }

private:
    vector<int> m_parent;
    vector<int> m_rank;
    vector<int> m_size;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (true) {
        int N, M;
        cin >> N >> M;
        if (N == 0 and M == 0) {
            break;
        }
        vector<ll> h(N);
        vector<ll> height(N);
        for (int i = 0; i < N; i++) {
            cin >> h[i];
            height[i] = h[i];
        }
        sort(height.begin(), height.end(), greater<ll>{});
        height.erase(unique(height.begin(), height.end()), height.end());
        vector<int> num(height.size(), 0);
        for (int i = 0; i < height.size(); i++) {
            for (int j = 0; j < N; j++) {
                if (h[j] >= height[i]) {
                    num[i]++;
                }
            }
        }

        vector<Edge> edge;
        for (int i = 0; i < M; i++) {
            int a, b;
            ll c;
            cin >> a >> b >> c;
            a--, b--;
            edge.push_back(Edge{a, b, c});
        }
        sort(edge.begin(), edge.end());

        DisjointSets uf(N);
        ll sum = 0;
        vector<bool> used(M, false);
        int e = 0;
        for (int j = 0; j < height.size(); j++) {
            const ll he = height[j];
            for (int i = 0; i < M; i++) {
                const int from = edge[i].from;
                const int to = edge[i].to;
                const ll cost = edge[i].cost;
                if (h[from] >= he and h[to] >= he and (not uf.same(from, to)) and (not used[i])) {
                    e++;
                    uf.unite(from, to);
                    sum += cost;
                    used[i] = true;
                }
            }
            if (e < num[j] - 1) {
                e = 0;
                sum = 0;
                uf = DisjointSets{N};
                fill(used.begin(), used.end(), false);
                sum = 0;
            }
        }
        cout << sum << endl;
    }

    return 0;
}

Measured memory_bytes: 3700