Language: C++

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int WWW = 0;WWW < (n);WWW++)cerr << v[WWW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

struct UF
{
	vector<int> par; // 親
	vector<int> sz; // 数
	// 初期化
	UF(int n):par(n),sz(n) {
		for(int i = 0; i < n; i++){
			par[i] = i;sz[i] = 1;
		}
	}
	// 木の根を求める
	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	// xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		par[x] = y;
		sz[y] += sz[x];
	}
	// xとyが同じ集合ならtrue
	bool same(int x, int y) { return find(x) == find(y); }
	int size(int n){return sz[find(n)];}
};

int n,m;
vector<pair<ll,ll>> h;
vector<pair<ll,pair<ll,ll>>> v;
bool sink[222];

ll check(ll num){
	UF uf(n);
	ll ret = 0;
	REP(i,m){
		ll A = v[i].SE.FI;
		ll B = v[i].SE.SE;
		ll C = v[i].FI;
		if(sink[A] | sink[B])continue;
		if(!uf.same(A,B)){
			uf.unite(A,B);
			ret += C;
		}
	}
	ll cou = 0;
	REP(i,n){
		cou = max(cou,uf.size(i));
	}
	return (cou == num ? ret : -1);
}

int main(){
	while(cin >> n >> m,n|m){
		h.clear();
		v.clear();
		REP(i,222)sink[i] = false;
		
		REP(i,n){
			ll tmp;
			cin >> tmp;
			h.PB(MP(tmp,i));
		}
		
		REP(i,m){
			ll a,b,c;
			cin >> a >> b >> c;
			a--;b--;
			v.PB(MP(c,MP(a,b)));
		}
		
		sort(ALL(h));
		sort(ALL(v));
		
		ll prev = check(n);
		ll sinknum;
		if(prev == -1){
			cout << 0 << endl;
		}
		else{
			bool flag = false;
			for(sinknum = 0;sinknum < n-1;sinknum++){
				while(sinknum < n-1 && h[sinknum].FI == h[sinknum+1].FI){
					sink[h[sinknum].SE] = true;
					sinknum++;
				}
				sink[h[sinknum].SE] = true;
				ll now = check(n-sinknum-1);
				if(now == -1){
					flag = true;
					break;
				}
				prev = now;
			}
			sink[h[sinknum].SE] = false;
			sinknum--;
			DBG(cout << "prev " << prev << " sinknum " << sinknum << endl;)
			DBG(cout << "sink" << endl;SHOW1d(sink,n);)
			ll tmp = 0;
			for(ll i = sinknum;i >= 0;i--){
				vector<ll> li;
				while(i > 0 && h[i].FI == h[i-1].FI){
					li.PB(i);
					i--;
				}
				li.PB(i);
				DBG(cout << "size " << li.size() << endl;);
				REP(j,m){
					ll A = v[j].SE.FI;
					ll B = v[j].SE.SE;
					ll C = v[j].FI;
					ll seica = -1;
					REP(k,li.size())if(sink[h[li[k]].SE] && (A == h[li[k]].SE || B == h[li[k]].SE))seica = li[k];
					if(seica != -1 && (sink[A] ^ sink[B])){
						tmp += C;
						sink[h[seica].SE] = false;
						j = 0;
					}
				}
				DBG(cout << "i " << i << endl;);
			}
			DBG(cout << "sink" << endl;SHOW1d(sink,n);)
			cout << prev + tmp << endl;
		}
	}
	return 0;
}



Compiler output:
a.cc: In function 'll check(ll)':
a.cc:74:26: error: no matching function for call to 'max(ll&, int)'
   74 |                 cou = max(cou,uf.size(i));
      |                       ~~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:74:26: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   74 |                 cou = max(cou,uf.size(i));
      |                       ~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:74:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   74 |                 cou = max(cou,uf.size(i));
      |                       ~~~^~~~~~~~~~~~~~~~