Problem:
Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define SET(x, a) memset(x, a, sizeof x);
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pld pair<ld, ld>
const ll MOD = 1000000007;
const ll INF = 1000000000;
template <class T> T read(T &x) {
  char c;
  int flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag *= -1;
  x = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = x * 10 + c - '0';
  x *= flag;
  return x;
}
string s;
int n;
int tot;
vector<pii> adj;
bool vis[100005];
void dfs(int x, int chosen, int dep){
  // if(!vis[dep]) {
    // cout << -1 << endl;
    // exit(0);
  // }
  // cout << x << " " << chosen << " " << tot<< endl;
  if(chosen == -1) return;
  int remain = chosen - 1;
  int pre = 0;
  while(remain){
    if(pre == -1){
      remain--;
      if(!vis[n - 1]){
        cout << -1 << endl;
        exit(0);
      }
      ++tot;
      adj.pb(mp(x, tot));
      dfs(tot, -1, dep+1);
      continue;
    }
    int mx = -1;
    for(int i = remain; i >= 1 && mx == -1; --i)
      if(s[i] == '1' && vis[n - i]) mx = i;
    ++tot;
    adj.pb(mp(x, tot));
    dfs(tot, mx, dep + 1);
    remain -= (mx == -1 ? 1 : mx);
    pre = mx;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  n = s.length();
  s = "#" + s;
  if(s[n] == '1' || s[1] == '0') return cout<< -1 << endl, 0;
  vis[0] =1;
  for(int i = 1; i <= n; ++i)
    vis[i] = s[i] - '0';
  string tmp = s;
  for(int i = n; i >= 1; --i){
    s = tmp;
    if(s[i] == '1'){
      s[i] = '0';
      dfs(++tot, i + 1, 1);
      
  while(tot < n){
    adj.pb(mp(++tot, 1));
  }
  if(adj.size() == n - 1 && tot == n){
    for(auto i : adj)
    cout << i.fi << " " << i.se << endl;
    exit(0);
  }
    }
  }
  cout << -1 << endl;
}