Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;

int n,m,x;
int u,v;
int grp;

pll vv[100005];
vvi f(100005);
int visited[100005];
int groups[100005];
map<pll,int> check;
map<int> connected;

void dfs(int u)
{
    visited[u] = 1;
    groups[u] = grp;
    for(int i=0;i<f[u].size();++i)
    {
        if(!visited[f[u][i]]) dfs(f[u][i]);
    }
}

int root(int i)
{
    while(i!=groups[i]) groups[i]=groups[groups[i]],i=groups[i];
    return i;
}


int main()
{
    cin >> n >> m;
    for(int i=0;i<n;++i) cin >> x, vv[i].first = x,vv[i].second = i;
    sort(vv,vv+n);
    for(int i=0;i<m;++i)
    {
        cin >> u >> v;
        f[u].push_back(v);
        f[v].push_back(u);
    }

    int cnt(0);
    for(int i=0;i<n;++i)
    {
        if(!visited[i])
        {
            grp=i;
            groups[i]=i;
            cnt++;
            dfs(i);
        }
    }

    if(n < 2*(n-m-1)) {cout << "Impossible";return 0;}
    if(m==n-1) {cout << 0;return 0;}

    queue<pll> q;
    int edges(cnt-1);
    ll res(0);
    for(int i=0;i<n;++i)
    {
        if(edges==0) break;
        if(q.empty()) q.push(vv[i]);
        else
        {
            pll b = q.front();
            //cout << b.first << " <===> " <<  vv[i].first << endl;
            //cout << root(visited[b.second]) << " ( " << visited[vv[i].second]<< " , " << root(visited[vv[i].second]) << endl;
            if(root(b.second)!=root(vv[i].second) && !check[pll(groups[b.second],groups[vv[i].second])] &&(!connected[root(b.second)] || !connected[root(vv[i].second)]))
            {
                check[pll(groups[b.second],groups[vv[i].second])] = 1;
                check[pll(groups[vv[i].second],groups[b.second])] = 1;
                connected[root(b.second)] = 1;
                connected[root(vv[i].second)] = 1;
                groups[root(vv[i].second)] = root(b.second);
                --edges;
                q.pop();
                res+= b.first + vv[i].first;
            }
            else q.push(vv[i]);
        }
    }
    if(edges) cout << "Impossible";
    else cout << res;
}

/*
4 1
1 2 3 4
0 1
*/



Compiler output:
a.cc:22:8: error: wrong number of template arguments (1, should be at least 2)
   22 | map<int> connected;
      |        ^
In file included from /usr/include/c++/14/map:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_map.h:102:11: note: provided for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'
  102 |     class map
      |           ^~~
a.cc: In function 'int main()':
a.cc:80:118: error: invalid types 'int[int]' for array subscript
   80 |             if(root(b.second)!=root(vv[i].second) && !check[pll(groups[b.second],groups[vv[i].second])] &&(!connected[root(b.second)] || !connected[root(vv[i].second)]))
      |                                                                                                                      ^
a.cc:80:148: error: invalid types 'int[int]' for array subscript
   80 |             if(root(b.second)!=root(vv[i].second) && !check[pll(groups[b.second],groups[vv[i].second])] &&(!connected[root(b.second)] || !connected[root(vv[i].second)]))
      |                                                                                                                                                    ^
a.cc:84:26: error: invalid types 'int[int]' for array subscript
   84 |                 connected[root(b.second)] = 1;
      |                          ^
a.cc:85:26: error: invalid types 'int[int]' for array subscript
   85 |                 connected[root(vv[i].second)] = 1;
      |                          ^