Language: C++

/* code by AbhiTaker
         ____
        /    \
       /  __  \              _______      __       _   _    _____    _____
      /  /  \  \            |__   __|    /  \     | | / /  |  ___|  |  _  \
     /  /____\  \     ___      | |      / /\ \    | |/ /   | |__    | |_)  )
    /  /______\  \   |___|     | |     / /__\ \   |   /    |  __|   |    _/
   /  /        \  \            | |    / /----\ \  | |\ \   | |___   | |\ \
  /__/          \__\           |_|   /_/      \_\ |_| \_\  |_____|  |_| \_\
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M 1000000007
#define N 2000016
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f first
#define s second
#define inf 9000000000000000000
vector<ll> v[N];
ll a[N],vis[N],val[N];
priority_queue< pair<ll,ll> ,vector<pair<ll,ll> > , greater<pair<ll,ll> >   > link,one;
priority_queue< ll ,vector<ll > , greater<ll >   >  pq[N];

void dfs(ll x, ll block)
{
    vis[x] = 1;
    pq[block].push(val[x]);

    for(auto node: v[x])
    {
        if(!vis[node])
            dfs(node, block);
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll n,m,i;
    cin>>n>>m;

    for(i=0; i<n; i++)
        cin>>val[i];

    for(i=1; i<=m; i++)
    {
        ll a,b;
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    ll block = 1;

    for(i=0; i<n; i++)
    {
        if(!vis[i])
        {
            dfs(i, block);
            if(pq[block].size()==1)
                one.push({pq[block].top(), block});
            else
                link.push({pq[block].top(), block});
            pq[block].pop();
            block++;
        }
    }

    if(link.size()+one.size()==1)
    {
        cout<<"0\n";
        return 0;
    }
    if(link.empty() && one.size()==2)
    {
        ll x = one.top().f;
        one.pop();
        x += one.top().f;
        cout<<x<<"\n";
        return 0;
    }
    if(link.empty() && one.size()>2)
    {
         cout<<"Impossible\n";
         return 0;
    }
    ll ans = 0,flag = 0;

    while(1)
    {
        if(link.size()==1 || link.empty())
            break;
       ll b,b2,cost,cost2;
       cost = link.top().f ; b = link.top().s;
       link.pop();
       cost2 = link.top().f ; b2 = link.top().s;
       link.pop();

       if(pq[b].empty() && pq[b2].empty())
       {
           one.push({cost, b});
           ons.push({cost2, b2});
           continue;
       } 

       ans += cost + cost2;
       if(pq[b].size()<pq[b2].size())
       {
           while(!pq[b].empty())
           {
               pq[b2].push(pq[b].top());
               pq[b].pop();
           }

           link.push({pq[b2].top(), b2});
           pq[b2].pop();
       }
       else{
            while(!pq[b2].empty())
           {
               pq[b].push(pq[b2].top());
               pq[b2].pop();
           }
           if(pq[b].empty() && !link.empty())
           {
               flag = 1;
               break;
           }
           link.push({pq[b].top(), b});
           pq[b].pop();
       }

       if(link.empty() || link.size()==1)
            break;

    }

    while(!link.empty())
    {
        if(one.empty())
            break;
       ans += one.top().f + link.top().f;
       ll ind = link.top().s;
       one.pop();
       link.pop();
       if(!pq[ind].empty())
       {
           link.push({pq[ind].top(), ind});
           pq[ind].pop();
       }
    }

    if(!one.empty())
        flag = 1;

    if(flag)
        cout<<"Impossible\n";
    else
        cout<<ans<<"\n";


    return 0;
}


Compiler output:
a.cc:26:79: error: 'std::priority_queue<std::pair<long long int, long long int>, std::vector<std::pair<long long int, long long int> >, std::greater<std::pair<long long int, long long int> > > link' redeclared as different kind of entity
   26 | priority_queue< pair<ll,ll> ,vector<pair<ll,ll> > , greater<pair<ll,ll> >   > link,one;
      |                                                                               ^~~~
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:11:
/usr/include/unistd.h:819:12: note: previous declaration 'int link(const char*, const char*)'
  819 | extern int link (const char *__from, const char *__to)
      |            ^~~~
a.cc: In function 'int main()':
a.cc:67:22: error: request for member 'push' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   67 |                 link.push({pq[block].top(), block});
      |                      ^~~~
a.cc:73:13: error: request for member 'size' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   73 |     if(link.size()+one.size()==1)
      |             ^~~~
a.cc:78:13: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   78 |     if(link.empty() && one.size()==2)
      |             ^~~~~
a.cc:86:13: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   86 |     if(link.empty() && one.size()>2)
      |             ^~~~~
a.cc:95:17: error: request for member 'size' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   95 |         if(link.size()==1 || link.empty())
      |                 ^~~~
a.cc:95:35: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   95 |         if(link.size()==1 || link.empty())
      |                                   ^~~~~
a.cc:98:20: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   98 |        cost = link.top().f ; b = link.top().s;
      |                    ^~~
a.cc:98:39: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   98 |        cost = link.top().f ; b = link.top().s;
      |                                       ^~~
a.cc:99:13: error: request for member 'pop' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
   99 |        link.pop();
      |             ^~~
a.cc:100:21: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  100 |        cost2 = link.top().f ; b2 = link.top().s;
      |                     ^~~
a.cc:100:41: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  100 |        cost2 = link.top().f ; b2 = link.top().s;
      |                                         ^~~
a.cc:101:13: error: request for member 'pop' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  101 |        link.pop();
      |             ^~~
a.cc:106:12: error: 'ons' was not declared in this scope; did you mean 'ans'?
  106 |            ons.push({cost2, b2});
      |            ^~~
      |            ans
a.cc:119:17: error: request for member 'push' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  119 |            link.push({pq[b2].top(), b2});
      |                 ^~~~
a.cc:128:38: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  128 |            if(pq[b].empty() && !link.empty())
      |                                      ^~~~~
a.cc:133:17: error: request for member 'push' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  133 |            link.push({pq[b].top(), b});
      |                 ^~~~
a.cc:137:16: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  137 |        if(link.empty() || link.size()==1)
      |                ^~~~~
a.cc:137:32: error: request for member 'size' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  137 |        if(link.empty() || link.size()==1)
      |                                ^~~~
a.cc:142:17: error: request for member 'empty' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  142 |     while(!link.empty())
      |                 ^~~~~
a.cc:146:34: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  146 |        ans += one.top().f + link.top().f;
      |                                  ^~~
a.cc:147:22: error: request for member 'top' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  147 |        ll ind = link.top().s;
      |                      ^~~
a.cc:149:13: error: request for member 'pop' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  149 |        link.pop();
      |             ^~~
a.cc:152:17: error: request for member 'push' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
  152 |            link.push({pq[ind].top(), ind});
      |                 ^~~~