#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
struct UnionFind
{
    vector<int> par, sz;
    UnionFind(int n) : par(n), sz(n, 1) {
        for (int i = 0; i < n; ++i) par[i] = i;
    }
    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        sz[y] = 0;
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    int size(int x) {
        return sz[root(x)];
    }
};
vector<int> h;
int beg = -1;
int cnt = 0;
struct Edge{
  int a, b, c;
};
void solve(int n, int m) {
  h.clear();
  h.resize(n);
  set<int> hst;
  for(int i=0;i<n;++i) {
    cin >> h[i];
    hst.insert(h[i]);
  }
  map<int, int> mp;
  int idx = 0;
  for(auto &e: hst) {
    mp[e] = idx;
    idx++;
  }
  vector<Edge> edges;
  for(int i=0;i<m;++i) {
    int a, b, c; cin >> a >> b >> c;
    a--; b--;
    edges.push_back({a, b, c});
  }
  cnt++;
  sort(edges.begin(), edges.end(), [](Edge &e1, Edge &e2) {
    int mi1 = min(h[e1.a], h[e1.b]), mi2 = min(h[e2.a], h[e2.b]);
    if(mi1 == mi2) {
      return e1.c < e2.c;
    } else {
      return mi1 > mi2;
    }
  });
  beg = -1;
  UnionFind uf(n);
  vector<bool> sel(n, false);
  bool ok = false;
  for(int i=0;i<m;++i) {
    uf.merge(edges[i].a, edges[i].b);
    sel[edges[i].a] = true;
    sel[edges[i].b] = true;
    int now = min(h[edges[i].a], h[edges[i].b]);
    int now2 = INF;
    if(i != m-1) {
      now2 = min(h[edges[i+1].a], h[edges[i+1].b]);
    }
    if(now == now2) continue;
    set<int> st;
    bool ok2 = true;
    if(i != m-1 && mp[now2] - mp[now] != -1) ok2 = false;
    for(int j=0;j<n;++j) {
      if(!sel[j]) {
        if(h[j] >= now) ok2 = false;
        continue;
      }
      st.insert(uf.root(j));
    }
    if((int)(st.size()) != 1 || !ok2) {
      ok = false;
    } else {
      if(!ok) beg = now;
      ok = true;
    }
  }
  for(int i=0;i<n;++i) {
    if(!sel[i]) {
      ok = false;
      break;
    }
  }
  if(!ok) {
    cout << 0 << endl;
    return;
  }
  sort(edges.begin(), edges.end(), [](Edge &e1, Edge &e2) {
    int mi1 = min(h[e1.a], h[e1.b]), mi2 = min(h[e2.a], h[e2.b]);
    if(mi1 < beg && mi2 < beg) {
      if(mi1 == mi2) {
        return e1.c < e2.c;
      } else {
        return mi1 > mi2;
      }
    } else if(mi1 >= beg && mi2 >= beg) {
      return e1.c < e2.c;
    } else if(mi1 >= beg) {
      return true;
    } else {
      return false;
    }
  });
  UnionFind uf2(n);
  int res = 0;
  for(int i=0;i<m;++i) {
    if(!uf2.issame(edges[i].a, edges[i].b)) {
      uf2.merge(edges[i].a, edges[i].b);
      res += edges[i].c;
    }
  }
  cout << res << endl;
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    int n, m; cin >> n >> m;
    if(n == 0) break;
    solve(n, m);
  }
}



Measured memory_bytes: 3556