Language: C++

const int maxn =1e5+5;
const double INF=0xfffff;
using namespace std;
typedef long long ll;
int fa[maxn];
ll val[maxn];
int num[maxn];
int mi[maxn];
int vis[maxn];
int vis2[maxn];
int anss[maxn];
struct node
{
    ll x, y;
    friend bool operator < (node a, node b)
    {
        return a.x > b.x;
    }
};
priority_queue<node>q;
int find(int x)
{
    if(fa[x]!=x)
        fa[x]=find(fa[x]);
    return fa[x];
}
void join(int x,int y)
{
    int p,q;
    p=find(x),q=find(y);
    if(p!=q)
    {
        fa[p]=q;
        mi[q]=min(mi[p],mi[q]);
    }
    return ;
}
int main()
{
    int n,m;
    int x,y;
    int sum=0;
    int cnt=0;
    ll ans=0;
    int i;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
    {
        cin>>val[i];
    }
    for(i=0; i<n; i++)
    {
        fa[i]=i;
        mi[i]=val[i];
    }
    while(m--)
        scanf("%d%d",&x,&y),join(x,y);
    for(int i=0; i<n; i++)
    {
        int t=find(i);
        if(vis[t]==0)
        {
            vis[t]=1;
            anss[cnt++]=t;
        }
    }
    if(cnt==1)
    {
        cout<<0<<"\n";
        return 0;
    }
    for(int i=0; i<cnt; i++)
    {
        ans+=mi[anss[i]];
        sum++;
    }
    for(int i=0; i<n; i++)
    {
        int t=find(i);
        node pp;
        pp.x=val[i];
        pp.y=t;
        q.push(pp);
    }
    while(!q.empty())
    {
        ll a=q.top().x;
        int b=q.top().y;
        q.pop();
        if(vis2[b]==0)
        {
            vis2[b]=1;
            continue;
        }
        if(num[b]<cnt-1&&sum<(cnt-1)*2)
        {
            sum++;
            num[b]++;
            ans+=a;
        }
        if(sum==(cnt-1)*2)
            break;
    }
    if(sum<(cnt-1)*2)
        cout<<"Impossible"<<"\n";
    else
        cout<<ans<<"\n";
    return 0;
}


Compiler output:
a.cc:20:1: error: 'priority_queue' does not name a type
   20 | priority_queue<node>q;
      | ^~~~~~~~~~~~~~
a.cc: In function 'void join(int, int)':
a.cc:34:15: error: 'min' was not declared in this scope; did you mean 'mi'?
   34 |         mi[q]=min(mi[p],mi[q]);
      |               ^~~
      |               mi
a.cc: In function 'int main()':
a.cc:46:5: error: 'scanf' was not declared in this scope
   46 |     scanf("%d%d",&n,&m);
      |     ^~~~~
a.cc:49:9: error: 'cin' was not declared in this scope
   49 |         cin>>val[i];
      |         ^~~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | const int maxn =1e5+5;
a.cc:69:9: error: 'cout' was not declared in this scope
   69 |         cout<<0<<"\n";
      |         ^~~~
a.cc:69:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:83:9: error: 'q' was not declared in this scope
   83 |         q.push(pp);
      |         ^
a.cc:85:12: error: 'q' was not declared in this scope
   85 |     while(!q.empty())
      |            ^
a.cc:105:9: error: 'cout' was not declared in this scope
  105 |         cout<<"Impossible"<<"\n";
      |         ^~~~
a.cc:105:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:107:9: error: 'cout' was not declared in this scope
  107 |         cout<<ans<<"\n";
      |         ^~~~
a.cc:107:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'