Language: C++

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;

typedef pair<int,int> P;
typedef tuple<int,int,int> T;
struct union_find{
    vector<int> par, rank;
    union_find(int n){
        par = vector<int>(n);
        rank = vector<int>(n);
    }
    void init(int n){
        for(int i=0;i<n;i++){
            par[i]=i;
            rank[i]=0;
        }
    }
    int find(int x){
        if(par[x]==x){
            return x;
        }else{
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x==y) return;
        if(rank[x]<rank[y]){
            par[x]=y;
        }else{
            par[y]=x;
            if(rank[x]==rank[y]) rank[x]++;
        }
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main(){
    int N,M;
    vector<P> H;
    vector<P> G[200];//to, cost
    bool used[200];
    priority_queue<T,vector<T>,greater<T>> que;
    union_find U(200),U2(200);

    while(cin>>N>>M,N){
        H.clear();
        for(int i=0;i<N;i++){
            int h;
            cin>>h;
            H.emplace_back(h,i);
        }
        sort(H.begin(),H.end(),greater<P>());
        for(int i=0;i<200;i++) G[i].clear();
        for(int i=0;i<M;i++){
            int a,b,c;
            cin>>a>>b>>c;
            a--;b--;
            G[a].emplace_back(b,c);
            G[b].emplace_back(a,c);
        }
        fill(used, used+200, false);
        U.init(200);
        U2.init(200);

        int h=-1;
        for(int i=0;i<N;i++){
            while(true){
                int a = H[i].second;
                for(auto e:G[a]){
                    int b=e.first;
                    if(used[b]) U2.unite(a,b); 
                }
                used[a]=true;
                if(i+1<N&&H[i].first==H[i+1].first){
                    i++;
                }else{
                    break;
                }
            }
            bool connected = true;
            for(int j=1;j<=i;j++){
                if(!U2.same(H[0].second,H[j].second)){
                    connected =false;
                    break;
                }
            }
            if(connected){
                if(h==-1) h=i;
            }else{
                h=-1;
            }
        }

        fill(used,used+200,false);
        int ans=0;
        while(!que.empty()) que.pop();
        for(int i=0;i<N;i++){
            while(true){
                int a = H[i].second;
                for(auto e:G[a]){
                    int b = e.first;
                    int c = e.second;
                    if(used[b]){
                        que.emplace(T(c,a,b));
                    }
                }
                used[a]=true;
                if(i+1<N&&H[i].first==H[i+1].first){
                    i++;
                }else{
                    break;
                }
            }
            if(i<h) continue;
            while(!que.empty()){
                int a,b,c;
                tie(c,a,b) = que.top(); que.pop();
                if(!U.same(a,b)){
                    ans+=c;
                    U.unite(a,b);
                }
            }
        }
        for(int i=1;i<N;i++){
            if(!U.same(0,i)){
                ans=0;
                break;
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:59:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   59 |         sort(H.begin(),H.end(),greater<P>());
      |         ^~~~
      |         short