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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(defun map-run-length-rotative (function seq &key (test #'eql))
  (declare (sequence seq)
           (function test function))
  (etypecase seq
    (vector
     (unless (zerop (length seq))
       (let ((prev (aref seq 0))
             (start 0)
             (init-elm (aref seq 0))
             (init-length 0))
         (declare ((integer 0 #.most-positive-fixnum) start init-length))
         (loop for pos from 1 below (length seq)
               unless (funcall test prev (aref seq pos))
               do (if (zerop init-length)
                      (setf init-length (- pos start))
                      (funcall function prev (- pos start)))
                  (setf prev (aref seq pos)
                        start pos)
               finally (if (funcall test init-elm (aref seq (- pos 1)))
                           (funcall function init-elm (+ init-length (- pos start)))
                           (progn
                             (funcall function prev (- pos start))
                             (funcall function init-elm init-length)))))))
    (list
     (error "Not implemented."))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defun calc (table)
  ;; (format t "~A~%" table)
  (let ((res #xffff))
    (declare (uint32 res))
    (map-run-length-rotative
     (lambda (x num)
       (declare (uint32 x num))
       (if (zerop x)
           (setq res (min res (+ num 1)))
           (when (> num 1)
             (setq res 1))))
     table)
    res))

(defun main ()
  (let* ((n (read))
         (counts (make-array 13 :element-type 'uint8 :initial-element 0))
         (res 0))
    (declare (uint8 res))
    (dotimes (i n) (incf (aref counts (read))))
    (incf (aref counts 0)) ; Takahashi-kun
    (when (>= (aref counts 0) 2)
      (println 0)
      (return-from main))
    (when (>= (aref counts 12) 2)
      (println 0)
      (return-from main))
    (loop for d from 1 to 11
          when (> (aref counts d) 2)
          do (println 0)
             (return-from main))
    (dotimes (bits (ash 1 11))
      (let ((table (make-array 24 :element-type 'bit :initial-element 0)))
        (declare (dynamic-extent table))
        (when (= (aref counts 0) 1)
          (setf (aref table 0) 1))
        (when (= (aref counts 12) 1)
          (setf (aref table 12) 1))
        (loop for d from 1 to 11
              when (= (aref counts d) 2)
              do (setf (aref table d) 1
                       (aref table (- 24 d)) 1))
        (loop for d from 1 to 11
              when (= (aref counts d) 1)
              do (if (logbitp (- d 1) bits)
                     (setf (aref table d) 1)
                     (setf (aref table (- 24 d)) 1)))
        (setf res (max res (calc table)))))
    (println res)))

#-swank (main)