Problem p03244 (C++)

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

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define inf (long long int)1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a, ll b);
ll palindrome(string s);
ll modexp(ll a, ll b, ll m);
void sieve();
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);

bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // cout<<"cjahl jaa"<<endl;
  ll n;
  cin >> n;
  ll v[n];
  loop(i, 0, n) { cin >> v[i]; }
  // cout<<"kiki "<<endl;
  pll e[100000 + 5];
  pll o[100000 + 5];
  loop(i, 0, 100005) {
    e[i].fi = 0;
    e[i].se = i;
  }
  loop(i, 0, 100005) {
    o[i].fi = 0;
    o[i].se = i;
  }
  // cout<<"kik "<<endl;
  for (ll i = 0; i < n; i = i + 2) {
    e[v[i]].fi++;
  }
  for (ll i = 1; i < n; i = i + 2) {
    o[v[i]].fi++;
  }
  // cout<<"ju "<<endl;
  sort(e, e + 100005, sortinrev);
  sort(o, o + 100005, sortinrev);
  // cout<<"hey "<<endl;
  /*loop(i,0,10){
      cout<<e[i].fi<<" "<<e[i].se<<endl;
  }
  loop(i,0,10){
      cout<<o[i].fi<<" "<<o[i].se<<endl;
  }*/
  ll sum1 = 0, sum2 = 0;
  loop(i, 0, 100005) {
    sum1 += e[i].fi;
    sum2 = sum2 + o[i].fi;
  }
  if (e[0].se != o[0].se) {
    cout << sum1 + sum2 - e[0].fi - o[0].fi << endl;
  } else {
    cout << min(sum1 + sum2 - e[1].fi - o[0].fi,
                sum1 + sum2 - e[0].fi - o[1].fi)
         << endl;
  }
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a == b)
    return a;
  if (a > b)
    return gcd(a % b, b);
  return gcd(a, b % a);
}

ll palindrome(string s) {
  ll l = 0;
  ll h = s.length() - 1;
  while (h > l) {
    if (s[l++] != s[h--]) {
      return 0;
    }
  }
  return 1;
}

ll modexp(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll temp = modexp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1)
    return (temp * (a % m)) % m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
  return temp;
}

void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vl getFactorization(ll x) {
  vl ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

void getZarr(string str, ll Z[]) {
  ll n = str.length();
  ll L, R, k;
  L = R = 0;
  for (ll i = 1; i < n; ++i) {
    if (i > R) {
      L = R = i;
      while (R < n && str[R - L] == str[R])
        R++;
      Z[i] = R - L;
      R--;
    } else {
      k = i - L;
      if (Z[k] < R - i + 1)
        Z[i] = Z[k];
      else {
        L = i;
        while (R < n && str[R - L] == str[R])
          R++;
        Z[i] = R - L;
        R--;
      }
    }
  }
}


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

#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define inf (long long int)1e18
#define eps 0.000001
#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define mod 1000000007
#define MAXN 100001
ll spf[MAXN];
ll gcd(ll a, ll b);
ll palindrome(string s);
ll modexp(ll a, ll b, ll m);
void sieve();
vl getFactorization(ll x);
void getZarr(string str, ll Z[]);

bool sortinrev(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif*/

  // cout<<"cjahl jaa"<<endl;
  ll n;
  cin >> n;
  ll v[n];
  loop(i, 0, n) { cin >> v[i]; }
  // cout<<"kiki "<<endl;
  pll e[100000 + 5];
  pll o[100000 + 5];
  loop(i, 0, 100005) {
    e[i].fi = 0;
    e[i].se = i;
  }
  loop(i, 0, 100005) {
    o[i].fi = 0;
    o[i].se = i;
  }
  // cout<<"kik "<<endl;
  for (ll i = 0; i < n; i = i + 2) {
    e[v[i]].fi++;
  }
  for (ll i = 1; i < n; i = i + 2) {
    o[v[i]].fi++;
  }
  // cout<<"ju "<<endl;
  sort(e, e + 100005, sortinrev);
  sort(o, o + 100005, sortinrev);
  // cout<<"hey "<<endl;
  /*loop(i,0,10){
      cout<<e[i].fi<<" "<<e[i].se<<endl;
  }
  loop(i,0,10){
      cout<<o[i].fi<<" "<<o[i].se<<endl;
  }*/
  ll sum1 = 0, sum2 = 0;
  loop(i, 0, 100005) {
    sum1 += e[i].fi;
    sum2 = sum2 + o[i].fi;
  }
  if (e[0].se != o[0].se) {
    cout << sum1 + sum2 - e[0].fi - o[0].fi << endl;
  } else {
    cout << min(sum1 + sum2 - e[1].fi - o[0].fi,
                sum1 + sum2 - e[0].fi - o[1].fi)
         << endl;
  }
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a == b)
    return a;
  if (a > b)
    return gcd(a % b, b);
  return gcd(a, b % a);
}

ll palindrome(string s) {
  ll l = 0;
  ll h = s.length() - 1;
  while (h > l) {
    if (s[l++] != s[h--]) {
      return 0;
    }
  }
  return 1;
}

ll modexp(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll temp = modexp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1)
    return (temp * (a % m)) % m; // if b is odd a^b = a^(b/2)*a^(b/2)*a
  return temp;
}

void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

vl getFactorization(ll x) {
  vl ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

void getZarr(string str, ll Z[]) {
  ll n = str.length();
  ll L, R, k;
  L = R = 0;
  for (ll i = 1; i < n; ++i) {
    if (i > R) {
      L = R = i;
      while (R < n && str[R - L] == str[R])
        R++;
      Z[i] = R - L;
      R--;
    } else {
      k = i - L;
      if (Z[k] < R - i + 1)
        Z[i] = Z[k];
      else {
        L = i;
        while (R < n && str[R - L] == str[R])
          R++;
        Z[i] = R - L;
        R--;
      }
    }
  }
}