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.



Sample input:
3
7 12 8


Sample output:
4

Submission status: Time Limit Exceeded

Solution code:
import math
#import sys
#input = sys.stdin.readline

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            
#
def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))

# a = list(map(int, input().split()))

#################################################
#################################################
#################################################
#################################################



#11-

n = int(input())

a = list(map(int, input().split()))
b = [0 for i in range(24)]
ans = 0

for i in range(2**n):
    b = [0 for i in range(25)]
    b[0]=1
    b[24]=1
    for j in range(n):
        if((i>>j) & 1):
            b[24-a[j]] += 1
        else:
            b[a[j]] += 1
    minim = 24
    prev = -24
    for j in range(0,25):
        if(b[j]==1):
            minim = min(minim, j-prev)
            prev = j
        elif(b[j]>=2):
            minim = 0
            break
    ans = max(ans,minim)
    #print(b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 7,12,18
# ありうるのは、(7,17),(12,12),(8,16)
# ......AC...B...CA.......
# 7,12,16を選んだとき
# ......A_...B...C_.......
# BとCの差が4時間になる
# 17,12,8でもよい
# ......_C...B..._A.......
# 50人存在するが、時刻は0-23の24通り (ただし高橋くんは0固定なので、それ以外の人は23通り)
# N = 24人になったら鳩ノ巣理論により必ず誰かが同じ時刻になるため、答えは0
# 23人以下の場合は、全組み合わせ（2^23 = (10 ** 6) * 8）を試せば良い

N = int(input())
D = list(map(int,input().split()))
D = sorted(D,key = lambda x:min(x,24-x)) # ソートにより同時刻が固まるとループを抜けやすくなる？

if N >= 24:
  print(0)
  exit(0)
# 23桁のbit文字列で0の場合はそのまま、1の場合はひっくり返す（24-D[i]）
# []24桁のリスト(0は高橋くんがいるため初めから1人)に、人数を加算していく
# あるindexですでに人がいたらその時点で答えは0になる
# 最小値を更新していく
ans = 0

for i in range(2 ** N):
  data = [0] * 25
  data[0] = 1 # 高橋くん
  data[24] = 1 # 高橋くん
  for j in range(N):
    if (i >> j)&1:
      if data[24-D[j]] == 1:# 同時刻に既に誰かいた時点で最小値は0確定
        break
      data[24-D[j]] += 1
    else:
      if data[D[j]] == 1: # この時点で最小値は0確定
        break
      data[D[j]] += 1
  else:
    # 最小値が0でない場合
    minval = 24
    lastexist = 0
    for j in range(1,len(data)):
      if data[j]:
        if j - lastexist < minval:
          minval = j - lastexist
        lastexist = j
    if minval > ans:
      ans = minval
print(ans)

Submission status: Time Limit Exceeded

Solution code:
INF = 1001001001
N = int(input())
D = list(map(int, input().split())) + [0]

ans = 0
for i in range(1<<(N-1)):
    tmp = INF
    for j in range(N-1):
        if i>>j & 1:
            d1 = D[j]
        else:
            d1 = 24 - D[j]
        for k in range(j+1, N-1):
            if i>>k & 1:
                d2 = D[k]
            else:
                d2 = 24 - D[k]
            tmp = min(tmp, min(abs(d1-d2), 24-abs(d1-d2)))
        tmp = min(tmp, min(abs(d1-D[N-1]), 24-abs(d1-D[N-1])))
        tmp = min(tmp, min(d1, 24-d1))
    tmp = min(tmp, min(D[N-1], 24-D[N-1]))
    ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
D=[int(i) for i in input().split()]
if n>=24:
  print(0)
else:
  ans=0
  for i in range(2**(n-1)):
    C=[1]+[0]*23+[1]
    C[D[0]]+=1
    for j in range(n-1):
      if i&(1<<j):
        C[24-D[j+1]]+=1
      else:
        C[D[j+1]]+=1
    cur=24
    if max(C)==1:
      t=0
      for j in range(1,25):
        if C[j]:
          cur=min(cur,t+1)
          t=0
          if not cur:
            break
        else:
          t+=1
    else:
      cur=0
    ans=max(ans,cur)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
D=[int(i) for i in input().split()]
if n>=24:
  print(0)
else:
  ans=0
  for i in range(2**(n-1)):
    C=[0]*25
    C[0]=1
    C[24]=1
    C[D[0]]+=1
    for j in range(n-1):
      if i&(1<<j):
        C[24-D[j+1]]+=1
      else:
        C[D[j+1]]+=1
    cur=24
    if max(C)==1:
      t=0
      for j in range(1,25):
        if C[j]==1:
          cur=min(cur,t+1)
          t=0
        else:
          t+=1
    else:
      cur=0
    ans=max(ans,cur)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
D=[int(i) for i in input().split()]
if n>=24:
  print(0)
else:
  ans=0
  for i in range(2**(n-1)):
    C=[0,D[0],24]
    for j in range(n-1):
      if i&(1<<j):
        C.append(24-D[j+1])
      else:
        C.append(D[j+1])
    C.sort()
    cur=24
    for j in range(n+1):
      cur=min(cur,C[j+1]-C[j])
    ans=max(ans,cur)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
D = [int(i) for i in input().split()]

C = collections.Counter(D)
if C.most_common()[0][1] >= 3:
    print(0)
    exit()


ans = 0
for i in range(2 ** N):
    D0 = [0, 24]
    for j in range(N):  # このループが一番のポイント
        if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
            D0.append(24-D[j])
        else:
            D0.append(D[j])

    D0 = sorted(D0)
    c = 24
    for i in range(N+1):
        c = min(c, D0[i+1]-D0[i])
    ans = max(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
N = int(input())
D = [int(i) for i in input().split()]

C = collections.Counter(D)
if C.most_common()[0][1] >= 3:
    print(0)
    exit()


ans = 0
for i in range(2 ** N):
    D0 = list((0, 24))
    for j in range(N):  # このループが一番のポイント
        if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
            D0.append(24-D[j])
        else:
            D0.append(D[j])

    D0 = np.sort(np.array(D0))
    c = np.amin(np.diff(D0))
    ans = max(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
N = int(input())
D = [int(i) for i in input().split()]+[0, 24]

C = collections.Counter(D)
if C.most_common()[0][1] >= 3:
    print(0)
    exit()


ans = 0
for i in range(2 ** N):
    D0 = D
    for j in range(N):  # このループが一番のポイント
        if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
            D0[j] = 24-D0[j]

        D0 = np.sort(np.array(D0))
        c = np.amin(np.diff(D0))
    ans = max(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
N = int(input())
D = [int(i) for i in input().split()]+[0, 24]

C = collections.Counter(D)
if C.most_common()[0][1] >= 3:
    print(0)
    exit()

ans = 0
for i in range(2 ** N):
    D0 = D
    for j in range(N):  # このループが一番のポイント
        if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
            D0[j] = 24-D0[j]
        D0 = sorted(D0)
        c = 24
        for k in range(N+1):
            c = min(c, D0[k+1]-D0[k])
    ans = max(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

from itertools import product

N = I()
D = III()

if N>=24:
    print(0)
elif 0 in D:
    print(0)
else:
    a = list(product([0,1],repeat=N))
    ans = 0
    for a0 in a:
        l = [False]*24
        flag = False
        for i in range(N):
            if a0[i]==0:
                if l[D[i]]:
                    flag = True
                    break
                else:
                    l[D[i]] = True
            else:
                if l[24-D[i]]:
                    flag = True
                    break
                else:
                    l[24-D[i]] = True
        if flag:
            continue
        before = 0
        val = 13
        for i in range(24):
            if l[i]:
                temp = min(i-before, 24-i+before)
                before = i
                if temp<val:
                    val = temp
        temp = min(before,24-before)
        if temp<val:
            val = temp
        if val>ans:
            ans = val
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

from itertools import product

N = I()
D = III()

if N>=25:
    print(0)
elif 0 in D:
    print(0)
else:
    a = list(product([0,1],repeat=N))
    ans = 0
    for a0 in a:
        l = [False]*24
        flag = False
        for i in range(N):
            if a0[i]==0:
                if l[D[i]]:
                    flag = True
                    break
                else:
                    l[D[i]] = True
            else:
                if l[24-D[i]]:
                    flag = True
                    break
                else:
                    l[24-D[i]] = True
        if flag:
            continue
        before = 0
        val = 13
        for i in range(24):
            if l[i]:
                temp = min(i-before, 24-i+before)
                before = i
                if temp<val:
                    val = temp
        temp = min(before,24-before)
        if temp<val:
            val = temp
        if val>ans:
            ans = val
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
if n==23:
    print(1)
op=2**n-1
ans=0
for i in range(op):
    t=[0]
    for j in range(n):
        if i&(2**j)>0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1,1+n):
        for q in range(p):
            b=abs(t[p]-t[q])
            a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
op=2**n-1
ans=0
for i in range(op):
    t=[0]
    for j in range(n):
        if i&(2**j)>0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1,1+n):
        for q in range(p):
            b=abs(t[p]-t[q])
            a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
op=[[0],[1]]
for i in range(n-1):
    tmp=[]
    for j in op:
        tmp.append(j+[0])
        tmp.append(j+[1])
    op=tmp
ans=0
for i in op:
    t=[0]
    for j in range(n):
        if i[j]==0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1,1+n):
        for q in range(p):
            b=abs(t[p]-t[q])
            a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=list(map(int,input().split()))
if n>=24:
    print(0)
    exit()
op=[[0],[1]]
for i in range(n-1):
    tmp=[]
    for j in op:
        tmp.append(j+[0])
        tmp.append(j+[1])
    op=tmp
ans=0
for i in op:
    t=[0]
    for j in range(n):
        if i[j]==0:
            t.append(d[j])
        else:
            t.append(24-d[j])
    a=10000
    for p in range(1+n):
        for q in range(1+n):
            if p>q:
                b=abs(t[p]-t[q])
                a=min(a,min(b,24-b))
    ans=max(ans,a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()
for i in range(13):
    if D.count(i) >= 3:
        print(0)
        exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 23:
    print(0)
    exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 24:
    print(0)
    exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        x = D[cur]
        dfs(cur+1, times + [x])
        dfs(cur+1, times + [24-x])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))
if N > 24:
    print(0)
    exit()

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = (arr[1:] - arr[:-1]).min()
        if result > answer:
            answer = result
    else:
        dfs(cur+1, times + [D[cur]])
        dfs(cur+1, times + [24-D[cur]])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
D = list(map(int, input().split()))

# 深さ優先探索で全通りを探索できるのでは curは０からスタート
def dfs(cur, times):
    global answer
    if cur == N:
        times.sort()
        arr = np.array(times)
        result = min(arr[1:] - arr[:-1])
        if result > answer:
            answer = result
    else:
        dfs(cur+1, times + [D[cur]])
        dfs(cur+1, times + [24-D[cur]])

answer = 0
dfs(0, [0, 24])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
import itertools

N = int(input())
D = list(map(int, input().split()))
M = Counter(D)
if max(M.values()) > 2:
    print(0)
elif 0 in D:
    print(0)
else:
    D = [[x, 24-x] for x in D]
    generator = list(itertools.product([0, 1], repeat=N))
    D_gen = [[D[i][g[i]] for i in range(N)] for g in generator]
    ans = 0
    for d in D_gen:
        if len(set(d)) < N:
            pass
        else:
            tmp = [0] + sorted(d) + [24]
            s = 13
            for i in range(N+1):
                s = min(s, tmp[i+1]-tmp[i])
            ans = max(ans, s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
import itertools

N = int(input())
D = list(map(int, input().split()))
M = Counter(D)
if max(M.values()) > 2:
    print(0)
elif 0 in D:
    print(0)
else:
    D = [[x, 24-x] for x in D]
    generator = list(itertools.product([0, 1], repeat=N))
    D_gen = [[D[i][g[i]] for i in range(N)] for g in generator]
    ans = 0
    for d in D_gen:
        tmp = [0] + sorted(d) + [24]
        s = 13
        for i in range(N+1):
            s = min(s, tmp[i+1]-tmp[i])
        ans = max(ans, s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter 
import itertools

N = int(input())
D = list(map(int, input().split()))
M = Counter(D)
if max(M.values()) > 2:
    print(0)
else:
    D = [[x, 24-x] for x in D]
    generator = list(itertools.product([0, 1], repeat=N))
    D_gen = [[D[i][g[i]] for i in range(N)] for g in generator]
    ans = 0
    for d in D_gen:
        tmp = [0] + sorted(d) + [24]
        s = 13
        for i in range(N+1):
            s = min(s, tmp[i+1]-tmp[i])
        ans = max(ans, s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = [int(i) for i in input().split()]
ans = []
for i in range(2 ** (n - 1)):
    dd = [0]
    k = (n - len(bin(i)[2:])) * "0" + bin(i)[2:]
    for j, kk in enumerate(k):
        if kk == "1":
            dd.append(24 - d[j])
        else:
            dd.append(d[j])
    dd.sort()
    ddd = [min(abs(dd[i + 1] - dd[i]), abs(24 - dd[i + 1] + dd[i])) for i in range(n)]
    ddd.append(min(abs(dd[-1] - dd[0]), abs(24 - dd[-1] + dd[0])))
    ans.append(min(ddd))
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = [int(i) for i in input().split()]
ans = []
for i in range(2 ** n):
    dd = [0]
    k = (n - len(bin(i)[2:])) * "0" + bin(i)[2:]
    for j, kk in enumerate(k):
        if kk == "1":
            dd.append(24 - d[j])
        else:
            dd.append(d[j])
    dd.sort()
    ddd = [min(abs(dd[i + 1] - dd[i]), abs(24 - dd[i + 1] + dd[i])) for i in range(n)]
    ddd.append(min(abs(dd[-1] - dd[0]), abs(24 - dd[-1] + dd[0])))
    ans.append(min(ddd))
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int, input().split()))

if N > 24:
    print(0)
else:
    ans = 0
    for i in range(2 ** N):
        bit = bin(i)[2:].zfill(N)
        D_mod24 = [0] * N
        for j in range(N):
            if bit[j] == '0':
                D_mod24[j] = D[j]
            else:
                D_mod24[j] = 24 - D[j]
        D_mod24.sort()
        ans_temp = min(min(D_mod24), 24 - max(D_mod24))
        for j in range(N-1):
            ans_temp = min(ans_temp, D_mod24[j+1] - D_mod24[j])
        ans = max(ans, ans_temp)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product, combinations

# 入力
N = int(input())
D = list(map(int, input().split()))

# N >=23 ならば鳩の巣原理より、max(s)=0である
# N <=22 ならば全探索で解を求める
ans = (
    0 if N >= 24 else
    max(
        int(len(set(t)) == N + 1) if N >= 12 else
        min(
            min(abs(a - b), 24 - abs(a - b))
            for a, b in combinations(t, 2)
        )
        for t in (
            product(
                (0,),
                *(
                    (d,) if d == 0 else
                    (d, 24 - d)
                    for d in D
                )
            )
        )
    )
)

# 出力
print(ans)

Submission status: Accepted

Solution code:

def ans_is_zero():
    print(0)
    exit(0)

def solve():
    n=int(input())
    if n > 23: ans_is_zero()

    d=list(map(int,input().split()))
    for num in d:
        if num == 0: ans_is_zero()

    ans=0
    d.sort()

    N = 1 << n
    for i in range(N):#left or right
        t=1
        ret = 100

        for j in range(n):
            if (i>>j)&1: num = d[j]
            else: num = 24 - d[j]

            if (t >> num)&1:
                ret = -1
                break
            t |= (1 << num)

        if ret == -1: continue
        prev = 0

        for j in range(1, 24):
            if ((t>>j)&1) == 0: continue
            ret = min(ret, j - prev)
            prev = j

        ret = min(ret, 24-prev)
        ans = max(ans, ret)
    print(ans)
solve()

Submission status: Accepted

Solution code:
import random

def ans_is_zero():
    print(0)
    exit(0)

def solve():
    n=int(input())
    if n > 23: ans_is_zero()

    d=list(map(int,input().split()))
    for num in d:
        if num == 0: ans_is_zero()

    ans=0
    random.shuffle(d)
    
    N = 1 << n
    for i in range(N):#left or right
        t=1
        ret = 100
        for j in range(n):
            if (i>>j)&1: num = d[j]
            else: num = 24 - d[j]

            if (t >> num)&1:
                ret = -1
                break
            t |= (1 << num)

        if ret == -1:
            continue

        if bin(t).count("1") < n + 1:#重複あり
            continue

        prev = 0
        for j in range(1, 24):
            if ((t>>j)&1) == 0: continue
            ret = min(ret, j - prev)
            prev = j

        ret = min(ret, 24-prev)
        ans = max(ans, ret)
    print(ans)
solve()

Submission status: Accepted

Solution code:
def ans_is_zero():
    print(0)
    exit(0)

def solve():
    n=int(input())
    if n > 23: ans_is_zero()

    d=list(map(int,input().split()))
    for num in d:
        if num == 0: ans_is_zero()

    ans=0

    N = 1 << n
    for i in range(N):#left or right
        t=1
        ret = 100
        for j in range(n):
            if (i>>j)&1: num = d[j]
            else: num = 24 - d[j]

            if (t >> num)&1:
                ret = -1
                break
            t |= (1 << num)
        
        if ret == -1:
            continue

        if bin(t).count("1") < n + 1:#重複あり
            continue

        prev = 0
        for j in range(1, 24):
            if ((t>>j)&1) == 0: continue
            ret = min(ret, j - prev)
            prev = j

        ret = min(ret, 24-prev)
        ans = max(ans, ret)
    print(ans)
solve()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
n=int(input())
from collections import Counter as co
ds=co(list(map(int,input().split() ))+[0]).most_common()
if ds[0][1]>=3:print(0)
else:
    nows=[]
    nexts=[]
    for k,v in ds:
        if v==1:nexts.append(k)
        else:
            nows.append(-k)
            nows.append(k)
    def f(k):
        res=24
        for i in range(len(k)-1):
            for j in range(i+1,len(k)):
                #x y  0 y-x 
                res=min(res,-abs(k[i]-k[j])%24 ,abs(k[i]-k[j])%24)
        return res
    from itertools import product as pr
    print(max(f(nows+[v*j  for v,j in zip(nexts,i)])for i in pr((1,-1),repeat=len(ds))))

Submission status: Accepted

Solution code:
def main():
    import itertools
    n = int(input())
    d = list(map(int,input().split()))
    d.sort()
    ans = [0]
    for i in range(n):
        if d[i]==0:
            ans.append(0)
        elif i%2==0:
            ans.append(24-d[i])
        else:
            ans.append(d[i])
    ans.sort()
    A = float('inf')
    for i in range(1,len(ans)):
        if A>min(ans[i]-ans[i-1],24-(ans[i]-ans[i-1])):
            A = min(ans[i]-ans[i-1],24-(ans[i]-ans[i-1]))
    if min(ans[-1]-ans[0],24-(ans[-1]-ans[0]))<A:
        A = min(ans[-1]-ans[0],24-(ans[-1]-ans[0]))
    print(A)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
d.sort()
t=[0]
for i,x in enumerate(d):
    if x==0:t.append(0)
    elif i%2:t.append(24-x)
    else:t.append(x)
t.sort()
ans=12
for i in range(1,n+1):
    ans=min(ans,t[i]-t[i-1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input()) + 1
d = [0] + list(map(int, input().split()))
ans = 0
def dfs(time_set,index):
    # print("{} {}".format(time_set, index))
    if len(time_set) >= n:
        time_list = list(time_set)
        time_list.sort()
        cand = min(time_list[-1] - time_list[0], 24 + time_list[0] - time_list[-1])
        global ans
        for i in range(1,n):
            cand = min(cand, time_list[i] - time_list[i-1])
        ans = max(ans, cand)
    elif index < n:
        time1, time2 = d[index], 24 - d[index]
        if time1 not in time_set:
            dfs(time_set | {time1}, index+1)
        if time2 not in time_set:
            dfs(time_set | {time2}, index+1)
dfs(set(),0)
print(ans)

Submission status: Accepted

Solution code:
import collections
from collections import deque
N=int(input())
D=list(map(int,input().split()))
c=collections.Counter(D)
count0=1
count12=0
zeroflag=0
l=[]
if c[12]>0:
    count12=c[12]
if c[0]>0:
    count0=count0+c[0]
#print(l,count0,count12)
#print(n)
allmax=0
q=deque()
l=[]
if (count0==1)and(zeroflag==0):
    q.append([0])
else:
    zeroflag=1

if (count12==1)and(zeroflag==0):
    length=len(q)
    j=0
    while j<length:
        buf1=[]
        l=q.popleft()
        buf1.extend(l)
        l.append(12)
        q.append(l)
        j=j+1
elif count12>1:
    zeroflag=1

if zeroflag==0:
    for i in range(1,12):
        length=len(q)
        j=0
        if c[i]==0: continue
        if c[i]==1:
            while j<length:
                buf1=[]
                buf2=[]
                l=q.popleft()
                buf1.extend(l)
                buf1.append(i)
                buf2.extend(l)
                buf2.append(24-i)
                q.append(buf1)
                q.append(buf2)
                j=j+1
        if c[i]==2:
            while j<length:
                buf3=[]
                l=q.popleft()
                buf3.extend(l)
                buf3.extend([i,24-i])
                q.append(buf3)
                j=j+1
        if c[i]>2:
            zeroflag=1
            break
        #print(q)
#print(q)

if zeroflag==0:
    for l in q:
        l.sort()
        mi=100
        n=len(l)
        for j in range(n-1):
            mi=min(mi,l[j+1]-l[j],24-l[j+1]+l[j])
        mi=min(mi,l[n-1],24-l[n-1])
        #print(mi)
        allmax=max(allmax,mi)
else:
    allmax=0
print(allmax)

Submission status: Accepted

Solution code:
import collections
from collections import deque
N=int(input())
D=list(map(int,input().split()))
c=collections.Counter(D)
count0=0
count12=0
zeroflag=0
l=[]
if c[12]>0:
    count12=c[12]
if c[0]>0:
    count0=c[0]+1
#print(l,count0,count12)
#print(n)
allmax=0
q=deque()
l=[]



if (count0==0)and(zeroflag==0):
    q.append([0])
else:
    zeroflag=1

if (count12==1)and(zeroflag==0):
    length=len(q)
    j=0
    while j<length:
        buf1=[]
        l=q.popleft()
        buf1.extend(l)
        l.append(12)
        q.append(l)
        j=j+1
elif count12>1:
    zeroflag=1

if zeroflag==0:
    for i in range(1,12):
        length=len(q)
        j=0
        if c[i]==0: continue
        if c[i]==1:
            while j<length:
                buf1=[]
                buf2=[]
                l=q.popleft()
                buf1.extend(l)
                buf1.append(i)
                buf2.extend(l)
                buf2.append(24-i)
                q.append(buf1)
                q.append(buf2)
                j=j+1
        if c[i]==2:
            while j<length:
                buf1=[]
                buf2=[]
                buf3=[]
                l=q.popleft()
                #buf1.extend(l)
                #buf1.extend([i,i])
                #buf2.extend(l)
                #buf2.extend([24-i,24-i])
                buf3.extend(l)
                buf3.extend([i,24-i])
                #q.append(buf1)
                #q.append(buf2)
                q.append(buf3)
                j=j+1
        if c[i]>2:
            zeroflag=1
            break
        #print(q)

#print(q)

if zeroflag==0:
    for l in q:
        l.sort()
        mi=100
        n=len(l)
        for j in range(n-1):
            mi=min(mi,l[j+1]-l[j],24-l[j+1]+l[j])
        mi=min(mi,l[n-1],24-l[n-1])
    #print(mi)
        allmax=max(allmax,mi)
else:
    allmax=0
print(allmax)