#define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}


unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

#define int ll
vector<int> h;
int union_tree[300];
int union_find(int now) {
    if (union_tree[now] == now) return now;
    return union_tree[now] = union_find(union_tree[now]);
}
int union_merge(int a, int b) {
    a = union_find(a);
    b = union_find(b);
    if (a == b) return 0;
    union_tree[a] = b;
    return 1;
}
void solve(){
    while (true) {
        h.clear();
        int n, m;
        cin >> n >> m;
        if (n == 0) return;
        set<int> neko;
        vector<int> gogo;
        REP(i, n) {
            int a;
            cin >> a;
            gogo.push_back(a);
            h.push_back(a);
            union_tree[i] = i;
        }
        sort(ALL(gogo));
        reverse(ALL(gogo));
        vector<tuple<int, int, int, int>> edges;
        REP(i, m) {
            int a, b, c;
            cin >> a >> b >> c;
            a--; b--;
            edges.push_back(make_tuple(min(h[a], h[b]), -c, a, b));
        }
        sort(ALL(edges));
        int itr = edges.size() - 1;
        int die = -1;
        for (auto x = gogo.begin(); x != gogo.end();++x) {
            int tmp = (*x);
            while (itr != -1 && get<0>(edges[itr]) == tmp) {
                union_merge(get<2>(edges[itr]), get<3>(edges[itr]));
                itr--;
            }
            int geko = -1;
            REP(q, n) {
                if (h[q] < tmp) continue;
                if (geko == -1) {
                    geko = union_find(q);
                }
                else {
                    if (geko != union_find(q)) {
                        die = x - gogo.begin();
                    }
                }
            }
        }
        die++;
        if (die == gogo.size()) {
            cout << 0 << endl;
            continue;
        }
        REP(i, n) {
            union_tree[i] = i;
        }
        int ans = 0;
        for (int i = die; i < gogo.size(); ++i) {
            vector<tuple<int, int, int>> nexts;
            REP(q, edges.size()) {
                if (get<0>(edges[q]) >= gogo[i]) {
                    nexts.push_back(make_tuple(-get<1>(edges[q]), get<2>(edges[q]), get<3>(edges[q])));
                }
            }
            sort(ALL(nexts));
            REP(q, nexts.size()) {
                ans += union_merge(get<1>(nexts[q]), get<2>(nexts[q])) * get<0>(nexts[q]);
            }
        }
        cout << ans << endl;
    }
}

#undef int
int main() {
    init();
    solve();
}


Measured memory_bytes: 5000