Problem p03816 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 1e2 + 10;
const int mod = 1e9 + 7;

map<int, int> mp;

int32_t main() {
  boost;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  int a[n];

  set<int> s;
  priority_queue<pair<int, int>> q;
  int temp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
    s.insert(a[i]);
  }

  for (auto i : s) {
    q.push({mp[i], i});
  }

  pair<int, int> hell = {0, 0}, hav = {0, 0}, hah = {0, 0};
  while (true) {

    if (q.top().first == 1)
      break;

    hell = q.top();
    q.pop();

    if (hell.first >= 3) {
      q.push({hell.first - 2, hell.second});
    } else if (q.size() > 2) {

      hav = q.top();
      q.pop();
      if (hav.first + hell.first >= 3) {
        if (hell.first == 2) {
          q.push({hell.first - 1, hell.second});
          if (hav.first > 1) {
            q.push({hav.first - 1, hav.second});
          }
        } else {
          q.push({hav.first - 1, hav.second});
        }
      } else {

        hah = q.top();
        q.pop();
        q.push({hav.first, hav.second});
        if (hah.first > 1) {
          q.push({hah.first - 1, hah.second});
        }
      }
    } else {
      break;
    }
  }

  cout << q.size();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define boost                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define int long long

const int N = 1e2 + 10;
const int mod = 1e9 + 7;

map<int, int> mp;

int32_t main() {
  boost;
  int n;
  cin >> n;
  int a[n];

  set<int> s;
  priority_queue<pair<int, int>> q;
  int temp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
    s.insert(a[i]);
  }

  for (auto i : s) {
    q.push({mp[i], i});
  }

  pair<int, int> hell = {0, 0}, hav = {0, 0}, hah = {0, 0};
  while (true) {

    if (q.top().first == 1)
      break;

    hell = q.top();
    q.pop();

    if (hell.first >= 3) {
      q.push({hell.first - 2, hell.second});
    } else if (q.size() > 2) {

      hav = q.top();
      q.pop();
      if (hav.first + hell.first >= 3) {
        if (hell.first == 2) {
          q.push({hell.first - 1, hell.second});
          if (hav.first > 1) {
            q.push({hav.first - 1, hav.second});
          }
        } else {
          q.push({hav.first - 1, hav.second});
        }
      } else {

        hah = q.top();
        q.pop();
        q.push({hav.first, hav.second});
        if (hah.first > 1) {
          q.push({hah.first - 1, hah.second});
        }
      }
    } else {
      break;
    }
  }

  cout << q.size();
  return 0;
}