Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iostream> // std::cout, std::fixed
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
vector<vector<int>> g;
vector<vector<ll>> rk;
vector<bool> vis;
vector<ll> p;

void bfs(ll s) {
  queue<ll> q;
  q.push(s);
  vector<ll> ans;
  while (!q.empty()) {
    ll cur = q.front();
    q.pop();
    ans.push_back(p[cur]);
    vis[s] = true;
    for (auto son : g[cur]) {
      if (vis[son])
        continue;
      vis[son] = true;
      q.push(son);
    }
  }
  sort(ans.begin(), ans.end());
  reverse(ans.begin(), ans.end());
  rk.push_back(ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  p = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  g = vector<vector<int>>(n);
  vis = vector<bool>(n, false);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    bfs(i);
  }
  ll ans = 0;
  ll vhave = 0;
  ll vneed = (rk.size() - 2) * 2 + 2;
  for (int i = 0; i < rk.size(); i++) {
    if (vhave == vneed) {
      cout << ans;
      return -1;
    }
    ans += rk[i][rk[i].size() - 1];
    rk[i].pop_back();
    vhave++;
  }

  priority_queue<ll, std::vector<ll>, std::greater<ll>> minq;
  for (int i = 0; i < rk.size(); i++) {
    for (auto x : rk[i])
      minq.push(x);
  }
  while (vhave < vneed) {
    if (minq.empty()) {
      cout << "Impossible";
      return 0;
    }
    ans += minq.top();
    minq.pop();
    vhave++;
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iostream> // std::cout, std::fixed
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
vector<vector<int>> g;
vector<vector<ll>> rk;
vector<bool> vis;
vector<ll> p;

void bfs(ll s) {
  queue<ll> q;
  q.push(s);
  vector<ll> ans;
  while (!q.empty()) {
    ll cur = q.front();
    q.pop();
    ans.push_back(p[cur]);
    vis[s] = true;
    for (auto son : g[cur]) {
      if (vis[son])
        continue;
      vis[son] = true;
      q.push(son);
    }
  }
  sort(ans.begin(), ans.end());
  reverse(ans.begin(), ans.end());
  rk.push_back(ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  p = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  g = vector<vector<int>>(n);
  vis = vector<bool>(n, false);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    bfs(i);
  }
  ll ans = 0;
  ll vhave = 0;
  ll vneed = (rk.size() - 2) * 2 + 2;
  for (int i = 0; i < rk.size(); i++) {
    if (vhave == vneed) {
      cout << ans;
      return 0;
    }
    ans += rk[i][rk[i].size() - 1];
    rk[i].pop_back();
    vhave++;
  }

  priority_queue<ll, std::vector<ll>, std::greater<ll>> minq;
  for (int i = 0; i < rk.size(); i++) {
    for (auto x : rk[i])
      minq.push(x);
  }
  while (vhave < vneed) {
    if (minq.empty()) {
      cout << "Impossible";
      return 0;
    }
    ans += minq.top();
    minq.pop();
    vhave++;
  }
  cout << ans;

  return 0;
}