#include "bits/stdc++.h"

using namespace std;

struct Edge
{
    int from, to, cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;

// 逆順にしているので注意
bool operator<(const Edge &e, const Edge &f)
{
    return e.cost > f.cost;
}

class UnionFind
{
    std::vector<int> data;

public:
    UnionFind(int size) : data(size, -1) {}
    bool unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x != y)
        {
            if (data[y] < data[x])
                std::swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    bool find(int x, int y)
    {
        return root(x) == root(y);
    }
    int root(int x)
    {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x)
    {
        return -data[root(x)];
    }
};

template <typename T>
void coordinateCompression(std::vector<T> &a)
{
    int N = a.size();
    std::vector<T> xs = a;
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    for (int i = 0; i < N; i++)
    {
        a[i] = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
    }
}

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

    int n, m;
    while (cin >> n >> m, n || m)
    {
        set<int> st = {0};
        vector<int> h(n + 1, 0);
        for (int i = 0; i < n; i++)
        {
            cin >> h[i];
        }
        coordinateCompression(h);
        vector<vector<int>> hs(n + 1);
        for (int i = 0; i < n; i++)
        {
            st.insert(h[i]);
            hs[h[i]].push_back(i);
        }
        Edges es;
        for (int i = 0; i < m; i++)
        {
            int a, b, c;
            cin >> a >> b >> c;
            a--;
            b--;
            es.emplace_back(a, b, c);
        }
        sort(es.rbegin(), es.rend());
        int last = -1, cnt = n;
        vector<int> sink(n, 0);
        for (auto h : st)
        {
            for (auto v : hs[h])
            {
                sink[v] = 1;
                cnt--;
            }
            if (cnt <= 1)
                break;
            UnionFind uf(n);
            int v = -1;
            for (const auto &e : es)
            {
                int a = e.from, b = e.to;
                if (!sink[a] && !sink[b])
                {
                    uf.unite(a, b);
                    v = a;
                }
            }
            // assert(v != -1);
            // cout << h << " " << v << " " << uf.size(v) << " " << cnt << endl;
            if (uf.size(v) == cnt)
                last = h;
            else
                break;
        }
        if (last == -1)
        {
            cout << 0 << endl;
            continue;
        }
        int ret = 0;
        UnionFind uf(n);
        for (int i = 0; i < n; i++)
        {
            sink[i] = 0;
        }
        for (auto h : st)
        {
            if (h <= last)
            {
                for (auto v : hs[h])
                {
                    sink[v] = 1;
                }
                continue;
            }
        }
        for (auto e : es)
        {
            int a = e.from, b = e.to, c = e.cost;
            if (sink[a] || sink[b])
                continue;
            if (uf.find(a, b))
                continue;
            uf.unite(a, b);
            ret += c;
        }
        for (int i = last; i >= 1; i--)
        {
            for (auto v : hs[i])
                sink[v] = 0;
            for (auto e : es)
            {
                int a = e.from, b = e.to, c = e.cost;
                if (sink[a] || sink[b])
                    continue;
                if (uf.find(a, b))
                    continue;
                uf.unite(a, b);
                ret += c;
            }
        }
        cout << ret << endl;
    }
}



Measured memory_bytes: 3516