prithivMLmods/Coder-Stat huggingface dataset:
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> odd(100000, 0);
  vector<int> even(100000, 0);
  int od = 0, ev = 0;
  rep(i, N) {
    int t;
    cin >> t;
    if (i % 2 == 0) {
      odd[t]++;
      if (odd[t] > odd[od])
        od = t;
    } else {
      even[t]++;
      if (even[t] > even[ev])
        ev = t;
    }
  }
  sort(odd.begin(), odd.end());
  reverse(odd.begin(), odd.end());
  sort(even.begin(), even.end());
  reverse(even.begin(), even.end());
  if (od == ev) {
    cout << N - max(even[0] + odd[1], even[1] + odd[0]) << endl;
  } else
    cout << N - even[0] - odd[0] << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> odd(100010, 0);
  vector<int> even(100010, 0);
  int od = 0, ev = 0;
  rep(i, N) {
    int t;
    cin >> t;
    if (i % 2 == 0) {
      odd[t]++;
      if (odd[t] > odd[od])
        od = t;
    } else {
      even[t]++;
      if (even[t] > even[ev])
        ev = t;
    }
  }
  sort(odd.begin(), odd.end());
  reverse(odd.begin(), odd.end());
  sort(even.begin(), even.end());
  reverse(even.begin(), even.end());
  if (od == ev) {
    cout << N - max(even[0] + odd[1], even[1] + odd[0]) << endl;
  } else
    cout << N - even[0] - odd[0] << endl;
}
Problem p03244 (C++)

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

int main() {
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> cnt1, cnt2;
  for (int i = 0; i < n; i++) {
    if (i % 2)
      cnt2.push_back(v[i]);
    else
      cnt1.push_back(v[i]);
  }
  map<int, int> CNT1, CNT2;
  for (int i = 0; i < cnt1.size(); i++) {
    CNT1[cnt1[i]]++;
  }
  for (int i = 0; i < cnt2.size(); i++) {
    CNT2[cnt2[i]]++;
  }
  int ans = 0;
  int ma = -1;
  vector<int> ANS;
  for (auto e : CNT1) {
    if (ma < e.second) {
      ANS.resize(1);
      ANS[0] = e.first;
    } else if (ma == e.second)
      ANS.push_back(e.first);
    ma = max(ma, e.second);
  }
  ans += cnt1.size() - ma;
  ma = 0;
  for (auto e : CNT2)
    for (auto E : ANS)
      if (E != e.first)
        ma = max(ma, e.second);
  int ans1 = ans + cnt2.size() - ma;
  ans = 0;
  ma = -1;
  ANS.resize(0);
  for (auto e : CNT2) {
    if (ma < e.second) {
      ANS.resize(1);
      ANS[0] = e.first;
    } else if (ma == e.second && ANS.size() == 1)
      ANS.push_back(e.first);
    ma = max(ma, e.second);
  }
  ans += cnt2.size() - ma;
  ma = 0;
  for (auto e : CNT1)
    for (auto E : ANS)
      if (E != e.first)
        ma = max(ma, e.second);
  int ans2 = ans + cnt1.size() - ma;
  cout << min(ans1, ans2) << endl;

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> cnt1, cnt2;
  for (int i = 0; i < n; i++) {
    if (i % 2)
      cnt2.push_back(v[i]);
    else
      cnt1.push_back(v[i]);
  }
  map<int, int> CNT1, CNT2;
  for (int i = 0; i < cnt1.size(); i++) {
    CNT1[cnt1[i]]++;
  }
  for (int i = 0; i < cnt2.size(); i++) {
    CNT2[cnt2[i]]++;
  }
  int ans = 0;
  int ma = -1;
  vector<int> ANS;
  for (auto e : CNT1) {
    if (ma < e.second) {
      ANS.resize(1);
      ANS[0] = e.first;
    } else if (ma == e.second && ANS.size() == 1)
      ANS.push_back(e.first);
    ma = max(ma, e.second);
  }
  ans += cnt1.size() - ma;
  ma = 0;
  for (auto e : CNT2)
    for (auto E : ANS)
      if (E != e.first)
        ma = max(ma, e.second);
  int ans1 = ans + cnt2.size() - ma;
  ans = 0;
  ma = -1;
  ANS.resize(0);
  for (auto e : CNT2) {
    if (ma < e.second) {
      ANS.resize(1);
      ANS[0] = e.first;
    } else if (ma == e.second && ANS.size() == 1)
      ANS.push_back(e.first);
    ma = max(ma, e.second);
  }
  ans += cnt2.size() - ma;
  ma = 0;
  for (auto e : CNT1)
    for (auto E : ANS)
      if (E != e.first)
        ma = max(ma, e.second);
  int ans2 = ans + cnt1.size() - ma;
  cout << min(ans1, ans2) << endl;

  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> mode2(vector<int> a) {
  sort(a.begin(), a.end());
  int temp1, temp1n;
  int temp2, temp2n;
  int p = 0;
  temp1n = 0;
  temp2n = 0;
  for (int i = 0; i < a.size() - 1; i++) {
    if (a[i] == a[i + 1]) {
      p++;
    } else {
      if (temp1n < p + 1) {
        temp2 = temp1;
        temp2n = temp1n;
        temp1n = p + 1;
        temp1 = a[i];
      } else if (temp2n < p + 1) {
        temp2n = p + 1;
        temp2 = a[i];
      }
      p = 0;
    }
  }
  if (a[a.size() - 2] == a[a.size() - 1]) {
    if (temp1n < p + 1) {
      temp2 = temp1;
      temp2n = temp1n;
      temp1n = p + 1;
      temp1 = a[a[a.size() - 1]];
    } else if (temp2n < p + 1) {
      temp2n = p + 1;
      temp2 = a[a.size() - 1];
    }
  }
  vector<int> mode = {temp1, temp1n, temp2, temp2n};
  return mode;
}

int main() {
  int n;
  cin >> n;
  vector<int> a;
  vector<int> b;
  int p;
  for (int i = 0; i < (n / 2); i++) {
    cin >> p;
    a.push_back(p);
    cin >> p;
    b.push_back(p);
  }
  vector<int> amode = mode2(a);
  vector<int> bmode = mode2(b);
  int ans;
  if (amode[0] != bmode[0]) {
    ans = n - amode[1] - bmode[1];
  } else {
    ans = min(n - amode[3] - bmode[1], n - amode[1] - bmode[3]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> mode2(vector<int> a) {
  sort(a.begin(), a.end());
  int temp1, temp1n;
  int temp2, temp2n;
  int p = 0;
  temp1n = 0;
  temp2n = 0;
  for (int i = 0; i < a.size() - 1; i++) {
    if (a[i] == a[i + 1]) {
      p++;
    } else {
      if (temp1n < p + 1) {
        temp2 = temp1;
        temp2n = temp1n;
        temp1n = p + 1;
        temp1 = a[i];
      } else if (temp2n < p + 1) {
        temp2n = p + 1;
        temp2 = a[i];
      }
      p = 0;
    }
  }
  if (a[a.size() - 2] == a[a.size() - 1]) {
    if (temp1n < p + 1) {
      temp2 = temp1;
      temp2n = temp1n;
      temp1n = p + 1;
      temp1 = a[a.size() - 1];
    } else if (temp2n < p + 1) {
      temp2n = p + 1;
      temp2 = a[a.size() - 1];
    }
  }
  vector<int> mode = {temp1, temp1n, temp2, temp2n};
  return mode;
}

int main() {
  int n;
  cin >> n;
  vector<int> a;
  vector<int> b;
  int p;
  for (int i = 0; i < (n / 2); i++) {
    cin >> p;
    a.push_back(p);
    cin >> p;
    b.push_back(p);
  }
  vector<int> amode = mode2(a);
  vector<int> bmode = mode2(b);
  int ans;
  if (amode[0] != bmode[0]) {
    ans = n - amode[1] - bmode[1];
  } else {
    ans = min(n - amode[3] - bmode[1], n - amode[1] - bmode[3]);
  }
  cout << ans << endl;
}
Problem p03244 (C++)

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

typedef pair<int, int> ii;
typedef long long ll;

int cnt1[MAX], cnt2[MAX], arr[MAX], vis1[MAX], vis2[MAX];
priority_queue<ii> pq1, pq2;

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

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    (i % 2) ? cnt2[arr[i]]++ : cnt1[arr[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (i % 2 && !vis2[arr[i]]) {
      vis2[arr[i]] = 1;
      pq2.push(mp(cnt2[arr[i]], arr[i]));
    } else if (i % 2 == 0 && !vis1[arr[i]]) {
      vis1[arr[i]] = 1;
      pq1.push(mp(cnt1[arr[i]], arr[i]));
    }
  }

  ii u = pq1.top(), v = pq2.top();
  int ans;
  if (u.second != v.second) {
    ans = (n / 2) - u.first + (n / 2) - v.first;
    cout << ans << endl;
    return 0;
  }

  int p = 0, q = 0, r = 0, s = 0;
  p = u.first;
  pq1.pop();
  q = v.first;
  pq2.pop();
  if (!pq1.empty())
    r = pq1.top().first;
  if (!pq2.empty())
    s = pq2.top().first;

  ans = (n / 2) - p + (n / 2) - s;
  ans = min(ans, (n / 2) - q + (n / 2) - r);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAX 100005
#define mp make_pair
using namespace std;

typedef pair<int, int> ii;
typedef long long ll;

int cnt1[MAX], cnt2[MAX], arr[MAX], vis1[MAX], vis2[MAX];
priority_queue<ii> pq1, pq2;

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

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    (i % 2) ? cnt2[arr[i]]++ : cnt1[arr[i]]++;
  }

  for (int i = 0; i < n; i++) {
    if (i % 2 && !vis2[arr[i]]) {
      vis2[arr[i]] = 1;
      pq2.push(mp(cnt2[arr[i]], arr[i]));
    } else if (i % 2 == 0 && !vis1[arr[i]]) {
      vis1[arr[i]] = 1;
      pq1.push(mp(cnt1[arr[i]], arr[i]));
    }
  }

  ii u = pq1.top(), v = pq2.top();
  int ans;
  if (u.second != v.second) {
    ans = (n / 2) - u.first + (n / 2) - v.first;
    cout << ans << endl;
    return 0;
  }

  int p = 0, q = 0, r = 0, s = 0;
  p = u.first;
  pq1.pop();
  q = v.first;
  pq2.pop();
  if (!pq1.empty())
    r = pq1.top().first;
  if (!pq2.empty())
    s = pq2.top().first;

  ans = (n / 2) - p + (n / 2) - s;
  ans = min(ans, (n / 2) - q + (n / 2) - r);

  cout << ans << endl;

  return 0;
}
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
import collections

n = int(input())
v = list(map(int, input().split()))

odd = collections.defaultdict(int)

even = collections.defaultdict(int)

for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1

    else:
        odd[v[i]] += 1

even = list(even.items())
odd = list(odd.items())

even.sort(key=lambda x: x[1], reverse=True)
odd.sort(key=lambda x: x[1], reverse=True)

if even[0][0] == odd[0][0]:
    if len(even) == 1 and len(odd) == 1:
        print(n // 2)
    elif len(even) == 1:
        print(n - (even[0][1] + odd[1][1]))
    elif len(odd) == 1:
        print(n - (even[1][1] + odd[0][1]))
    else:
        print(min(n - (even[1][1] + odd[0][1])), (n - (even[0][1] + odd[1][1])))
else:
    print(n - (even[0][1] + odd[0][1]))


Fixed submission:
import collections

n = int(input())
v = list(map(int, input().split()))

odd = collections.defaultdict(int)

even = collections.defaultdict(int)

for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1

    else:
        odd[v[i]] += 1

even = list(even.items())
odd = list(odd.items())

even.sort(key=lambda x: x[1], reverse=True)
odd.sort(key=lambda x: x[1], reverse=True)

if even[0][0] == odd[0][0]:
    if len(even) == 1 and len(odd) == 1:
        print(n // 2)
    elif len(even) == 1:
        print(n - (even[0][1] + odd[1][1]))
    elif len(odd) == 1:
        print(n - (even[1][1] + odd[0][1]))
    else:
        print(min(n - (even[1][1] + odd[0][1]), n - (even[0][1] + odd[1][1])))
else:
    print(n - (even[0][1] + odd[0][1]))
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

if len(set(v)) == 1:
    print(n // 2)
else:
    v1 = [v[i] for i in range(0, n, 2)]
    v2 = [v[i + 1] for i in range(0, n, 2)]

    kw1 = Counter(v1)
    kw2 = Counter(v2)

    x1 = kw1.most_common()[0][0]
    x2 = kw2.most_common()[0][0]

    if x1 != x2:
        ans = 0
        for i in v1:
            if i != x1:
                ans += 1
        for i in v2:
            if i != x2:
                ans += 1
    else:
        y1 = kw1.most_common()[1][0]
        y2 = kw2.most_common()[1][0]

        ans1 = 0
        ans2 = 0

        for i in v1:
            if i != y1:
                ans1 += 1
        for i in v2:
            if i != x2:
                ans1 += 1

        for i in v1:
            if i != x1:
                ans2 += 1
        for i in v2:
            if i != y2:
                ans2 += 1

        ans = min(ans1, ans2)

print(ans)


Fixed submission:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

if len(set(v)) == 1:
    print(n // 2)
else:
    v1 = [v[i] for i in range(0, n, 2)]
    v2 = [v[i + 1] for i in range(0, n, 2)]

    kw1 = Counter(v1)
    kw2 = Counter(v2)

    x1 = kw1.most_common()[0][0]
    x2 = kw2.most_common()[0][0]

    if x1 != x2:
        ans = 0
        for i in v1:
            if i != x1:
                ans += 1
        for i in v2:
            if i != x2:
                ans += 1
    else:
        y1 = kw1.most_common()[1][0]
        y2 = kw2.most_common()[1][0]

        ans1 = 0
        ans2 = 0

        for i in v1:
            if i != y1:
                ans1 += 1
        for i in v2:
            if i != x2:
                ans1 += 1

        for i in v1:
            if i != x1:
                ans2 += 1
        for i in v2:
            if i != y2:
                ans2 += 1

        ans = min(ans1, ans2)

    print(ans)
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())

index = 0
even_numbers = []
odd_numbers = []

for i in input().split():
    if index % 2 == 0:
        even_numbers.append(int(i))
    else:
        odd_numbers.append(int(i))
    index += 1

even = Counter(even_numbers).most_common(2)
odd = Counter(odd_numbers).most_common(2)

if even[0][1] == odd[0][1]:
    print(min(N - even[0][1] - odd[1][1], N - even[1][1] - odd[0][1]))
else:
    print(N - even[0][1] - odd[0][1])


Fixed submission:
from collections import Counter

N = int(input())

index = 0
even_numbers = []
odd_numbers = []

for i in input().split():
    if index % 2 == 0:
        even_numbers.append(int(i))
    else:
        odd_numbers.append(int(i))
    index += 1

even = Counter(even_numbers).most_common(2)
odd = Counter(odd_numbers).most_common(2)

if even[0][0] == odd[0][0]:
    if len(even) == 1:
        print(N // 2)
    else:
        print(N - even[0][1] - max(even[1][1], odd[1][1]))
else:
    print(N - even[0][1] - odd[0][1])
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
v = list(map(int, input().split()))
even_list = [0] * (n + 1)
odd_list = [0] * (n + 1)

for i in range(n):
    if i % 2 == 0:
        even_list[v[i]] += 1

    else:
        odd_list[v[i]] += 1

if even_list.index(max(even_list)) == odd_list.index(max(odd_list)):
    even_sorted, odd_sorted = sorted(even_list), sorted(odd_list)
    print(
        min(n - even_sorted[-1] - odd_sorted[-2], n - even_sorted[-2] - odd_sorted[-1])
    )

else:
    print(n - max(even_list) - max(odd_list))


Fixed submission:
n = int(input())
v = list(map(int, input().split()))
even_list = [0] * (100000 + 1)
odd_list = [0] * (100000 + 1)

for i in range(n):
    if i % 2 == 0:
        even_list[v[i]] += 1

    else:
        odd_list[v[i]] += 1

if even_list.index(max(even_list)) == odd_list.index(max(odd_list)):
    even_sorted, odd_sorted = sorted(even_list), sorted(odd_list)
    print(
        min(n - even_sorted[-1] - odd_sorted[-2], n - even_sorted[-2] - odd_sorted[-1])
    )

else:
    print(n - max(even_list) - max(odd_list))
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
v = tuple(map(int, input().split()))
c1 = Counter(v[::2])
c2 = Counter(v[1::2])
if len(set(v)) == 1:
    print(n // 2)
else:
    c1m = c1.most_common(1)
    c2m = c2.most_common(1)
    if c1m[0][0] == c2m[0][0]:
        print(min(n - c1m[0][1] - c2m[1][1], n - c1m[1][1] - c2m[0][1]))
    else:
        print(n - c1m[0][1] - c2m[0][1])


Fixed submission:
from collections import Counter

n = int(input())
v = tuple(map(int, input().split()))
c1 = Counter(v[::2])
c2 = Counter(v[1::2])
if len(set(v)) == 1:
    print(n // 2)
else:
    c1m = c1.most_common(2)
    c2m = c2.most_common(2)
    if c1m[0][0] == c2m[0][0]:
        print(min(n - c1m[0][1] - c2m[1][1], n - c1m[1][1] - c2m[0][1]))
    else:
        print(n - c1m[0][1] - c2m[0][1])
Problem p03244 (C++)

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

int main() {
  const int Vmax = 100000;
  using pii = pair<int, int>;
  int N, buf;
  cin >> N;

  int Nhalf = N / 2;

  vector<pii> odd;
  vector<pii> even;
  for (int i = 1; i <= Vmax; i++) {
    odd.emplace_back(make_pair(i, 0));
    even.emplace_back(make_pair(i, 0));
  }
  for (int i = 0; i < N; i += 2) {
    cin >> buf;
    odd.at(buf).second++;

    cin >> buf;
    even.at(buf).second++;
  }

  auto comp = [](pii a, pii b) {
    bool ans =
        (a.second != b.second) ? (a.second > b.second) : (a.first > b.first);
    return ans;
  };
  sort(odd.begin(), odd.end(), comp);
  sort(even.begin(), even.end(), comp);

  int ans = (odd.at(0).first != even.at(0).first)
                ? (Nhalf - odd.at(0).second) + (Nhalf - even.at(0).second)
            : (odd.at(1).second > even.at(1).second)
                ? (Nhalf - odd.at(1).second) + (Nhalf - even.at(0).second)
                : (Nhalf - odd.at(0).second) + (Nhalf - even.at(1).second);

  cout << ans << endl;
}

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

int main() {
  const int Vmax = 100000;
  using pii = pair<int, int>;
  int N, buf;
  cin >> N;

  int Nhalf = N / 2;

  vector<pii> odd;
  vector<pii> even;
  for (int i = 0; i <= Vmax;
       i++) { // 頻度をとるとき1-indexedのままにするため,ダミーを入れる
    odd.emplace_back(make_pair(i, 0));
    even.emplace_back(make_pair(i, 0));
  }
  for (int i = 0; i < N; i += 2) {
    cin >> buf;
    odd.at(buf).second++;

    cin >> buf;
    even.at(buf).second++;
  }

  auto comp = [](pii a, pii b) {
    bool ans =
        (a.second != b.second) ? (a.second > b.second) : (a.first > b.first);
    return ans;
  };
  sort(odd.begin(), odd.end(), comp);
  sort(even.begin(), even.end(), comp);

  int ans = (odd.at(0).first != even.at(0).first)
                ? (Nhalf - odd.at(0).second) + (Nhalf - even.at(0).second)
            : (odd.at(1).second > even.at(1).second)
                ? (Nhalf - odd.at(1).second) + (Nhalf - even.at(0).second)
                : (Nhalf - odd.at(0).second) + (Nhalf - even.at(1).second);

  cout << ans << endl;
}
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
v = input().split()

v1 = v[::2]
v2 = v[1::2]
c1 = Counter(v1).most_common(2)
c2 = Counter(v2).most_common(2)

if c1[0][0] != c2[0][0]:
    print(n - c1[0][1] - c2[0][1])
else:
    print(min(n - c1[0][1] - c2[1][1], n - c1[1][1] - c2[0][1]))


Fixed submission:
from collections import Counter

n = int(input())
v = input().split()

v1 = v[::2]
v2 = v[1::2]
c1 = Counter(v1).most_common(2)
c2 = Counter(v2).most_common(2)

if len(set(v)) == 1:
    print(len(v) // 2)
elif c1[0][0] != c2[0][0]:
    print(n - c1[0][1] - c2[0][1])
else:
    print(min(n - c1[0][1] - c2[1][1], n - c1[1][1] - c2[0][1]))
Problem p03244 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i in range(n):
        if i % 2 == 0:
            d1[v[i]] += 1
        else:
            d2[v[i]] += 1
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans = n
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            aa1 = rd1[a1[min(h, la1)]]
            aa2 = rd2[a2[min(i, la2)]]
            for j in aa1:
                for k in aa2:
                    if j != k:
                        ans = min(
                            ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)]
                        )
                    else:
                        ans = min(
                            ans,
                            (n // 2)
                            - a1[min(h, la1)]
                            + (n // 2)
                            - a2[min(i, la2)]
                            + min(a1[min(h, la1)], a2[min(i, la2)]),
                        )
    print(ans)


if __name__ == "__main__":
    solve()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i in range(n):
        if i % 2 == 0:
            d1[v[i]] += 1
        else:
            d2[v[i]] += 1
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans = n
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            aa1 = rd1[a1[min(h, la1)]]
            aa2 = rd2[a2[min(i, la2)]]
            s1 = set(aa1)
            s2 = set(aa2)
            if len(s1) > 1 or len(s2) > 1 or (s1 - s2) or (s2 - s1):
                ans = min(ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)])
            else:
                ans = min(
                    ans,
                    (n // 2)
                    - a1[min(h, la1)]
                    + (n // 2)
                    - a2[min(i, la2)]
                    + min(a1[min(h, la1)], a2[min(i, la2)]),
                )
    print(ans)


if __name__ == "__main__":
    solve()
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> K;
  vector<pair<int, int>> G;
  vector<int> KI(n / 2);
  vector<int> GU(n / 2);
  for (int i = 0; i < n / 2; i++) {
    cin >> KI.at(i) >> GU.at(i);
  }
  sort(KI.begin(), KI.end());
  sort(GU.begin(), GU.end());
  K.push_back(make_pair(1, KI.at(0)));
  G.push_back(make_pair(1, GU.at(0)));
  for (int i = 1; i < n / 2; i++) {
    if (K.at(K.size() - 1).second == KI.at(i)) {
      K.at(K.size() - 1).first = K.at(K.size() - 1).first + 1;
    } else {
      K.push_back(make_pair(1, KI.at(i)));
    }
  }
  for (int i = 1; i < n / 2; i++) {
    if (G.at(K.size() - 1).second == GU.at(i)) {
      G.at(K.size() - 1).first = G.at(K.size() - 1).first + 1;
    } else {
      G.push_back(make_pair(1, GU.at(i)));
    }
  }
  K.push_back(make_pair(0, -1));
  G.push_back(make_pair(0, -1));
  sort(K.begin(), K.end());
  reverse(K.begin(), K.end());
  sort(G.begin(), G.end());
  reverse(G.begin(), G.end());
  if (K.at(0).second == G.at(0).second) {
    cout << n - max(K.at(0).first + G.at(1).first,
                    K.at(1).first + G.at(0).first)
         << endl;
  } else {
    cout << n - K.at(0).first - G.at(0).first << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> K;
  vector<pair<int, int>> G;
  vector<int> KI(n / 2);
  vector<int> GU(n / 2);
  for (int i = 0; i < n / 2; i++) {
    cin >> KI.at(i) >> GU.at(i);
  }
  sort(KI.begin(), KI.end());
  sort(GU.begin(), GU.end());
  K.push_back(make_pair(1, KI.at(0)));
  G.push_back(make_pair(1, GU.at(0)));
  for (int i = 1; i < n / 2; i++) {
    if (K.at(K.size() - 1).second == KI.at(i)) {
      K.at(K.size() - 1).first = K.at(K.size() - 1).first + 1;
    } else {
      K.push_back(make_pair(1, KI.at(i)));
    }
  }
  for (int i = 1; i < n / 2; i++) {
    if (G.at(G.size() - 1).second == GU.at(i)) {
      G.at(G.size() - 1).first = G.at(G.size() - 1).first + 1;
    } else {
      G.push_back(make_pair(1, GU.at(i)));
    }
  }
  K.push_back(make_pair(0, -1));
  G.push_back(make_pair(0, -1));
  sort(K.begin(), K.end());
  reverse(K.begin(), K.end());
  sort(G.begin(), G.end());
  reverse(G.begin(), G.end());
  if (K.at(0).second == G.at(0).second) {
    cout << n - max(K.at(0).first + G.at(1).first,
                    K.at(1).first + G.at(0).first)
         << endl;
  } else {
    cout << n - K.at(0).first - G.at(0).first << endl;
  }
}
Problem p03245 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  ll x[n], y[n];
  int f[2] = {0, 0};
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    f[(x[i] + y[i] + 2) % 2]++;
  }
  if (f[0] * f[1] > 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> d;
  for (int i = 0; i < 35; i++)
    d.push_back((ll)pow(2, i));
  if (f[0] > 0)
    d.push_back(1);
  cout << d.size() << endl;
  for (int i = 0; i < d.size() - 1; i++)
    cout << d[i] << " ";
  cout << d.back() << endl;
  for (int i = 0; i < n; i++) {
    ll u = x[i] + y[i];
    ll v = x[i] - y[i];
    if (f[0] > 0) {
      u--;
      v--;
    }
    for (int j = 0; j < 35; j++) {
      u += d[j];
      v += d[j];
    }
    u /= 2;
    v /= 2;
    for (int j = 0; j < 35; j++) {
      ll s = u / ((ll)pow(2, j)) & 1;
      ll t = v / ((ll)pow(2, j)) & 1;
      if (s) {
        if (t)
          cout << "R";
        else
          cout << "U";
      } else {
        if (t)
          cout << "D";
        else
          cout << "L";
      }
    }
    if (f[0] > 0)
      cout << "R";
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  ll x[n], y[n];
  int f[2] = {0, 0};
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    f[(x[i] + y[i] + 1000000000000) % 2]++;
  }
  if (f[0] * f[1] > 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> d;
  for (int i = 0; i < 35; i++)
    d.push_back((ll)pow(2, i));
  if (f[0] > 0)
    d.push_back(1);
  cout << d.size() << endl;
  for (int i = 0; i < d.size() - 1; i++)
    cout << d[i] << " ";
  cout << d.back() << endl;
  for (int i = 0; i < n; i++) {
    ll u = x[i] + y[i];
    ll v = x[i] - y[i];
    if (f[0] > 0) {
      u--;
      v--;
    }
    for (int j = 0; j < 35; j++) {
      u += d[j];
      v += d[j];
    }
    u /= 2;
    v /= 2;
    for (int j = 0; j < 35; j++) {
      ll s = u / ((ll)pow(2, j)) & 1;
      ll t = v / ((ll)pow(2, j)) & 1;
      if (s) {
        if (t)
          cout << "R";
        else
          cout << "U";
      } else {
        if (t)
          cout << "D";
        else
          cout << "L";
      }
    }
    if (f[0] > 0)
      cout << "R";
    cout << endl;
  }
}
Problem p03245 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
using namespace std;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const string dir = "URDL";

string solve(long long x, long long y, const vector<long long> &arm) {
  string ret;
  int id;
  long long d, dd, xx, yy;
  for (auto it = arm.rbegin(); it != arm.rend(); it++) {
    d = LLONG_MAX;
    for (int i = 0; i < 4; i++) {
      xx = x + dx[i] * *it;
      yy = y + dy[i] * *it;
      dd = abs(xx) + abs(yy);

      if (dd < d) {
        id = i;
        d = dd;
      }
    }

    ret += dir[(id + 2) % 4];
    x += dx[id] * *it;
    y += dy[id] * *it;
  }

  reverse(ret.begin(), ret.end());

  return ret;
}

int main(void) {
  int n;
  cin >> n;

  vector<long long> x(n), y(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];

  bool b = (abs(x[0]) + abs(y[0])) % 2;
  for (int i = 0; i < n; i++) {
    if ((abs(x[i]) + abs(y[i])) % 2 != b) {
      cout << -1 << endl;
      return -1;
    }
  }

  long long md = 0;
  for (int i = 0; i < n; i++)
    md = max(md, abs(x[i]) + abs(y[i]));

  vector<long long> arm;
  if (b == 0)
    arm.push_back(1);
  for (long long i = 1, sum = (b == 0 ? 1 : 0); sum < md; i *= 2) {
    arm.push_back(i);
    sum += i;
  }

  cout << arm.size() << endl;
  for (int i = 0; i < arm.size(); i++)
    cout << arm[i] << (i + 1 == arm.size() ? '\n' : ' ');

  for (int i = 0; i < n; i++)
    cout << solve(x[i], y[i], arm) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
using namespace std;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const string dir = "URDL";

string solve(long long x, long long y, const vector<long long> &arm) {
  string ret;
  int id;
  long long d, dd, xx, yy;
  for (auto it = arm.rbegin(); it != arm.rend(); it++) {
    d = LLONG_MAX;
    for (int i = 0; i < 4; i++) {
      xx = x + dx[i] * *it;
      yy = y + dy[i] * *it;
      dd = abs(xx) + abs(yy);

      if (dd < d) {
        id = i;
        d = dd;
      }
    }

    ret += dir[(id + 2) % 4];
    x += dx[id] * *it;
    y += dy[id] * *it;
  }

  reverse(ret.begin(), ret.end());

  return ret;
}

int main(void) {
  int n;
  cin >> n;

  vector<long long> x(n), y(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];

  bool b = (abs(x[0]) + abs(y[0])) % 2;
  for (int i = 0; i < n; i++) {
    if ((abs(x[i]) + abs(y[i])) % 2 != b) {
      cout << -1 << endl;
      return 0;
    }
  }

  long long md = 0;
  for (int i = 0; i < n; i++)
    md = max(md, abs(x[i]) + abs(y[i]));

  vector<long long> arm;
  if (b == 0)
    arm.push_back(1);
  for (long long i = 1, sum = (b == 0 ? 1 : 0); sum < md; i *= 2) {
    arm.push_back(i);
    sum += i;
  }

  cout << arm.size() << endl;
  for (int i = 0; i < arm.size(); i++)
    cout << arm[i] << (i + 1 == arm.size() ? '\n' : ' ');

  for (int i = 0; i < n; i++)
    cout << solve(x[i], y[i], arm) << endl;

  return 0;
}
Problem p03245 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 4e1 + 5;
int n, m, a[N], b[N], c[N];
char s[N];
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();
  c[1] = 1, m = ~abs(a[1] + b[1]) & 1;
  for (int i = 2; i <= n; ++i)
    if (m != (~abs(a[i] + b[i]) & 1))
      return puts("-1"), 0;
  c[++m] = 1;
  for (int i = 1; i <= 30; ++i)
    c[++m] = 1 << i;
  printf("%d\n", m);
  for (int i = 1; i <= m; ++i)
    printf("%d ", c[i]);
  puts("");
  for (int i = 1; i <= n; ++i) {
    int nx = 0, ny = 0;
    for (int j = m; j; --j) {
      int dx = a[i] - nx, dy = b[i] - ny;
      if (abs(dx) > abs(dy))
        dx > 0 ? (nx += c[j], s[j] = 'R') : (nx -= c[j], s[j] = 'L');
      else
        dy > 0 ? (ny += c[j], s[j] = 'U') : (ny -= c[j], s[j] = 'D');
    }
    puts(s + 1);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      f = -1;
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 1e3 + 5, M = 4e1 + 5;
int n, m, a[N], b[N], c[M];
char s[M];
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();
  c[1] = 1, m = ~abs(a[1] + b[1]) & 1;
  for (int i = 2; i <= n; ++i)
    if (m != (~abs(a[i] + b[i]) & 1))
      return puts("-1"), 0;
  c[++m] = 1;
  for (int i = 1; i <= 30; ++i)
    c[++m] = 1 << i;
  printf("%d\n", m);
  for (int i = 1; i <= m; ++i)
    printf("%d ", c[i]);
  puts("");
  for (int i = 1; i <= n; ++i) {
    int nx = 0, ny = 0;
    for (int j = m; j; --j) {
      int dx = a[i] - nx, dy = b[i] - ny;
      if (abs(dx) > abs(dy))
        dx > 0 ? (nx += c[j], s[j] = 'R') : (nx -= c[j], s[j] = 'L');
      else
        dy > 0 ? (ny += c[j], s[j] = 'U') : (ny -= c[j], s[j] = 'D');
    }
    puts(s + 1);
  }
  return 0;
}
Problem p03245 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int x[2010], y[2010], pow[2010], sum[2010], n;
int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
    sum[i] = abs(x[i] + y[i]);
  }
  for (i = 1; i < n; i++)
    if (sum[i] % 2 != sum[i + 1] % 2) {
      printf("%d", -1);
      return 0;
    }
  int maxd = 0, cnt = 0;
  for (i = 1; i <= n; i++)
    maxd = max(maxd, sum[i]);
  if (!(sum[1] % 2)) {
    pow[++cnt] = 1;
    maxd--;
  }
  int tmp = 1;
  while (tmp <= maxd + 1) {
    pow[++cnt] = tmp;
    tmp *= 2;
  }
  printf("%d\n", cnt);
  for (i = 1; i <= cnt; i++)
    printf("%d ", pow[cnt - i + 1]);
  printf("\n");
  for (i = 1; i <= n; i++) {
    int fx = x[i], fy = y[i], nx = 0, ny = 0;
    for (j = cnt; j >= 1; j--) {
      int chax = fx - nx, chay = fy - ny;
      if (abs(chax) > abs(chay)) {
        if (chax > 0) {
          printf("R");
          nx += pow[j];
        } else {
          printf("L");
          nx -= pow[j];
        }
      } else {
        if (chay > 0) {
          printf("U");
          ny += pow[j];
        } else {
          printf("D");
          ny -= pow[j];
        }
      }
    }
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int x[2010], y[2010], pow[2010], sum[2010], n;
int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
    sum[i] = abs(x[i] + y[i]);
  }
  for (i = 1; i < n; i++)
    if (sum[i] % 2 != sum[i + 1] % 2) {
      printf("%d", -1);
      return 0;
    }
  int maxd = 0, cnt = 0;
  for (i = 1; i <= n; i++)
    maxd = max(maxd, sum[i]);
  if (!(sum[1] % 2)) {
    pow[++cnt] = 1;
    maxd--;
  }
  int tmp = 1;
  while (tmp - 1 <= maxd) {
    pow[++cnt] = tmp;
    tmp *= 2;
  }
  printf("%d\n", cnt);
  for (i = 1; i <= cnt; i++)
    printf("%d ", pow[cnt - i + 1]);
  printf("\n");
  for (i = 1; i <= n; i++) {
    int fx = x[i], fy = y[i], nx = 0, ny = 0;
    for (j = cnt; j >= 1; j--) {
      int chax = fx - nx, chay = fy - ny;
      if (abs(chax) > abs(chay)) {
        if (chax > 0) {
          printf("R");
          nx += pow[j];
        } else {
          printf("L");
          nx -= pow[j];
        }
      } else {
        if (chay > 0) {
          printf("U");
          ny += pow[j];
        } else {
          printf("D");
          ny -= pow[j];
        }
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int n, mx[2], mi[2], my[2], ans, a[2][100005];
int main() {
  int i, j, t;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &t), a[i % 2][t]++;
  for (i = 0; i < 2; i++)
    for (j = 1; j <= 100000; j++) {
      if (a[i][j] > mx[i])
        mx[i] = a[i][j], mi[i] = j;
    }
  if (mi[0] != mi[1])
    ans = n - mx[0] - mx[1];
  else {
    for (i = 0; i < 2; i++)
      for (j = 1; j <= 100000; j++) {
        if (j == mi[0])
          continue;
        if (a[i][j] > my[j])
          my[i] = a[i][j];
      }
    ans = n - max(mx[0] + my[1], mx[1] + my[0]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int n, mx[2], mi[2], my[2], ans, a[2][100005];
int main() {
  int i, j, t;
  cin >> n;
  for (i = 0; i < n; i++)
    scanf("%d", &t), a[i % 2][t]++;
  for (i = 0; i < 2; i++)
    for (j = 1; j <= 100000; j++) {
      if (a[i][j] > mx[i])
        mx[i] = a[i][j], mi[i] = j;
    }
  if (mi[0] != mi[1])
    ans = n - mx[0] - mx[1];
  else {
    for (i = 0; i < 2; i++)
      for (j = 1; j <= 100000; j++) {
        if (j == mi[0])
          continue;
        if (a[i][j] > my[i])
          my[i] = a[i][j];
      }
    ans = n - max(mx[0] + my[1], mx[1] + my[0]);
  }
  cout << ans;
  return 0;
}
Problem p03246 (C++)

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

using namespace std;

typedef long long ll;

const int N = 10000;
int a[N + 1], b[N + 1];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (i % 2) {
      b[x]++;
    } else {
      a[x]++;
    }
  }
  int ma = 0, ai = 0;
  int mb = 0, bi = 0;
  for (int i = 0; i <= N; i++) {
    if (a[i] > ma) {
      ma = a[i];
      ai = i;
    }
    if (b[i] > mb) {
      mb = b[i];
      bi = i;
    }
  }
  int s = ma + mb;
  if (ai == bi) {
    int m = 0;
    for (int i = 0; i <= N; i++) {
      if (i == ai)
        continue;
      m = max(m, a[i]);
    }
    s = mb + m;
    m = 0;
    for (int i = 0; i <= N; i++) {
      if (i == bi)
        continue;
      m = max(m, b[i]);
    }
    s = max(s, ma + m);
  }
  cout << n - s << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 100000;
int a[N + 1], b[N + 1];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (i % 2) {
      b[x]++;
    } else {
      a[x]++;
    }
  }
  int ma = 0, ai = 0;
  int mb = 0, bi = 0;
  for (int i = 0; i <= N; i++) {
    if (a[i] > ma) {
      ma = a[i];
      ai = i;
    }
    if (b[i] > mb) {
      mb = b[i];
      bi = i;
    }
  }
  int s = ma + mb;
  if (ai == bi) {
    int m = 0;
    for (int i = 0; i <= N; i++) {
      if (i == ai)
        continue;
      m = max(m, a[i]);
    }
    s = mb + m;
    m = 0;
    for (int i = 0; i <= N; i++) {
      if (i == bi)
        continue;
      m = max(m, b[i]);
    }
    s = max(s, ma + m);
  }
  cout << n - s << endl;
}
Problem p03246 (C++)

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

using namespace std;
typedef long long LL;

inline LL read() {
  LL x;
  scanf("%lld", &x);
  return x;
}
inline void read(char *s) { scanf("%s", s); }
inline void no() {
  printf("no\n");
  exit(0);
}

#define maxn 12000
int v[2][maxn], ans[2], pans[2];
int main() {
  int n = read();
  for (int i = 0; i < n; i += 1)
    v[i & 1][read()] += 1;
  ans[0] = ans[1] = 0;
  v[0][0] = v[1][0] = -1;
  for (int i = 0; i < maxn; i += 1)
    for (int j = 0; j < 2; j += 1)
      if (v[j][i] > v[j][ans[j]])
        ans[j] = i;
  if (ans[0] != ans[1])
    printf("%d", n - v[0][ans[0]] - v[1][ans[1]]);
  else {
    for (int i = 0; i < maxn; i += 1)
      for (int j = 0; j < 2; j += 1)
        if (v[j][i] > v[j][pans[j]] && i != ans[j])
          pans[j] = i;
    printf("%d",
           n - max(v[0][ans[0]] + v[1][pans[1]], v[0][pans[0]] + v[1][ans[1]]));
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

inline LL read() {
  LL x;
  scanf("%lld", &x);
  return x;
}
inline void read(char *s) { scanf("%s", s); }
inline void no() {
  printf("no\n");
  exit(0);
}

#define maxn 120000
int v[2][maxn], ans[2], pans[2];
int main() {
  int n = read();
  for (int i = 0; i < n; i += 1)
    v[i & 1][read()] += 1;
  ans[0] = ans[1] = 0;
  v[0][0] = v[1][0] = -1;
  for (int i = 0; i < maxn; i += 1)
    for (int j = 0; j < 2; j += 1)
      if (v[j][i] > v[j][ans[j]])
        ans[j] = i;
  if (ans[0] != ans[1])
    printf("%d", n - v[0][ans[0]] - v[1][ans[1]]);
  else {
    for (int i = 0; i < maxn; i += 1)
      for (int j = 0; j < 2; j += 1)
        if (v[j][i] > v[j][pans[j]] && i != ans[j])
          pans[j] = i;
    printf("%d",
           n - max(v[0][ans[0]] + v[1][pans[1]], v[0][pans[0]] + v[1][ans[1]]));
  }
}
Problem p03246 (C++)

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

#define pii pair<int, int>
#define ll long long
#define F first
#define S second
#define mp make_pair

using namespace std;

const int N = 1e5 + 123;

int n;

int a[N], cnt1[N], cnt2[N], ans = N;

pii x, y, p;

set<pii> s, t;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i += 2) {
    cnt1[a[i]]++;
    cnt2[a[i + 1]]++;
  }
  for (int i = 1; i < n; i += 2) {
    s.insert(mp(cnt1[a[i]], a[i]));
    t.insert(mp(cnt2[a[i + 1]], a[i + 1]));
  }
  x = *s.rbegin();
  y = *t.rbegin();
  s.erase(s.end());
  t.erase(t.end());
  if (x.S == y.S) {
    if (!t.empty()) {
      p = *t.rbegin();
      ans = min(n - (x.F + p.F), ans);
    }
    if (!s.empty()) {
      p = *s.rbegin();
      ans = min(ans, n - (y.F + p.F));
    }
    if (!t.empty() && !s.empty()) {
      x = *s.rbegin();
      y = *t.rbegin();
      ans = min(ans, n - (x.F + y.F));
    }
    if (t.empty() && s.empty()) {
      ans = n / 2;
    }
  } else {
    ans = n - (x.F + y.F);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

#define pii pair<int, int>
#define ll long long
#define F first
#define S second
#define mp make_pair

using namespace std;

const int N = 1e5 + 123;

int n;

int a[N], cnt1[N], cnt2[N], ans = N;

pii x, y, p;

set<pii> s, t;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i += 2) {
    cnt1[a[i]]++;
    cnt2[a[i + 1]]++;
  }
  for (int i = 1; i < n; i += 2) {
    s.insert(mp(cnt1[a[i]], a[i]));
    t.insert(mp(cnt2[a[i + 1]], a[i + 1]));
  }
  x = *s.rbegin();
  y = *t.rbegin();
  s.erase(*s.rbegin());
  t.erase(*t.rbegin());
  if (x.S == y.S) {
    if (!t.empty()) {
      p = *t.rbegin();
      ans = min(n - (x.F + p.F), ans);
    }
    if (!s.empty()) {
      p = *s.rbegin();
      ans = min(ans, n - (y.F + p.F));
    }
    if (!t.empty() && !s.empty()) {
      x = *s.rbegin();
      y = *t.rbegin();
      ans = min(ans, n - (x.F + y.F));
    }
    if (t.empty() && s.empty()) {
      ans = n / 2;
    }
  } else {
    ans = n - (x.F + y.F);
  }
  cout << ans;
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

vector<P> s;
vector<P> t;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  scanf("%d", &n);
  int v[n];
  rep(i, 0, n) {
    cin >> v[i];
    v[i]--;
  }
  rep(j, 0, 100000) {
    s.push_back(P(0, j));
    t.push_back(P(0, j));
  }
  rep(i, 0, n) {
    if (i % 2 == 0) {
      s[v[i]].first++;
    } else
      t[v[i]].first++;
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  if (s[0].second != t[0].second) {
    printf("%d\n", n - s[0].first - t[0].first);
  } else {
    int a = max(s[1].first, t[1].first);
    printf("%d\n", n - s[0].first - a);
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

vector<P> s;
vector<P> t;
int main() {
  int n;
  scanf("%d", &n);
  int v[n];
  rep(i, 0, n) {
    cin >> v[i];
    v[i]--;
  }
  rep(j, 0, 100000) {
    s.push_back(P(0, j));
    t.push_back(P(0, j));
  }
  rep(i, 0, n) {
    if (i % 2 == 0) {
      s[v[i]].first++;
    } else
      t[v[i]].first++;
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  if (s[0].second != t[0].second) {
    printf("%d\n", n - s[0].first - t[0].first);
  } else {
    int a = max(s[1].first, t[1].first);
    printf("%d\n", n - s[0].first - a);
  }
}
Problem p03246 (C++)

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

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("vvv_in.txt", "r", stdin);
#endif

  int n;
  cin >> n;

  map<int, int> high[2];

  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;

    ++high[i % 2][k];
  }

  int mx[2] = {};

  for (auto it = high[0].begin(); it != high[0].end(); ++it)
    if (high[0][mx[0]] < high[0][it->first])
      mx[0] = it->first;

  for (auto it = high[1].begin(); it != high[1].end(); ++it)
    if (high[1][mx[1]] < high[1][it->first])
      mx[1] = it->first;

  int md[2] = {};

  for (auto it = high[0].begin(); it != high[0].end(); ++it)
    if (high[0][md[0]] < high[0][it->first] && it->first != mx[0])
      md[0] = it->first;

  for (auto it = high[1].begin(); it != high[1].end(); ++it)
    if (high[1][md[1]] < high[1][it->first] && it->first != mx[1])
      md[1] = it->first;

  if (mx[0] != mx[1])
    cout << n - high[0][mx[0]] - high[1][mx[1]];
  else
    cout << n - max(high[0][mx[0]] + high[1][md[1]],
                    high[0][md[0]] + high[1][mx[1]]);

  return 0;
}


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

#define ll long long

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  map<int, int> high[2];

  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;

    ++high[i % 2][k];
  }

  int mx[2] = {};

  for (auto it = high[0].begin(); it != high[0].end(); ++it)
    if (high[0][mx[0]] < high[0][it->first])
      mx[0] = it->first;

  for (auto it = high[1].begin(); it != high[1].end(); ++it)
    if (high[1][mx[1]] < high[1][it->first])
      mx[1] = it->first;

  int md[2] = {};

  for (auto it = high[0].begin(); it != high[0].end(); ++it)
    if (high[0][md[0]] < high[0][it->first] && it->first != mx[0])
      md[0] = it->first;

  for (auto it = high[1].begin(); it != high[1].end(); ++it)
    if (high[1][md[1]] < high[1][it->first] && it->first != mx[1])
      md[1] = it->first;

  if (mx[0] != mx[1])
    cout << n - high[0][mx[0]] - high[1][mx[1]];
  else
    cout << n - max(high[0][mx[0]] + high[1][md[1]],
                    high[0][md[0]] + high[1][mx[1]]);

  return 0;
}