Language: C++

#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
struct con
{
    multiset<int>conset;
    int index;
    bool operator <(const con &A)const
    {
        if((*conset.begin())<(*A.conset.begin()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};
multiset<con>s;//每个联通块下的点按照权值排序
int n,m,tem1,tem2,cnt;
long long ans;
int a[MAXN];
int vis[MAXN];
int f[MAXN];
vector<int>G[MAXN];
void init()
{
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
    }
}
int findfather(int x)
{
    if(x!=f[x])
    {
        f[x]=findfather(f[x]);
    }
    return f[x];
}
void mergefather(int x,int y)
{
    f[findfather(x)]=y;
}
void spfa(int st)
{
    queue<int>q;
    q.push(st);
    vis[st]=1;
    con c1;
    c1.index=++cnt;
    c1.conset.insert(a[st]);
    //s.conset.insert(a[st]);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i];
            if(!vis[v])
            {
                c1.conset.insert(a[v]);
                vis[v]=1;
                q.push(v);
            }
        }
    }
    s.insert(c1);
}
int main()
{
    scanf("%d %d",&n,&m);
        init();
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d",&tem1,&tem2);
        tem1++;tem2++;
        G[tem1].push_back(tem2);
        G[tem2].push_back(tem1);
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            spfa(i);
        }
    }
  //  printf("%d\n",(*s.begin()).index);
   //printf("%d\n",s.size());
    while(cnt>1)
    {
        con c1=*(s.begin());
        multiset<con>::iterator it=++s.begin();
        //printf("A %d\n",s.size());
        for(;it!=s.end();it++)
        {
        //    printf("ss");
            con c2=*it;
            //printf("%d %d\n",findfather(c1.index),findfather(c2.index));

            if(findfather(c1.index)!=findfather(c2.index))
            {
                s.erase(s.begin()++);
                s.erase(it);
             //   printf("ss");
                ans+=long long(*c1.conset.begin());
                ans+=long long(*(c2.conset.begin()));
                c1.conset.erase(c1.conset.begin());
                c2.conset.erase(c2.conset.begin());
                if(c1.conset.size()>1)
                {
                    s.insert(c1);
                }
                if(c2.conset.size()>1)
                {
                    s.insert(c2);
                }
                mergefather(c1.index,c2.index);
                cnt--;
                break;
            }
        }
            if(it==s.end()&&cnt>1)
            {
                printf("Impossible\n");
                return 0;
            }
        //(*(++s.begin())).conset.erase((*(++s.begin())).conset.begin());
        //(*s.begin()).conset.erase((*s.begin()).conset.begin());
    }
    printf("%lld\n",ans);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:112:22: error: expected primary-expression before 'long'
  112 |                 ans+=long long(*c1.conset.begin());
      |                      ^~~~
a.cc:113:22: error: expected primary-expression before 'long'
  113 |                 ans+=long long(*(c2.conset.begin()));
      |                      ^~~~