#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-9;
const double PI  = acos(-1.0);


const int MAX_V=200;
const ll INF=LONG_MAX;

ll cost[MAX_V][MAX_V];
ll mincost[MAX_V];
bool used[MAX_V];

void init(int V){
    V++;
    REP(i,MAX_V){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
}
ll prim(int V,bool do_init=true){
    if(do_init){
        init(V);
    }
    V++;
    ll res=0;
    while(true){
        int v=-1;
        REP(u,V){
            if(!used[u]&&(v==-1||mincost[u]<mincost[v])&&mincost[u]<INF)v=u;
        }
        if(v==-1)break;
        used[v]=true;
        res+=mincost[v];
        REP(u,MAX_V){
            mincost[u]=min(mincost[u],cost[v][u]);
        }
    }
    return res;
}

int main()
{
    int n,m;
    int cases=0;
    while(cin>>n>>m,n|m){
        cases++;
        if(cases==72){
            int aaa=1;
        }
        vector<pii> h(n);
        REP(i,n){
            cin>>h[i].first;
            h[i].second=i;
        }
        sort(RALL(h));
        vvi true_cost(n,vi(n,INF));
        REP(i,m){
            int a,b,c;
            cin>>a>>b>>c;
            true_cost[a-1][b-1]=c;
            true_cost[b-1][a-1]=c;
        }
        REP(i,n){
            REP(j,n){
                cost[i][j]=true_cost[h[i].second][h[j].second];
                cost[j][i]=true_cost[h[j].second][h[i].second];
            }
        }
        int max_index=-1;
        for(int i=n-1;i>=0;i--){
            while(i<n-1&&i>=0&&h[i+1].first==h[i].first){
                i--;
            }
            prim(i);
            bool ok=true;
            REP(j,i+1){
                if(!used[j]){
                    ok=false;
                    break;
                }
            }
            if(!ok){
                max_index=i;
                break;
            }
        }
        ll ans=0;
        if(max_index<n-1){
            max_index++;
            while(max_index<n-1&&max_index>=0&&h[max_index+1].first==h[max_index].first){
                max_index++;
            }
            ans=prim(max_index);
        }
        for(int i=max_index;i<n;i++){
            while(i<n-1&&h[i+1].first==h[i].first){
                i++;
            }
            ans+=prim(i,false);
            //int mmin=INF;
            //REP(j,i){
            //  mmin=min(mmin,cost[j][i]);
            //}
            //if(mmin<INF)
            //ans+=mmin;
        }
        cout<<ans<<endl;
    }
}

Measured memory_bytes: 1788