#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
const int INF = 1<<29;
const double EPS = 1e-8;
const double PI = acos(-1);
typedef long long ll;
typedef pair<int,int> pii;


struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

struct Edge {
  int src, dst,cost;
  Edge(int a, int b, int c) :src(a),dst(b),cost(c) {}
  bool operator<(const Edge &rhs) const {
    return cost < rhs.cost;
  }
};

const int H = 1000001;
vector<int> vers[1000001];
vector<Edge> g[201];
int main() {
  int n,m;
  while(cin>>n>>m, n||m) {
    REP(i,H) vers[i].clear();
    REP(i,n) {
      int h; cin >> h;
      vers[h].push_back(i);
      g[i].clear();
    }
    REP(i,m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;b--;
      g[a].push_back(Edge(a,b,c));
      g[b].push_back(Edge(b,a,c));
    }
    UnionFind uf(n);
    vector<bool> ok(H);
    set<int> st;
    int cnt = 0;
    int lastVer = -1;
    for (int i=H-1; i>=0; --i) {
      FOR(it, vers[i]) {
        int a = *it;
        st.insert(a);
        FOR(e, g[a]) {
          if (st.count(e->dst)) {
            uf.unionSet(e->src, e->dst);
          }
        }
        cnt++;
        lastVer = a;
      }
      if (lastVer != -1) {
        if (uf.size(lastVer) == cnt) {
          ok[i] = 1;
        }
      }
    }
    int last = -1;
    REP(i,H) {
      if (!ok[i]) break;
      last = i;
    }
    // cout << "last = " << last << endl;
    if (last == -1) {
      cout << 0 << endl;
      continue;
    }
    st.clear();
    vector<Edge> es;
    for (int i=H-1; i>=last; --i) {
      FOR(it, vers[i]) {
        int a = *it;
        st.insert(a);
        FOR(e, g[a]) {
          if (st.count(e->dst)) {
            es.push_back(*e);
          }
        }
      }      
    }
    UnionFind kr(n);
    int ans = 0;
    sort(ALL(es));    
    FOR(it, es) {
      if (kr.unionSet(it->src, it->dst)) {
        ans += it->cost;
      }
    }
    // cout << ans << endl;
    for (int i=H-1; i>=0; --i) {
      vector<Edge> es;
      FOR(it, vers[i]) {
        int a = *it;
        st.insert(a);
        FOR(e, g[a]) {
          if (st.count(e->dst)) {
            es.push_back(*e);
          }
        }
      }
      sort(ALL(es));    
      FOR(it, es) {
        if (kr.unionSet(it->src, it->dst)) {
          ans += it->cost;
        }
      }      
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 26948