Problem:
Score : 500 points

Problem Statement

In CODE FESTIVAL XXXX, there are N+1 participants from all over the world, including Takahashi.

Takahashi checked and found that the time gap (defined below) between the local times in his city and the i-th person's city was D_i hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is d o'clock at the moment when the local time in city A is 0 o'clock, then the time gap between these two cities is defined to be min(d,24-d) hours.
Here, we are using 24-hour notation.
That is, the local time in the i-th person's city is either d o'clock or 24-d o'clock at the moment when the local time in Takahashi's city is 0 o'clock, for example.

Then, for each pair of two people chosen from the N+1 people, he wrote out the time gap between their cities. Let the smallest time gap among them be s hours.

Find the maximum possible value of s.

Constraints

1 \leq N \leq 50

0 \leq D_i \leq 12

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the maximum possible value of s.

Sample Input 1

3
7 12 8

Sample Output 1

4

For example, consider the situation where it is 7, 12 and 16 o'clock in each person's city at the moment when it is 0 o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is 4 hours.

Sample Input 2

2
11 11

Sample Output 2

2

Sample Input 3

1
0

Sample Output 3

0

Note that Takahashi himself is also a participant.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::Ordering;

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get {
    ([$t: ty]) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

struct HopcroftKarp {
    l_size: usize,
    r_size: usize,
    list: Vec<Vec<usize>>,
    seen: Vec<bool>,
    matched: Vec<bool>,
    level: Vec<isize>,
    matching: Vec<usize>,
}
impl HopcroftKarp {
    fn new(l: usize, r: usize) -> Self {
        HopcroftKarp {
            l_size: l,
            r_size: r,
            list: vec![Vec::new(); l],
            seen: vec![false; l],
            matched: vec![false; l],
            level: vec![-1; l + 1],
            matching: vec![l; r],
        }
    }
    fn add_edge(&mut self, from: usize, to: usize) {
        self.list[from].push(to);
    }
    fn bfs(&mut self) {
        let mut queue = VecDeque::new();
        for left in 0..self.l_size {
            self.level[left] = -1;
            if !self.matched[left] {
                queue.push_back(left);
                self.level[left] = 0;
            }
        }
        self.level[self.l_size] = self.l_size as isize;
        while let Some(left) = queue.pop_front() {
            for i in 0..self.list[left].len() {
                let right = self.list[left][i];
                let next = self.matching[right];
                if self.level[next] == -1 {
                    self.level[next] = self.level[left] + 1;
                    queue.push_back(next);
                }
            }
        }
    }
    fn dfs(&mut self, left: usize) -> bool {
        if left == self.l_size {
            return true;
        } else if self.seen[left] {
            return false;
        }
        self.seen[left] = true;
        for i in 0..self.list[left].len() {
            let right = self.list[left][i];
            let next = self.matching[right];
            if self.level[next] > self.level[left] && self.dfs(next) {
                self.matching[right] = left;
                return true;
            }
        }
        false
    }
    fn solve(&mut self) -> usize {
        self.seen = vec![false; self.l_size];
        self.matched = vec![false; self.l_size];
        self.level = vec![-1; self.l_size + 1];
        self.matching = vec![self.l_size; self.r_size];
        let mut res = 0;
        loop {
            self.bfs();
            self.seen = vec![false; self.l_size];
            let mut finished = true;
            for left in 0..self.l_size {
                if !self.matched[left] && self.dfs(left) {
                    self.matched[left] = true;
                    res += 1;
                    finished = false;
                }
            }
            if finished {
                break;
            }
        }
        res
    }
}

fn solve() {
    let n = get!(usize);
    let d = get!([usize]);

    if n >= 24 {
        println!("0");
        return;
    }

    let mut mx = 0;
    for i in 0..1usize << 23 {
        if i.count_ones() as usize != n {
            continue;
        }

        let mut set = BTreeSet::new();
        set.insert(0);
        for j in 0..23 {
            if (i >> j) & 1 == 1 {
                set.insert(j + 1);
            }
        }

        let mut matching = HopcroftKarp::new(n + 1, 24);
        matching.add_edge(0, 0);
        let mut flag = false;
        for j in 0..n {
            if !set.contains(&d[j]) && !set.contains(&(24 - d[j])) {
                flag = true;
                break;
            }
            if d[j] == 12 {
                if set.contains(&d[j]) {
                    matching.add_edge(j + 1, d[j]);
                }
            } else {
                if set.contains(&d[j]) {
                    matching.add_edge(j + 1, d[j]);
                }
                if set.contains(&(24 - d[j])) {
                    matching.add_edge(j + 1, 24 - d[j]);
                }
            }
        }
        if flag {
            continue;
        }
        if matching.solve() < n + 1 {
            continue;
        }
        let vec = set.into_iter().collect::<Vec<_>>();
        let mut mn = std::usize::MAX;
        for j in 0..vec.len() - 1 {
            mn = min!(mn, vec[j + 1] - vec[j]);
        }
        mn = min!(mn, 24 + vec[0] - vec[vec.len() - 1]);
        mx = max!(mn, mx);
    }
    println!("{}", mx);
}