#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cassert>
#include<set>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define all(n) (n).begin(),(n).end()
#define MAX 3000
using namespace std;

class UnionFindTree
{
public:
  int par[MAX];

  void init(int N){ rep(i,N)par[i] = i; }  

  int find(int x)
  {
    if(par[x] == x)return x;
    return par[x] = find(par[x]);
  }

  void unit(int x,int y)
  {
    x = find(x);
    y = find(y);
    if(x != y)par[x] = y;
  }

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

};

struct edge
{
  int x,y,cost;
  edge(int x=inf,int y=inf,int cost=inf):x(x),y(y),cost(cost){}
  bool operator < (const edge& a)const
  {
    return cost < a.cost;
  }
};

struct P
{
  int h,identifier;
  P(int h=inf,int identifier=inf):h(h),identifier(identifier){}
  bool operator < (const P& a)const
  {
    if(h != a.h)return h > a.h;
    return identifier < a.identifier;
  }
};

int N,M;
//vector<int> h,ph;
vector<P> h;
vector<int> H;
vector<edge> G;

bool check()
{
  UnionFindTree UTF;
  UTF.init(N);
  rep(i,G.size())
    if(!UTF.same(G[i].x,G[i].y))
      UTF.unit(G[i].x,G[i].y);
  rep(i,N)UTF.find(i);
  rep(i,N)if(UTF.par[i] != UTF.par[0])return false;
  return true;
}

void compute()
{
  
  if(!check())
    {
      cout << 0 << endl;
      return;
    }

  sort(all(h));
  sort(all(G));

  UnionFindTree group;
  group.init(N);
  long long ans = 0;

  rep(i,H.size())
    {
      //int TM = ph[i];
      int TM = h[i].h;
      UnionFindTree UTF;
      UTF.init(N);
      rep(j,G.size())
	{
	  int x = G[j].x;
	  int y = G[j].y;
	  if(H[x] < TM || H[y] < TM)continue;
	  if(!UTF.same(x,y))
	    {
	      UTF.unit(x,y);
	    }
	}

      rep(j,N)if(TM <= H[j])UTF.find(j);
      /*
      set<int> out;
      rep(j,N)if(h[j] >= TM && UTF.par[j] != j)out.insert(UTF.par[j]);
      if(out.size() >= 2)
	{
	  ans = 0;
	  group.init(N);
	  continue;
	}
      */
      
      bool cont = false;
      for(int j=0;j<N;j++)if(TM <= H[j] && !UTF.same(h[0].identifier,j))
			    {
			      ans = 0;
			      group.init(N);
			      cont = true;
			      break;
			    }
      if(cont)continue;

      rep(j,G.size())
	{
	  int x = G[j].x;
	  int y = G[j].y;
	  if(H[x] < TM || H[y] < TM)continue;
	  if(!group.same(x,y))
	    {
	      //cout << TM << " unit(" << x+1 << "," << y+1 << ")" << endl;
	      group.unit(x,y);
	      ans += G[j].cost;
	    }
	}
      //cout << "ans = " << ans << endl;
    }

  cout << ans << endl;

}

int main()
{
  while(cin >> N >> M,N|M)
    {
      h.clear(),G.clear(),H.clear();
      h.resize(N),G.resize(M),H.resize(N);
      //rep(i,N)cin >> h[i],ph[i] = h[i];
      rep(i,N)cin >> h[i].h,h[i].identifier = i,H[i] = h[i].h;
      rep(i,M)cin >> G[i].x >> G[i].y >> G[i].cost,G[i].x--,G[i].y--;

      compute();

    }
  return 0;
}

/*
struct P
{
  int h,identifier;
  P(int h=inf,int identifier=inf):h(h),identifier(identifier){}
  bool operator < (const P& a)const
  {
    if(h != a.h)return h > a.h;
    return identifier < a.identifier;
  }
};

struct edge
{
  int x,y,cost;
  edge(int x=inf,int y=inf,int cost=inf):x(x),y(y),cost(cost){}
  bool operator < (const edge& a)const
  {
    return cost < a.cost;
  }
};

int par[MAX];
int N,M;
vector<int> h,ph;
vector<edge> vec;

int find(int x)
{
  if(par[x] == x)return x;
  return par[x] = find(par[x]);
}

void unit(int x,int y)
{
  x = find(x);
  y = find(y);
  if(x == y)return;
  par[x] = y;
}

int main()
{
  while(cin >> N >> M,N|M)
    {
      
      ph.clear(),h.clear(),vec.clear();
      ph.resize(N),h.resize(N),vec.resize(M);
      rep(i,N)par[i] = i;

      rep(i,N)cin >> h[i],ph[i] = h[i];
	
      rep(i,M)cin >> vec[i].x >> vec[i].y >> vec[i].cost,vec[i].x--,vec[i].y--;
      
      sort(ph.begin(),ph.end(),greater<int>());
      sort(vec.begin(),vec.end());


 
      int ans = 0;
      rep(i,ph.size())
	{
	  int TM = ph[i];
	  int cost = 0;
	  rep(j,vec.size())
	    {
	      int x = vec[j].x;
	      int y = vec[j].y;

	      if(h[x] < TM || h[y] < TM)continue;
	      if(find(x) != find(y))
		{
		  cout << TM << " unit("<<x+1 <<"," <<y+1 << ") : " << vec[j].cost << endl;
		  unit(x,y);
		  cost += vec[j].cost;
		}

	    }
	  cout << "ans : " << ans << endl;
	}

      int ch = find(0);
      bool out = false;
      rep(i,N)if(find(i) != ch)
	{
	  out = true;
	  break;
	}

      cout << (out?0:ans) << endl;
      
    }
  return 0;
}
*/

Measured memory_bytes: 1272