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: Accepted

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

D.append(0)
D.sort()
ans = 0
time1 = []
time2 = []
for i in range(N+1):
    if D.count(D[i]) == 2:
        if D[i] == 0:
            ans = -1
            break
        time2.append(D[i])
        time2.append(24-D[i])
    if D.count(D[i]) + D.count(24-D[i]) > 2:
        ans = -1
        break
    if D.count(D[i]) == 1:
        time1.append(D[i])

time2 = set(time2)    
if ans == 0:
    for i in range(2**len(time1)):
        temp = []
        for j in range(len(time1)):
            if i & 1<<j:
                temp.append(24-time1[j])
            else:
                temp.append(time1[j])
        temp += list(time2)
        temp.sort()
        cnt = 24-temp[-1]
        if temp[-1] == 0:
            cnt = 0
        for k in range(len(temp)-1):
            cnt = min(cnt, temp[k+1]-temp[k])
        ans = max(ans, cnt)
else:
    ans = 0

print(ans)

Submission status: Accepted

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

from collections import Counter
C = Counter(D)

ans = 0
import heapq
for pattern in range(1<<11):
    if C[0] >= 1: continue
    if C[12] >= 2: continue
    if C.most_common(1)[0][1] >= 3: continue
    q = [24]
    for d in range(1,12):
        if C[d] == 1:
            if pattern & 1: heapq.heappush(q, d)
            else: heapq.heappush(q, 24-d)
        elif C[d] == 2:
            heapq.heappush(q, d)
            heapq.heappush(q, 24-d)
        pattern>>=1
    if C[12] == 1: heapq.heappush(q, 12)
    pre = 0
    tmp = 24
    for _ in range(N+1):
        d = heapq.heappop(q)
        tmp = min(tmp, d-pre)
        pre = d
    ans = max(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
D = list(map(int,input().split()))
dc = Counter(D)
flag = True
count = 0
cand = [0]
one = []
if 0 in dc:
    flag = False
if 12 in dc:
    if dc[12] >= 2:
        flag = False
    else:
        cand.append(12)
for i in range(1,12):
    if i not in dc:
        continue
    if dc[i] == 1:
        count += 1
        one.append(i)
    elif dc[i] >= 3:
        flag = False
        break
    else:
        cand += [24-i,i]
if flag:
    if count:
        ans = 0
        for i in range(2**count):
            candone = []
            for j in range(count):
                if (i >> j) & 1 == 1:
                    candone.append(one[j])
                else:
                    candone.append(24-one[j])
            cand2 = cand+candone
            cand2.sort()
            ans1 = min(cand2[-1]-cand2[0],24-cand2[-1]+cand2[0])
            for i in range(1,len(cand2)):
                ans1 = min(ans1,cand2[i]-cand2[i-1])
            ans = max(ans,ans1)
        print(ans)
    else:
        cand.sort()
        if len(cand) == 1:
            print(0)
        else:
            ans = 24
            for i in range(1,len(cand)):
                ans = min(ans,cand[i]-cand[i-1])
            print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
import itertools
import copy

def solve(N: int, D: "List[int]"):
    counter = dict(Counter(D+[0]))
    time = [False]*25
    once = []
    # 枝狩り処理
    for i in range(13): 
        if counter.get(i) == None:
            continue
        if counter[i] == 2:
            if i == 0:
                print(0)
                return
            time[12+i] = True
            time[12-i] = True
        elif counter[i] >= 3: #どう頑張っても時刻のさの最大値は0
            print(0)
            return
        else:
            # if i != 0:
            once.append(i)

    LEN = len(once)
    bit_list = list(itertools.product([0, 1], repeat=LEN))

    if LEN == 0:
        min_dif = 12        
        time = [i for i,t in enumerate(time) if t]
        for first,second in list(itertools.combinations(time,2)):
            min_dif = min(min_dif,min(abs(first-second),24-abs(first-second)))
        print(min_dif)
        return

    answer = 0

    # onceを進んでるか戻ってるかのどちらかに振り分けていく
    for bit in bit_list:
        tmp = copy.deepcopy(time)
        
        for i in range(LEN): # 1なら進んでる0なら戻ってる
            if bit[i] == 1:
                tmp[12+once[i]] = True
            else:
                tmp[12-once[i]] = True

        # Trueのものだけ抜き出す
        tmp = [i for i,t in enumerate(tmp) if t]
        min_dif = 12
        for first,second in list(itertools.combinations(tmp,2)):
            min_dif = min(min_dif,min(abs(first-second),24-abs(first-second)))
        answer = max(min_dif,answer)

    print(answer)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, D)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
d = list(map(int, input().split()))
d.sort()
ans = min(d)
for i in range(n):
    if i % 2 == 0:
        d[i] = 24-d[i]


for i in range(n):
    for j in range(n):
        if i != j:
            ans = min(ans, abs(d[i]-d[j]))

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
from collections import deque

def main():
    N = int(input())
    d = list(map(int,input().split()))
    
    d.sort()
    now = 1
    time = [0]
    for num in d:
        if now == 1:
            time.append(num)
        else:
            time.append(24-num)
        now *= -1
    
    time.sort()
    ans = 24-time[-1]
    for i in range(N):
        ans = min(ans,time[i+1]-time[i])
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
cnt=[0]*24
cnt[0]=1
for i in range(n):
  cnt[d[i]]+=1
  cnt[(24-d[i])%24]+=1
  if cnt[d[i]]>=3 or cnt[24-d[i]]>=3:
    print(0)
    exit()
ans=[0]*24
ans[0]=1
right=True
for i in range(1,13):
  if cnt[i]>=2:
    ans[i]=1
    ans[24-i]=1
  elif cnt[i]==0:
    continue
  else:
    if right:
      right=False
      ans[i]=1
    else:
      right=True
      ans[24-i]=1
L=24
num=0
for i in range(1,24):
  if ans[i]==0:
    num+=1
  else:
    L=min(L,num)
    num=0
L=min(L,num)
print(L+1)

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import product
import numpy as np


def timeGap(D):
    day = []
    candidate = []
    c = Counter(D)

    for k, v in c.items():
        # 0, 12は2つある時点で答えは0
        if k == 0 or k == 12:
            if v >= 2:
                return 0
            else:
                day.append(k)
            continue

        # 1~11も3以上ある時点で答えは0
        if v >= 3:
            return 0
        # 2つの場合は反対にして配置
        elif v == 2:
            day.append(k)
            day.append(24-k)
        else:
            candidate.append(k)

    # candidate は10個以下なので、全探索
    res = 0
    n = len(candidate)
    for pair in product(range(2), repeat=n):
        tmp = day[::]
        for i in range(n):
            if pair[i]:
                tmp.append(candidate[i])
            else:
                tmp.append(24 - candidate[i])
        tmp.sort()
        tmp.append(tmp[0]+24)
        res = max(res, np.diff(tmp).min())
    return res


if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    ans = timeGap(D)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
D=[int(i) for i in input().split()]
C=[1]+[0]*12

for d in D:
  C[min(d,24-d)]+=1
if max(C)>2 or C[0]>1 or C[12]>1:
  print(0)
else:
  ans=0
  
  for i in range(2**(11)):
    CC=[1]+[0]*23+[1]
    for j in range(1,12):
      if C[j]==2:
        CC[j]=1
        CC[24-j]=1
      elif C[j]==1:
        if i&(1<<(j-1)):
          CC[j]=1
        else:
          CC[24-j]=1
    if C[12]==1:
      CC[12]=1
    cur=24
    t=0
    for j in range(1,25):
      if CC[j]==1:
        cur=min(cur,t+1)
        t=0
      else:
        t+=1
    ans=max(ans,cur)
  print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
input = sys.stdin.readline


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


def calc(F):    
    res = 0
    for f in F:
        S = format(f, "024b")[22::-1]
        count = 12
        tmp = 1
        for s in S:
            if s == "1":
                count = min(count, tmp)
                tmp = 1
            else:
                tmp += 1
        count = min(count, tmp)
        res = max(res, count)
    
    return res


def g(D, F):
    if len(D) == 0:
        return calc(F)

    d = D[0]
    s = set()
    for f in F:
        if (f >> d) & 1 == 0:
            s.add(f + 2 ** d)
        
        d = 24 - d
        if (f >> d) & 1 == 0:
            s.add(f + 2 ** d)
    
    return g(D[1:], s)
    

def main():
    N = int(input())

    D = read_list()
    if D.count(0) > 0:
        print(0)
        return
    F = {1,}    
    print(g(D, F)) 
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = input()
D_list = list(map(int, input().split()))
lis = sorted(D_list)
lis_plus = lis[::2]
lis_minus = lis[1::2]
lis_plus_all = [0] + lis_plus
lis_minus_all = [0] + lis_minus

if lis_plus:
    lis_minus_all += [24 - lis_plus[-1]]
if lis_minus:
    lis_plus_all += [24 - lis_minus[-1]]

gaps_plus = [lis_plus_all[n + 1] - lis_plus_all[n] for n in range(len(lis_plus_all) - 1)] + [24]
gaps_minus = [lis_minus_all[n + 1] - lis_minus_all[n] for n in range(len(lis_minus_all) - 1)] + [24]

print(min(min(gaps_plus), min(gaps_minus)))

Submission status: Accepted

Solution code:
import sys
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False

def ddprint(x):
  if DBG:
    print(x)

n = inn()
d = inl()
a = [0]*13
for i in range(n):
    a[d[i]] += 1
    if d[i]==0 or d[i]==12 and a[12]>=2 or a[d[i]]>=3:
        print(0)
        exit()
if n>=13:
    print(1)
    exit()
ddprint('a')
ddprint(a)
mx = -1
for i in range(2**n):
    s = [(i>>j)%2 for j in range(n)]
    e = [0]*25
    e[0] = 1
    for j in range(n):
        suf = (d[j] if s[j]==0 else 24-d[j])
        e[suf] += 1
    ddprint('s,e')
    ddprint(s)
    ddprint(e)
    mn = 25
    strk = 0
    for j in range(1,24):
        if e[j]>=2:
            mn = 0
            break
        elif e[j]==1:
            mn = min(mn, strk+1)
            strk = 0
        else:
            strk += 1
    ddprint(mn)
    mn = min(mn, strk+1)
    mx = max(mx,mn)
print(mx)

Submission status: Accepted

Solution code:
n = int(input())
D = [0] + sorted(list(map(int, input().split())))
E = [D[i] if i%2 else 24-D[i] for i in range(n+1)]
ans = float("inf")
for i in range(n):
  for j in range(i+1, n+1):
    k = (E[i]-E[j])%24
    ans = min(ans, min(k, 24-k))
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from itertools import combinations

def distance(i, j):
	return min(abs(j - i), abs(24-j+i))

def main():
	n = int(input().strip())
	v = sorted(list(map(int, input().strip().split())))
	t = [0] * n

	for i in range(n):
		d = v[i]
		if i % 2 == 0:
			t[i] = d
		else:
			t[i] = 24 - d
	cmin = min(v) #chokudaiとの時差
	for z in combinations(range(n), 2):
		i, j = z[0], z[1]
		cmin = min(cmin, distance(t[i], t[j]))
	print(cmin)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def dif(x,y):
    if x<y:
        x,y = y,x
    return min(x-y,24-(x-y))
from itertools import combinations
N = int(input())
D = sorted(list(map(int,input().split())))
T = [0 for _ in range(N)]
for i in range(N):
    d = D[i]
    if i%2==0:
        T[i] = d
    else:
        T[i] = 24-d
cmin = min(D)
for z in combinations(range(N),2):
    i,j = z[0],z[1]
    cmin = min(cmin,dif(T[i],T[j]))
print(cmin)

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
d.sort()
time=[0]*24
time[0]=1
for i in range(n):
  if i%2:
    time[d[i]]+=1
  else:
    time[-d[i]]+=1
if max(time)>1:
  print(0)
  exit()
ans=[]
cnt=1
for i in range(1,25):
  i=i%24
  if time[i]==0:
    cnt+=1
  else:
    ans.append(cnt)
    cnt=1
print(min(ans))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
exist=[0 for i in range(13)]
for i in l:
  exist[i]+=1
if exist[0]>=1 or exist[-1]>=2 or max(exist)>=3:print(0)
else:
  single=[]
  stand=[]
  if exist[-1]==1:single.append(12)
  for i in range(1,12):
    if exist[i]==1:
      single.append(i)
    if exist[i]==2:
      stand.append(i)
      stand.append(24-i)
  m=len(single)
  def getstatus(num):
    L=[]
    for i in range(m)[::-1]:
      if num//(2**i)==1:
        L.append(single[i])
      else:
        L.append(24-single[i])
      num%=2**i
    return L
  def dif(LIST):
    ret=100
    for i in range(len(LIST)-1):
      if LIST[i+1]-LIST[i]<ret:ret=LIST[i+1]-LIST[i]
    return ret
  ans=0
  for i in range(2**m):
    if dif(sorted([0]+getstatus(i)+stand+[24]))>ans:ans=dif(sorted([0]+getstatus(i)+stand+[24]))
  print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
D=list(map(int,input().split()))
D.sort();
for i in range(n-1,-1,-1):
    D.append(24-D[i])
# 各々が持つ変化の可能性は２通り
# 貪欲じゃあ無理


def solve_binary(mid):
    now=0; count=0
    used=[0]*(2*n)
    for i,di in enumerate(D):
        if not used[i] and now+mid<=di:
            now=di; count+=1
            used[i]=used[2*n-1-i]=1
    return count==n and now+mid<=24

def binary_search(n):
    ok=0
    ng=n
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve_binary(mid):
            ok=mid
        else:
            ng=mid
    return ok


print(binary_search(24))
# print(D)
# print(solve_binary(0))
# print(solve_binary(12))

Submission status: Accepted

Solution code:
#n>=25なら鳩ノ巣で0
#2人以上居るdは一意
#あとはしらみつぶし

n = int(input())
if n>=25:
    print(0)
    exit()

d = list(map(int, input().split( )))
if 0 in d:##
    print(0)
    exit()
d.append(0)
dic = {}

for di in d:
    if di in dic:
        dic[di] += 1
    else:
        dic[di]= 1
solo = []
times = []
for di in dic:
    if dic[di]==1:#di=0はここに入る
        solo.append(di)
    elif dic[di]==2:
        times.append(di)
        if di!=12:
            times.append(24-di)
        else:
            print(0)
            exit()
    else:
        print(0)
        exit()
        
ans = 0
m = len(solo)
bit = 1<<m

for i in range(bit):
    times2 = []
    for t in times:
        times2.append(t)
    for j in range(m):
        if i>>j&1:
            times2.append(solo[j])
        else:####???
            times2.append(24-solo[j])
    times2.sort()
    dif = 24
    if len(times2)==1:###
        print(0)
        exit()
    #print(times2)

    for k in range(len(times2)-1):
        dif = min(dif,times2[k+1]-times2[k])
    dif = min(dif,times2[0] + 24 - times2[-1])
    ans = max(ans,dif)
print(ans)

Submission status: Accepted

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

D_list = [0] * (12 + 1)
for d in D:
  D_list[d] += 1
#print(D_list)
  
for d in D_list:
  if d >= 3:
    print(0)
    exit()    
    
answer = []
for n in range(2 ** 13):
  tmp = [0]
  for j in range(13):
    if D_list[j] == 0:
      continue
    if D_list[j] == 1:
      if (n >> j) & 1:
        tmp.append(j)
      else:
        tmp.append(24 - j)
    if D_list[j] == 2:
      tmp.append(j)
      tmp.append(24 - j)
  tmp.sort()
  tmp.append(tmp[0] + 24)
  #print(tmp)
  res = []
  for i in range(1, len(tmp)):
    res.append(tmp[i] - tmp[i - 1])
  answer.append(min(res))
print(max(answer))

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


n = onem()

d = l()

d.sort()

a = [0 for i in range(24)]
a[0] += 1

for i in range(n):
    if i % 2 == 0:
        a[d[i]] += 1
    else:
        if d[i]:
            a[24 - d[i]] += 1
        else:
            a[0] += 1

co = 12

for i in range(24):
    if a[i] >= 2:
        co = 0
        break
    for j in range(i+1,24):
        if a[i] >= 1 and a[j] >= 1:
            co = min(co,min(24-(j-i),j-i))
print(co)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

ans = 0
def gap(a,b):
    c = abs(a-b)
    return min(c,24-c)
def solve():
    global ans
    def dfs(f,i,k):
        global ans
        if i == n:
            ans = k
        else:
            nd = d[i]
            nk = min([gap(nd,j) for j in f])
            if nk > ans:
                nf = [i for i in f]+[nd]
                dfs(nf,i+1,min(k,nk))
            if d[i] != 12:
                nd = 24-d[i]
                nk = min([gap(nd,j) for j in f])
                if nk > ans:
                    nf = [i for i in f]+[nd]
                    dfs(nf,i+1,min(k,nk))

    n = I()
    d = LI()
    f = [0]
    dfs(f,0,float("inf"))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import bisect
n = int(input())
d = list(map(int,input().split()))
d.sort()
l = [0,24]
for i in range(n):
    if i % 2 == 0:
        l.append(24-d[i])
    else:
        l.append(d[i])
l.sort()
ans = l[1]-l[0]
for i in range(n+1):
    if l[i+1] - l[i] < ans:
        ans = l[i+1] - l[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)

res = 0
for k in range(2):
    b = [0, 24]
    tmp = 10 ** 10
    for i, j in enumerate(a):
        if i % 2 == k:
            b.append(24 - j)
        else:
            b.append(j)
    b = sorted(b)
    for i, j in zip(b, b[1:]):
        tmp = min(tmp, j - i)
    res = max(res, tmp)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
D = [int(a) for a in input().split()]
X = [0] * 13

for d in D:
    X[d] += 1
if X[0] or max(X) >= 3 or X[12] >= 2:
    print(0)
else:
    ma = 0
    for i in range(1<<13):
        Y = [0]
        for j in range(13):
            if X[j] == 2:
                Y.append(j)
                Y.append(24-j)
            elif X[j] == 1:
                if i >> j & 1:
                    Y.append(j)
                else:
                    Y.append(24-j)
        Y = sorted(Y)
        M = len(Y)
        ma = max(ma, min([abs((Y[i] - Y[i-1]) % 24) for i in range(M)]))
    print(ma)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])


def main():
    n=inp()
    a=inpl()
    a.sort()
    dic = defaultdict(int)
    dic[0] += 1
    for i in range(n):
        dic[a[i]] += 1
    for e in dic:
        if dic[e] > 2:
            print(0)
            return
        if e == 0 or e == 12:
            if dic[e]>1:
                print(0)
                return
    x = [0 for _ in range(24)]
    cnt = 0
    for i in range(13):
        if dic[i]==0:
            continue
        elif dic[i] == 1:
            if cnt % 2 == 0:
                x[i] += 1
            else:
                x[24-i] += 1
        else:
            x[i] += 1
            x[24-i] += 1
        cnt += 1
    ans = 24
    for i in range(24):
        for j in range(24):
            if i==j or x[i]==0 or x[j]==0:
                continue
            ans = min(ans,abs(i-j),24-abs(i-j))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

s = 24
D.sort()
D2 = [_ for _ in D]
for i in range(N):
  if D2[i] == D2[i + 1]:
    D2[i] = 24 - D2[i]

D2.sort()
for i in range(N):
  s = min(D2[i + 1] - D2[i], s)
s = min(24 - D2[N] + D2[0], s)

if s != 0 and N <= 12:
  for b in range(1 << N):
    D2 = [_ for _ in D]
    for i in range(N):
      if b & (1 << i):
        D2[i + 1] = 24 - D2[i + 1]
    D2.sort()
    s2 = 24
    for i in range(N):
      s2 = min(D2[i + 1] - D2[i], s2)
    s2 = min(24 - D2[N] + D[0], s2)
    s = max(s2, s)

print(s)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
D = [int(i) for i in input().split()]

def solve() :
  cnt = Counter(D)
  one = []
  L1 = []
  for k, v in cnt.items() :
    if v >= 3 :
      return 0
    if v == 2 :
      L1.append(k)
      L1.append(24 - k)
    if v == 1 :
      one.append(k)

  ret = 0
  for i in range(1 << len(one)) :
    L2 = L1[::]
    for j in range(len(one)) :
      if i & 1 :
        L2.append(one[j])
      else :
        L2.append(24 - one[j])

      i >>= 1

    L2.append(0)
    L2.append(24)
    L2.sort()
    tmp = float('inf')
    for i in range(1, len(L2)) :
      tmp = min(tmp, L2[i] -  L2[i-1])
    if tmp != float('inf') :
      ret = max(ret, tmp)

  return ret

print(solve())

Submission status: Accepted

Solution code:
from collections import Counter
import copy


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

if N >= 24:
    print (0)
    exit()

lst_time = [0]
lst_candidate = []

C = Counter(D)
for i in range(13):
    if i == 0:
        if C[0] > 0:
            print (0)
            exit()
    elif i == 12:
        if C[12] >= 2:
            print (0)
            exit()
        if C[12] == 1:
            lst_time.append(12)
    else:
        if C[i] >= 3:
            print (0)
            exit()
        elif C[i] == 2:
            lst_time.append(i)
            lst_time.append(24 - i)
        elif C[i] == 1:
            lst_candidate.append(i)

# print (lst_time)
# print (lst_candidate)

def calc(lst):
    tmp = 10 ** 9
    for i in range(len(lst)):
        for j in range(i + 1, len(lst)):
            tmp = min(tmp, min(abs(lst[i] - lst[j]), 24 - abs(lst[i] - lst[j])))
    return tmp

s = 0 #ans
n = len(lst_candidate)
for i in range(2 ** n):
    tmp_lst = copy.deepcopy(lst_time)
    for j in range(n):
        if (i >> j) & 1:
            tmp_lst.append(lst_candidate[j])
        else:
            tmp_lst.append(24 - lst_candidate[j])
    
    # print (tmp_lst)
    # tmp = calc(tmp_lst)
    s = max(s, calc(tmp_lst))

print (s)

Submission status: Accepted

Solution code:
from copy import deepcopy

N = int(input())
D = list(map(int,input().split()))
T = [0 for _ in range(13)]
ans = 0

for i in range(N):
    if D[i]==0:
        print(0)
        break
    T[D[i]] += 1
    if T[D[i]] >=3:
        print(0)
        break
        
else:
    C = [0]
    S = []
    for j in range(13):
        if T[j]==2:
            C.append(j)
            C.append(-j)
        elif T[j]==1:
            S.append(j)

    for j in range(2**len(S)):
        CC = deepcopy(C)
        for k in range(len(S)):
            if ((j >> k) & 1):
                CC.append(S[k])
            else:
                CC.append(-S[k])
                
        mind = 25
        for p in range(N):
            for q in range(p+1,N+1):
                mind = min(mind,min(abs(CC[p]-CC[q]),24-abs(CC[p]-CC[q])))
        ans = max(ans,mind)
        
    print(ans)

Submission status: Accepted

Solution code:
def examC():
    N = I(); D = LI()
    ans = -1
    choice = [0]*13; choice[0] +=1
    for i in D:
        choice[i] +=1
        if choice[i]>2:
            ans = 0
            break
    if choice[0]>=2 or choice[12]>=2:
        ans = 0
#    print(choice)
    curT = [0]; cur = []
    for i in range(1,12):
        if choice[i]==1:
            cur.append(i)
        elif choice[i]==2:
            curT.append(i)
            curT.append(24-i)
    ne = Counter(choice)
    if choice[12]==1:
        ne[1] -=1
        curT.append(12)
    loop = 2**(ne[1]-1)
#    print(loop)
    if ans==0:
        print(ans)
        exit()
    ans = 0
    for i in range(loop):
        now = copy.deepcopy(curT); ansC = 24
        for j in range(ne[1]-1):
            if i&(1<<j)==(1<<j):
                now.append(cur[j])
            else:
                now.append(24-cur[j])
        now.sort()
        for k in range(len(now)):
            if k != len(now) - 1:
                ansC = min(ansC, now[k+1]-now[k], 24-(now[k+1]-now[k]))
            else:
                ansC = min(ansC, now[k], 24-now[k])
        ans = max(ans, ansC)
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read

N, *D = map(int, read().split())

D_1 = [D.count(i) for i in range(13)]

if D_1[0] or D_1[-1] > 1:
    print(0)
    exit()

for i in D_1:
    if i > 2:
        print(0)
        exit()


d = [False] * (12 + 1 + 12)

# 高橋君
d[12] = True

rl = True
for i in range(13):
    for _ in range(D_1[i]):
        if rl:
            d[12 + i] = True
            rl = False
        else:
            d[12 - i] = True
            rl = True

a = [i for i, j in enumerate(d) if j]
a.append(24 + a[0])
s = 24
for i, j in zip(a, a[1:]):
    if j - i < s:
        s = j - i

print(s)

Submission status: Accepted

Solution code:
import collections
import copy
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()

D1 = [0, 24]
m = []
for k, v in C.items():
    if v == 2:
        D1.append(k)
        D1.append(24-k)
    elif v == 1:
        m.append(k)

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

    # print(D0)

    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: Accepted

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 0 in D:
    print(0)
elif N>=24:
    print(0)
elif N==23:
    D.sort()
    for i in range(N):
        if D[i]!=i//2+1:
            print(0)
            exit()
    print(1)
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)