#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )
#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

class UnionFind
{
private:
	vector<int> parent;
	vector<int> rank;

public:
	UnionFind( int n ) : parent( n ), rank( n, 0 )
	{
		for ( int i = 0; i < n; i++ )
		{
			parent[i] = i;
		}

		return;
	}

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

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

	void unite( int x, int y )
	{
		x = find( x );
		y = find( y );

		if ( x == y )
		{
			return;
		}

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

			if ( rank[x] == rank[y] )
			{
				rank[x]++;
			}
		}

		return;
	}
};
// UnionFind( N )
// find( x )
// same( x, y )
// unite( x, y )

struct Edge
{
	int u, v, c;
	
	Edge( int u, int v, int c ) : u( u ), v( v ), c( c )
	{
		return;
	}

	bool operator < ( const Edge &a ) const
	{
		return c < a.c;
	}
};

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

	for ( int n, m; cin >> n >> m, n | m; )
	{
		VI hs( n );
		FOR( h, hs )
		{
			cin >> h;
		}
		
		VI times = hs;
		sort( ALL( times ) );
		times.erase( unique( ALL( times ) ), times.end() );
		reverse( ALL( times ) );

		vector<Edge> G;
		REP( i, 0, m )
		{
			int a, b, c;
			cin >> a >> b >> c;
			G.PB( Edge( a - 1, b - 1, c ) );
		}
		sort( ALL( G ) );

		UnionFind uf( n );
		vector<bool> exists( n, false );
		int res = 0;
		REP( i, 0, times.size() )
		{
			REP( j, 0, n )
			{
				if ( times[i] <= hs[j] )
				{
					exists[j] = true;
				}
			}

			REP( i, 0, m )
			{
				if ( !exists[ G[i].u ] || !exists[ G[i].v ] || uf.same( G[i].u, G[i].v ) )
				{
					continue;
				}
				uf.unite( G[i].u, G[i].v );
				res += G[i].c;
			}

			const int pos = find( ALL( exists ), true ) - exists.begin();
			bool connected = true;
			REP( i, 0, n )
			{
				if ( !exists[i] )
				{
					continue;
				}
				connected &= uf.same( pos, i );
			}

			if ( !connected )
			{
				res = 0;
				uf = UnionFind( n );
			}
		}

		cout << res << endl;
	}

	return 0;
}

Measured memory_bytes: 1712