Problem p03816 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

int main() {
  int N;
  cin >> N;
  map<int, int> m;
  int A;
  REP(i, N) {
    cin >> A;
    ++m[A];
  }
  priority_queue<int> q;
  for (auto kv : m) {
    q.push(kv.second);
  }
  while (!q.empty()) {
    int a = q.top();
    if (a == 1)
      break;
    q.pop();
    int b = q.top();
    q.pop();
    --a;
    --b;
    if (a > 0)
      q.push(a);
    if (b > 0)
      q.push(b);
  }
  cout << q.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define PB push

#define MOD 1000000007LL
#define EPS 1e-10

int main() {
  int N;
  cin >> N;
  map<int, int> m;
  int A;
  REP(i, N) {
    cin >> A;
    ++m[A];
  }
  priority_queue<int> q;
  for (auto kv : m) {
    q.push(kv.second);
  }
  while (!q.empty()) {
    int a = q.top();
    if (a == 1)
      break;
    q.pop();
    if (q.empty()) {
      --a;
      --a;
      if (a > 0)
        q.push(a);
    } else {
      int b = q.top();
      q.pop();
      --a;
      --b;
      if (a > 0)
        q.push(a);
      if (b > 0)
        q.push(b);
    }
  }
  cout << q.size() << endl;
  return 0;
}