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 OCaml (4.02.3) (judge verdict: Runtime Error)

open Printf
open Scanf

let id x = x
let pair x y = x, y

let n = scanf "%d" id
let vs = Array.init n @@ fun _ -> scanf " %d" id

module IMap = Map.Make(struct type t = int let compare = Pervasives.compare end)

let odd = ref IMap.empty
let even = ref IMap.empty

let incrmap mapr key =
  if IMap.mem key !mapr then
    mapr := IMap.add key (IMap.find key !mapr + 1) !mapr
  else
    mapr := IMap.add key 1 !mapr

let maxi mapr =
  let lis = IMap.bindings !mapr in
  let lis = List.sort (fun (k1,v1) (k1,v2) -> v2 - v1) lis in
  match lis with
  | [] -> failwith ""
  | (v, n) :: rest -> (v, n, rest)

let () =
  for i = 0 to n - 1 do
    if i mod 2 == 0 then begin
      incrmap odd vs.(i)
    end else begin
      incrmap even vs.(i)
    end
  done;
  let (k1, v1, rest1) = maxi odd in
  let (k2, v2, rest2) = maxi even in
  let run k1 k2 v1 v2 rest1 rest2 =
    if k1 <> k2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      n1 + n2
    end else if v1 > v2 then begin
      let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1 in
      match rest2 with
      | [] -> n1 + n / 2
      | (k, v) :: _ -> n1 + n / 2 - v
    end else if v1 < v2 then begin
      let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2 in
      match rest1 with
      | [] -> n2 + n / 2
      | (k, v) :: _ -> n2 + n / 2 - v 
    end else begin
      (* k1 = k2, v1 = v2 *)
      let rec loop rest1 rest2 =
        match rest1, rest2 with
        | [], [] -> 0
        | [], (k', v') :: rest' -> v'
        | (k', v') :: rest', [] -> v'
        | (k1', v1') :: rest1', (k2', v2') :: rest2' ->
          if v1' < v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 - v1'
          else if v1' > v2' then
            let n1 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest1' in
            let n2 = List.fold_left (fun acc (v, n) -> acc + n) 0 rest2' in
            n1 + n2 - v2' 
          else n / 0 (* loop rest1' rest2' *)
      in
      v1 + loop rest1 rest2
    end
  in
  let result = run k1 k2 v1 v2 rest1 rest2 in
  printf "%d\n" result