#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;

#define INF 1e9

class UnionFindTree{
  int m_size;
  vector<int> parent;
  vector<int> rank;
  int find(int a);
public:
  void init(int size);
  void unite(int a, int b);
  bool same(int a, int b);
};

void UnionFindTree::init(int size){
  m_size = size;
  parent.clear();
  rank.clear();
  for(int i = 0; i < size; i++){
    parent.push_back(i);
    rank.push_back(0);
  }
  return;
}

int UnionFindTree::find(int a){
  if(parent[a] != a){ parent[a] = find(parent[a]); }
  return parent[a];
}

void UnionFindTree::unite(int a, int b){
  int x = find(a);
  int y = find(b);
  if(x == y){ return; }

  if(rank[x] < rank[y]){ parent[x] = y; }
  else{
    parent[y] = x;
    if(rank[x] == rank[y]){ rank[x]++; } 
  }
  return;
}

bool UnionFindTree::same(int a, int b){
  return find(a) == find(b);
}

// ???????????????
pair<int, int> kruskal(int v, vector<pair<pair<int, int>, pair<int, int> > > edges, vector<bool> NGedge){
  int cost = 0, unite_count = 0;
  UnionFindTree uf;
  uf.init(v);

  for(int i = 0; i < edges.size(); i++){
    int nowcost = edges[i].first.second;
    int a = edges[i].second.first;
    int b = edges[i].second.second;
    if(NGedge[i] || uf.same(a, b)){ continue; }

    uf.unite(a, b);
    cost += nowcost;
    unite_count++;
  }

  return make_pair(cost, unite_count);
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 &&  m == 0){ break; }

    // ??¶??????????????±?????\???
    vector<int> h(n);
    vector<pair<int, int> > sink;
    for(int i = 0; i < n; i++){
      cin >> h[i];
      sink.push_back(make_pair(h[i], i));
    }
    sort(sink.begin(), sink.end());
    
    // ???????????????????????????????????±?????\???
    int a, b, c;
    vector<pair<pair<int, int>, pair<int, int> > > edges;
    for(int i = 0; i < m; i++){
      cin >> a >> b >> c;
      --a; --b;
      //((-????????\, ?????????), (from, to))
      edges.push_back(make_pair(make_pair(-min(h[a], h[b]), c), make_pair(a, b)));
    }
    sort(edges.begin(), edges.end());

    // ?????????????????¶????´??£????????????£???????´?????????????????¢??????????
    int dobon;
    vector<bool> NGedge(m, false);
    for(dobon = 0; dobon < n - 1; dobon++){
      pair<int, int> result = kruskal(n, edges, NGedge);
      // ???????????????????????¶??????????????£?????????????????£??????break
      if(result.second != n - 1 - dobon){ break; };
      
      // NG??????????????´??°
      for(int j = 0; j < m; j++){
        if(edges[j].second.first == sink[dobon].second || edges[j].second.second == sink[dobon].second){
          NGedge[j] = true;
        }
      }
    }
    // ????????????????????£?????????????????£?????????0?????????
    if(dobon <= 0){ cout << 0 << endl; }
    else{
      int bord = h[sink[dobon - 1].second];
      // ?????????dobon????????§??£????????????????????????????????£?????????
      // dobon????????\????????£???????????????????????????????????????
      for(int i = 0; i < m; i++){
        if(bord < -edges[i].first.first){ edges[i].first.first = -bord; }
      }
      sort(edges.begin(), edges.end());
      
      pair<int, int> result = kruskal(n, edges, vector<bool>(m, false));
      cout << result.first << endl;
    }
  }
  
  return 0;
}

Measured memory_bytes: 1876