#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

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

class UnionFind {
public:
  const static int MAX_N = 100010;
  int par[MAX_N];
  int s[MAX_N];
  UnionFind() { init(); }
  UnionFind(int n) { init(n); }
  void init() { for(int i=0; i<MAX_N; ++i) par[i] = i, s[i] = 1; }
  void init(int n) { for(int i=0; i<n; ++i) par[i] = i, s[i] = 1; }
  int find(int x) {
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(s[x] < s[y]) par[x] = y, s[y] = s[x] + s[y];
    else par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y);}
};
UnionFind uf;

vector<PII> g[205];
VI t[205];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true) {
    int n, m;
    cin >> n >> m;
    if(!n) break;
    REP(i, 205) g[i].clear(), t[i].clear();
    uf.init(n);
    VI h(n), hh(n);
    REP(i, n) cin >> h[i], hh[i] = h[i];
    VI a(m), b(m), c(m);
    REP(i, m) {
      cin >> a[i] >> b[i] >> c[i]; a[i]--, b[i]--;
      g[a[i]].PB({b[i], c[i]});
      g[b[i]].PB({a[i], c[i]});
    }

    VI ver(n);
    sort(ALL(hh));
    hh.erase(unique(ALL(hh)), hh.end());
    REP(i, n) h[i] = lower_bound(ALL(hh), h[i]) - hh.begin();
    REP(i, n) {
      t[h[i]].PB(i);
      ver[i] = h[i];
    }

    // cout << ver << endl;

    int last = hh.size()-1;
    VI exist(n, 1);
    VI used(n, 0);

    function<void(int)> dfs = [&](int x) {
      used[x] = true;
      for(auto i: g[x]) if(!used[i.first]) dfs(i.first);
    };

    REP(i, hh.size()) {
      // cout << i << endl;
      // 時刻iで沈む頂点を除く
      for(int j: t[i]) exist[j] = 0;
      REP(j, n) used[j] = !exist[j];

      // dfsで連結性を確かめる
      int cnt = 0;
      REP(j, n) {
        // cout << "j:" << j << endl;
        if(used[j]) continue;
        dfs(j);
        cnt++;
      }
      if(cnt >= 2) {
        last = i;
        break;
      }
    }

    // そもそも二つに分裂してる状態
    // dfsで連結性を確かめる
    used.assign(n, 0);
    int cnt = 0;
    REP(j, n) {
      if(used[j]) continue;
      dfs(j);
      cnt++;
    }
    if(cnt >= 2) {
      cout << 0 << endl;
      continue;
    }
    // cout << "a" << endl;

    VVI edge[205];
    REP(i, m) edge[min(ver[a[i]], ver[b[i]])].PB({c[i], a[i], b[i]});
    // REP(i, hh.size()) cout << edge[i] << " " << t[i] << endl;

    // 時刻[last, hh.size()-1]までで沈む頂点に関する辺集合
    VVI vec;
    FOR(i, last, hh.size()) {
      for(auto j: edge[i]) {
        vec.PB(j);
      }
    }
    // cout << vec << endl;
    sort(ALL(vec));
    int ans = 0;
    for(auto j: vec) {
      if(uf.same(j[1], j[2])) continue;
      ans += j[0];
      uf.unite(j[1], j[2]);
    }
    // cout << ans << endl;

    for(int i=last-1; i>=0; --i) {
      // 時間iで沈む頂点に関連する辺集合
      sort(ALL(edge[i]));
      for(auto j: edge[i]) {
        if(uf.same(j[1], j[2])) continue;
        ans += j[0];
        uf.unite(j[1], j[2]);
      }
      // cout << ans << endl;
    }
    cout << ans << endl;
  }

  return 0;
}



Measured memory_bytes: 8288