Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define COUT(a) cout << a << endl;

using namespace std;
using Int = long long;
using P = pair<Int, Int>;

int main() {
  Int N;
  cin >> N;

  vector<Int> cnt_o(10001), cnt_e(10001);

  FOR(i, 0, N) {
    Int v;
    cin >> v;
    if (i % 2 == 0) {
      cnt_e[v]++;
    } else {
      cnt_o[v]++;
    }
  }

  auto it_max_o = max_element(begin(cnt_o), end(cnt_o));
  auto it_max_e = max_element(begin(cnt_e), end(cnt_e));
  Int max_o = *it_max_o;
  Int max_e = *it_max_e;
  Int ans = 0;

  if (it_max_o - begin(cnt_o) != it_max_e - begin(cnt_e)) {
    Int sum_o = accumulate(begin(cnt_o), end(cnt_o), 0);
    Int sum_e = accumulate(begin(cnt_e), end(cnt_e), 0);
    ans = sum_o + sum_e - (max_o + max_e);
  } else {
    Int sum_o = accumulate(begin(cnt_o), end(cnt_o), 0);
    Int sum_e = accumulate(begin(cnt_e), end(cnt_e), 0);

    if (sum_o == max_o && sum_e != max_e) {
      sort(begin(cnt_e), end(cnt_e), greater<Int>());
      ans = sum_o + sum_e - (max_o + cnt_e[1]);

    } else if (sum_o != max_o && sum_e == max_e) {
      sort(begin(cnt_o), end(cnt_o), greater<Int>());
      ans = sum_o + sum_e - (cnt_o[1] + max_e);

    } else {
      sort(begin(cnt_e), end(cnt_e), greater<Int>());
      sort(begin(cnt_o), end(cnt_o), greater<Int>());

      ans = sum_o + sum_e - (max(max_o, max_e) + max(cnt_e[1], cnt_o[1]));
    }
  }

  COUT(ans);
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define COUT(a) cout << a << endl;

using namespace std;
using Int = long long;
using P = pair<Int, Int>;

int main() {
  Int N;
  cin >> N;

  vector<Int> cnt_o(100010), cnt_e(100010);

  FOR(i, 0, N) {
    Int v;
    cin >> v;
    if (i % 2 == 0) {
      cnt_e[v]++;
    } else {
      cnt_o[v]++;
    }
  }

  auto it_max_o = max_element(begin(cnt_o), end(cnt_o));
  auto it_max_e = max_element(begin(cnt_e), end(cnt_e));
  Int max_o = *it_max_o;
  Int max_e = *it_max_e;
  Int ans = 0;

  if (it_max_o - begin(cnt_o) != it_max_e - begin(cnt_e)) {
    Int sum_o = accumulate(begin(cnt_o), end(cnt_o), 0);
    Int sum_e = accumulate(begin(cnt_e), end(cnt_e), 0);
    ans = sum_o + sum_e - (max_o + max_e);
  } else {
    Int sum_o = accumulate(begin(cnt_o), end(cnt_o), 0);
    Int sum_e = accumulate(begin(cnt_e), end(cnt_e), 0);

    if (sum_o == max_o && sum_e != max_e) {
      sort(begin(cnt_e), end(cnt_e), greater<Int>());
      ans = sum_o + sum_e - (max_o + cnt_e[1]);

    } else if (sum_o != max_o && sum_e == max_e) {
      sort(begin(cnt_o), end(cnt_o), greater<Int>());
      ans = sum_o + sum_e - (cnt_o[1] + max_e);

    } else {
      sort(begin(cnt_e), end(cnt_e), greater<Int>());
      sort(begin(cnt_o), end(cnt_o), greater<Int>());

      ans = sum_o + sum_e - (max(max_o, max_e) + max(cnt_e[1], cnt_o[1]));
    }
  }

  COUT(ans);
}