#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

struct union_find{
  int N;
  vector<int> parent;
  vector<int> siz;

  union_find(int N): parent(N), siz(N, 1){
    for (int i  =0;i < N;i++) parent[i] = i;
  }

  int root(int x){
    if(parent[x] == x) return x;
    else return parent[x] = root(parent[x]);
  }

  void merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return;
    if(siz[x] < siz[y]) swap(x, y);
    parent[y] = x;
    siz[x] += siz[y];
    siz[y] = 0;
  }

  bool are_same(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};

ll n,m;
vl h;
vl sink;
map<ll,vl> hour_of_node;
vl visit;
graph g;
graph sg;
ll ans;

void dfs(ll x){
  visit[x] = 1;
  for(auto y : g[x]){
    if(visit[y.first] == 0 && sink[y.first] == 0) dfs(y.first);
  }
}

bool valid(){
  for(ll i = 1;i <= n;i++){
    if(sink[i] == visit[i]){
      // cerr << "!" << i << endl;
      return false;
    }
  }
  return true;
}

int main(){
  while (cin >> n >> m) {
    if(n == 0 && m == 0) return 0;
    h = vl(n+1);
    for(ll i = 1;i <= n;i++) cin >> h[i];
    g = graph();
    for(ll i = 0;i < m;i++){
      ll a,b,c;
      cin >> a >> b >> c;
      g[a][b] = g[b][a] = c;
    }

    sink = vl(n+1);
    hour_of_node = map<ll,vl>();
    for(ll i = 1;i <= n;i++){
      hour_of_node[h[i]].push_back(i);
    }
    visit = vl(n+1);
    dfs(1);
    if(!valid()){
      cout << 0 << endl;
      continue;
    }
    auto hour_iter = hour_of_node.begin();
    for(;hour_iter != hour_of_node.end();hour_iter++){
      // cerr << hour_iter->first << endl;
      for(ll x : hour_iter->second){
        sink[x] = 1;
      }
      visit = vl(n+1);
      for(ll i = 1;i <= n;i++){
        if(sink[i] == 0){
          dfs(i);
          // cerr << i << endl;
          break;
        }
      }
      if(!valid()){
        for(ll x : hour_iter-> second){
          sink[x] = 0;
        }
        break;
      }
    }
    // if(hour_iter == hour_of_node.end()) // cerr << "!!!!!!" << endl;
    vector<vl> edges;
    for(ll i = 1;i <= n;i++){
      if(sink[i] == 0){
        for(auto y : g[i]){
          if(sink[y.first] == 0){
            edges.push_back(vl({y.second, i, y.first}));
          }
        }
      }
    }
    sort(edges.begin(),edges.end());
    union_find uf(n+1);
    ans = 0;
    for(ll i = 0;i < edges.size();i++){
      ll a = edges[i][1];
      ll b = edges[i][2];
      ll c = edges[i][0];
      if(!uf.are_same(a,b)){
        uf.merge(a,b);
        ans += c;
      }
    }
    hour_iter--;
    for(;;hour_iter--){
      edges = vector<vl>();
      for(ll x : hour_iter-> second){
        sink[x] = 0;
      }
      for(ll x : hour_iter->second){
        for(auto y : g[x]){
          if(sink[y.first] == 0){
            edges.push_back(vl({y.second, x, y.first}));
          }
        }
      }
      sort(edges.begin(),edges.end());

      for(ll i = 0;i < edges.size();i++){
        ll a = edges[i][1];
        ll b = edges[i][2];
        ll c = edges[i][0];
        if(!uf.are_same(a,b)){
          uf.merge(a,b);
          ans += c;
        }
      }
      if(hour_iter == hour_of_node.begin()) break;
    }
    cout << ans << endl;



  }

}



Measured memory_bytes: 7524