#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

using T = tuple<int64, int64, int64>;

class UnionFind{
private:
  ::std::vector<int_fast32_t> par;
  size_t n;

public:
  UnionFind(){}
  UnionFind(size_t n):n(n){
    par.resize(n, -1);
  }

  uint_fast32_t find(uint_fast32_t x){
    return par[x] < 0 ? x : par[x] = find(par[x]);
  }

  size_t size(uint_fast32_t x){
    return -par[find(x)];
  }

  bool unite(uint_fast32_t x, uint_fast32_t y){
    x = find(x);
    y = find(y);
    if(x == y) return false;
    if(size(x) < size(y)) std::swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  bool same(uint_fast32_t x, uint_fast32_t y){
    return find(x) == find(y);
  }
};


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

  int64 N, M;
  while (cin >> N >> M && N + M) {
    vector<PLL> h(N);
    REP(i, N) {
      cin >> h[i].fs; h[i].fs *= -1; h[i].sc = i;
    }
    vector<vector<int64>> con(N);
    vector<T> edges(M);
    REP(i, M) {
      int64 a, b, c;
      cin >> a >> b >> c;
      edges[i] = T(c, a-1, b-1);
      con[a-1].push_back(i);
      con[b-1].push_back(i);
    }
    vector<T> usable_edge;

    UnionFind ok(N);
    vector<int> canuse(N, 0);
    sort(all(h));
    UnionFind uf(N);
    int64 idx = 0, res = 0, par = h[0].sc;
    while (idx < h.size()) {
      canuse[h[idx].sc] = 1;
      REP(i, con[h[idx].sc].size()) {
        usable_edge.push_back(edges[con[h[idx].sc][i]]);
        int64 a, b, c;
        tie(c, a, b) = edges[con[h[idx].sc][i]];
        if (canuse[a] && canuse[b]) ok.unite(a, b);
      }
      if (idx < h.size()-1 && h[idx].fs == h[idx+1].fs) {
        idx++;
        continue;
      }
      if (ok.size(par) == idx+1) {
        sort(all(usable_edge));
        REP(i, usable_edge.size()) {
          int64 a, b, c;
          tie(c, a, b) = usable_edge[i];
          if (uf.same(a, b)) {
            continue;
          } else if (canuse[a] && canuse[b]) {
            uf.unite(a, b);
            res += c;
          }
        }
      } else {
        uf = UnionFind(N);
        res = 0;
      }
      idx++;
    }
    cout << res << endl;
  }
}



Measured memory_bytes: 6220