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:


from collections import Counter
from collections import defaultdict

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




c = Counter(D)


# 高橋君と同じ時間帯の人が一人でもいれば、時差の最小は常に0
if c[0] >= 2:
    print(0)
    exit(0)

# 高橋君との時差がdの人が3人以上いる場合も、時差の最小は常に0（同じ時間帯の都市に二人以上いることになるから）
for i in c.values():
    if i >= 3:
        print(0)
        exit()


times = defaultdict(int)
c[0] += 1
# 0時との時間差をとるときに24がいたほうがよいか？なくてもいいか。
c[24] += 1
before_12 = True
D.sort()
for d in D:
    if before_12:
        times[d] += 1
        before_12 = False
    else:
        times[24-d] += 1
        before_12 = True


# 高橋君との時差で答えを初期化
ans = min(D)

tlst = list(times.keys())
tlst.sort()

for a,b in zip(tlst, tlst[1:]):
    ans = min(ans, b - a)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10 **9 + 7
INF = 10 ** 10
from collections import Counter
from copy import deepcopy

def main():
    N = int(input())
    D = Counter(map(int,input().split()))
    l = 0
    time = [0]
    either = []
    for k,v in D.items():
        if v == 1:
            l += 1
            either.append(k)
        elif v == 2:
            if k == 12:
                print(0)
                return
            time.extend((k,24 - k))
        else:
            print(0)
            return
    
    if l == 0:
        gap = INF
        for t in time:
            for s in time:
                if s == t:
                    continue
                gap = min(gap,min(abs(s - t),24 - abs(s - t)))
        print(gap)
        return
    
    ans = 0
    for bit in range(1<<l):
        T = deepcopy(time)
        gap = INF
        for i in range(l):
            if (bit>>i)&1:
                t = either[i]
            else:
                t = 24 - either[i]
            for x in T:
                gap = min(gap,min(abs(t - x),24 - abs(t - x)))
            T.append(t)
        ans = max(ans,gap)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
N = int(input())
Dn = list(map(int,input().split(' ')))
d_map = {}
for d in Dn:
    if d not in d_map:
        d_map[d] = 1
    else:
        d_map[d] += 1
if 0 in d_map:
    print(0)
    exit(0)
if len(Dn) == 1:
    print(Dn[0])
    exit(0)
for v in d_map.values():
    if 3 <= v:
        print(0)
        exit(0)
all_combination = [ [0] ]
for k,v in d_map.items():
    if v == 1:
        len_all_combination = len(all_combination)
        for i in range(len_all_combination):
            tmp = copy.deepcopy(all_combination[i])
            all_combination[i].append(k)
            tmp.append(24-k)
            all_combination.append(tmp)
    else:
        for comb in all_combination:
            comb.append(k)
            comb.append(24-k)
ans = -1
for comb in all_combination:
    comb.sort()
    #print(comb)
    tmp = 100
    for i in range(1,len(comb)):
        if comb[i] > 12:
            tmp = min(tmp,min(comb[i]-comb[i-1],24-comb[i]))
        else:
            tmp = min(tmp,comb[i]-comb[i-1])
    ans = max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
D = list(map(int, input().split()))
D.sort()
d = [0]*25
d[0] = d[-1] = 1
for i in range(N):
    if i & 1:
        d[D[i]] += 1
    else:
        d[24-D[i]] += 1
for i in range(25):
    if d[i] > 1:
        print(0)
        exit()
last, ans = 0, 24
for i in range(1, 25):
    if d[i]:
        ans = min(i-last, ans)
        last = i
print(ans)

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
D = lr()

l = {i: 0 for i in range(13)}

for d in D:
    l[d] += 1

possible_lists = [[]]

for k, v in l.items():
    if v > 2:
        possible_lists = [[0]]
        break
    elif v > 1:
        new = [[0]] + [pl + [k, 24-k] for pl in possible_lists]
        # new = [pl + [k, k] for pl in possible_lists] + [pl + [24-k, 24-k] for pl in possible_lists] + [pl + [k, 24-k] for pl in possible_lists]
        possible_lists = new
    elif v > 0:
        new = [pl + [k] for pl in possible_lists] + [pl + [24-k] for pl in possible_lists]
        possible_lists = new

Max = -1
for pl in possible_lists:
    Min = 13
    pl.sort()
    for i in range(len(pl)):
        if i == 0:
            cand = pl[i]-0
            Min = min(Min, min(cand, 24-cand))
        elif i == len(pl)-1:
            cand = pl[i]-pl[i-1]
            Min = min(Min, min(cand, 24-cand))
            cand = pl[i]-0
            Min = min(Min, min(cand, 24-cand))
        else:
            cand = pl[i]-pl[i-1]
            Min = min(Min, min(cand, 24-cand))
    Max = max(Min, Max)

print(Max)

Submission status: Accepted

Solution code:
from itertools import groupby

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

def dfs(list,num):
    global ans
    if num==N:
        m=100
        for i in range(0,N):
            for j in range(i+1,N+1):
                s=abs(list[i]-list[j])
                test=min(s,24-s)
                m=min(test,m)
        ans=max(ans,m)
    else:
        dfs(list+[D[num]],num+1)
        dfs(list+[24-D[num]],num+1)

if N>=24:
    print(0)
elif N>=12:
    D.sort()
    D=groupby(D)
    for key,group in D:
        g=len(list(group))
        if key==0:
            print(0)
            exit()
        elif key==12:
            if g>1:
                print(0)
                exit()
        else:
            if g>2:
                print(0)
                exit()
    print(1)
else:
    ans=0
    dfs([0],0)
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
A=sorted(A)
D=[0]*25
D[0]+=1
D[24]+=1
for i in range(N):
  if i%2==0:
    D[A[i]]+=1
  else:
    D[24-A[i]]+=1

for i in D:
  if i>1:
    print(0)
    exit()
p=0
ans=24
for i in range(1,25):
  if D[i]==1:
    d=(i-p)
    ans=min(d,ans)
    p=i
print(ans)

Submission status: Accepted

Solution code:
# 想定解法

N = int(input())
D = list(map(int, input().split()))
num_time = [0] * 13
num_time[0] = 2
num_time[12] = 1

fixed = [0]
unfixed = []
for d in D:
  num_time[d] += 1
  if d == 12:
    fixed.append(12)
  if num_time[d] >= 3:
    print(0)
    quit()

for t in range(1, 12):
  if num_time[t] == 2:
    fixed.extend([t, 24 - t])
  elif num_time[t] == 1:
    unfixed.append(t)

ans = 0
n = len(unfixed)
for i in range(2**n):
  t = fixed.copy() + [24]
  # bitが0のものはt, 1のものは24-tとして設定する
  for j in range(n):
    if i >> j & 1:
      t.append(unfixed[j])
    else:
      t.append(24 - unfixed[j])
  t.sort()
  s = 24
  m = len(t)
  for k in range(m-1):
    _s = t[k+1] - t[k]
    s = min(s, _s)
  ans = max(ans, s)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = [0] + list(map(int, input().split()))
Diffs = [[[0, 0] for i in range(N+1)] for j in range(N+1)]

# ある都市間の時差の取りうる値は2種類のみ
# 先ずはそれを全列挙
candidates = []
for i in range(N+1):
  for j in range(i+1, N+1):
    di, dj = D[i], D[j]
    diff1, diff2 = abs(di-dj), min(24-di-dj, di+dj)
    candidates.extend([[i, j, diff1, 0], [i, j, diff2, 1]])
    Diffs[i][j] = [diff1, diff2]

# 上で全列挙した時差に関して、その時差が全体の最小値となり得るかどうかを確認していく
# 最小値となり得るかどうかを確認したい時差の値をdiffとすると、
# 任意の2都市間の時差がdiff以上となる必要があり、そのような時間の設定方法があるかどうかを確認する
# 矛盾するかどうかは都市を2色で塗り分ける要領で確認できる
ans = 0
for idx1, idx2, diff, c in candidates:
  conditions = [-1] * (N+1)
  if c == 0:
    conditions[idx1] = conditions[idx2] = 0
  else:
    conditions[idx1] = 0; conditions[idx2] = 1
  res = True
  for i in range(N+1):
    for j in range(i+1, N+1):
      if idx1 == i and idx2 == j:
        continue
      diff1, diff2 = Diffs[i][j]
      if diff1 < diff and diff2 < diff:
        res = False
        break
      elif diff1 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = 0; conditions[j] = 1
        elif c1 < 0:
          conditions[i] = 1 - c2
        elif c2 < 0:
          conditions[j] = 1 - c1
        else:
          if c1 == c2:
            res = False
            break
      elif diff2 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = conditions[j] = 0
        elif c1 < 0:
          conditions[i] = c2
        elif c2 < 0:
          conditions[j] = c1
        else:
          if c1 != c2:
            res = False
            break
    else:
      continue
    break
  if res:
    ans = max(ans, diff)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = [0] + list(map(int, input().split()))
Diffs = [[[0, 0] for i in range(N+1)] for j in range(N+1)]

candidates = []
for i in range(N+1):
  for j in range(i+1, N+1):
    di, dj = D[i], D[j]
    diff1, diff2 = abs(di-dj), min(24-di-dj, di+dj)
    candidates.extend([[i, j, diff1, 0], [i, j, diff2, 1]])
    Diffs[i][j] = [diff1, diff2]

ans = 0
for idx1, idx2, diff, c in candidates:
  conditions = [-1] * (N+1)
  if c == 0:
    conditions[idx1] = conditions[idx2] = 0
  else:
    conditions[idx1] = 0; conditions[idx2] = 1
  res = True
  for i in range(N+1):
    for j in range(i+1, N+1):
      if idx1 == i and idx2 == j:
        continue
      diff1, diff2 = Diffs[i][j]
      if diff1 < diff and diff2 < diff:
        res = False
        break
      elif diff1 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = 0; conditions[j] = 1
        elif c1 < 0:
          conditions[i] = 1 - c2
        elif c2 < 0:
          conditions[j] = 1 - c1
        else:
          if c1 == c2:
            res = False
            break
      elif diff2 < diff:
        c1, c2 = conditions[i], conditions[j]
        if c1 < 0 and c2 < 0:
          conditions[i] = conditions[j] = 0
        elif c1 < 0:
          conditions[i] = c2
        elif c2 < 0:
          conditions[j] = c1
        else:
          if c1 != c2:
            res = False
            break
    else:
      continue
    break
  if res:
    ans = max(ans, diff)

print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    D=LI()
    if( 0 in D):
        print(0)
    elif( N >= 24):
        print(0)
    elif( N>= 12):
        C=Counter(D)
        for c in C.values():
            if(c>=3):
                print(0)
                return
        print(1)
    else:
        ans = 0
        for mask in range(1<<N):
            times = [0,24]
            for i in range(N):
                if(mask >> i & 1):
                    times.append(24-D[i])
                else:
                    times.append(D[i])
            times.sort()
            sub = [times[i+1] - times[i] for i in range(len(times)-1)]
            ans = max(ans ,min(sub))
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import product
from collections import Counter


def solve():
    N = int(input())
    *D, = map(int, input().split())

    l, m = [], []
    for i, j in Counter(D).items():
        if j>=3 or (i==12 and j>=2) or i==0:
            print(0)
            return
        elif j==2:
            l.append(i)
        elif j==1:
            m.append(i)

    size = len(m)
    ans = 0

    for i in product(range(2), repeat=size):
        c = []
        for j, k in enumerate(i):
            if k:
                c.append(m[j])
            else:
                c.append(24-m[j])
        for j in l:
            c.append(j)
            c.append(24-j)

        v = 24
        for j in c:
            d = min(j, 24-j)
            v = min(v, d)

        for j in range(N):
            for k in range(j+1, N):
                d = abs(c[j]-c[k])
                v = min(v, min(d, 24-d))
        ans = max(ans, v)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    d = LI()
    ans = 0
    q = [1]
    for i in range(n):
        q_ = []
        while q:
            p = q.pop()
            if not (1 << d[i]) & p:
                q_.append((1 << d[i]) | p)
            if not (1 << (24 - d[i])) & p:
                q_.append((1 << (24 - d[i])) | p)
        q = q_
    for qi in q:
        tmp = 25
        for i in range(25):
            for j in range(i + 1, 25):
                x = (1 << i) | (1 << j)
                if (qi & x) == x:
                    tmp = min(tmp, min(j - i, 24 - (j - i)))
        ans = max(ans, tmp)
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())
D = list(map(int,input().split()))
L = [1] + [0]*12
for d in D:
    L[min(d, 24-d)] += 1
if max(L)>2 or L[0]>1 or L[12]>1:
    print(0)
else:
    ans = 0
    for i in range(2**11):
        T = [1] + [0]*23 + [1]
        for j in range(1, 12):
            if L[j]==2:
                T[j] = 1
                T[24-j] = 1 
            elif L[j]==1:
                if i&(1<<(j-1)):
                    T[j] = 1
                else:
                    T[24-j] = 1
        if L[12] == 1:
            T[12] = 1
        cur = 24
        t = 0
        for j in range(1, 25):
            if T[j]==1:
                cur = min(cur, t+1)
                t = 0
            else:
                t += 1
        ans = max(ans, cur)
    print(ans)

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:
# -*- coding: utf-8 -*-
N = int(input())
D = list(map(int, input().split(' ')))

frqs = [0 for _ in range(24)]
frqs[0] = 1
for d in D:
    if not d:
        print(0)
        exit(0)

    i = min(d, 24 - d)
    frqs[i] += 1



indices = []
for i in range(13):
    if frqs[i] > 2:
        print(0)
        exit(0)

    elif frqs[i] == 2:
        frqs[i] -= 1
        frqs[24-i] += 1

    elif frqs[i] == 1:
        indices.append(i)


def get(frqs):
    ret = float('inf')
    for i in range(24):
        if frqs[i]:
            for j in range(i+1, 24):
                if frqs[j]:
                    d = abs(j - i)
                    ret = min(ret, min(d, 24 - d))
    return ret

buf = [[]]
for _ in indices:
    buf2 = []
    while buf:
        b = buf.pop()
        buf2.append(b + [True])
        buf2.append(b + [False])

    buf = buf2

ans = get(frqs)
for bools in buf:
    for i, b in zip(indices, bools):
        if i and b:
            frqs[i] -= 1
            frqs[24 - i] += 1

        ans = max(ans, get(frqs))

    for i, b in zip(indices, bools):
        if i and b:
            frqs[i] += 1
            frqs[24 - i] -= 1


print(ans)

Submission status: Accepted

Solution code:
N, *D = map(int, open(0).read().split())
D.append(0)
used = [0]*(N+1)
cnt = N
while True:
  ls = []
  for i in range(N+1):
    for j in range(N+1):
      if i==j:
        continue
      a,b = min(D[i],D[j]),max(D[i],D[j])
      d = b-a
      ls.append((min(d,24-d),i,j))
  ls.sort(key=lambda x:x[0])
  d,a,b = ls[0]
  if used[a]==0 and used[b]==0:
    da = (24-D[a])%24
    ma = 100
    for i in range(N+1):
      if i==a:
        continue
      x = abs(da-D[i])
      ma = min(ma,x,24-x)
    db = (24-D[b])%24
    mb = 100
    for i in range(N+1):
      if i==b:
        continue
      x = abs(db-D[i])
      mb = min(mb,x,24-x)
    if ma<mb:
      if d>=mb:
        print(d)
        break
      D[b] = db
      used[b] = 1
    else:
      if d>=ma:
        print(d)
        break
      D[a] = da
      used[a] = 1
  elif used[a]==0:
    used[a] = 1
    da = (24-D[a])%24
    ma = 100
    for i in range(N+1):
      if i==a:
        continue
      x = abs(da-D[i])
      ma = min(ma,x,24-x)
    if d>=ma:
      print(d)
      break
    D[a] = da
  elif used[b]==0:
    used[b] = 1
    db = (24-D[b])%24
    mb = 100
    for i in range(N+1):
      if i==b:
        continue
      x = abs(db-D[i])
      mb = min(mb,x,24-x)
    if d>=mb:
      print(d)
      break
    D[b] = mb
  else:
    print(d)
    break

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
a = sorted([0,24]+a)
for i in range(1, n+1, 2): a[i] = 24-a[i]
a.sort()
ans = 24
for i in range(n+1): ans = min(ans, abs(a[i]-a[i+1]))
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from datetime import date, timedelta

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
D = LIST()
D.sort()

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

time = [0]
for i, d in enumerate(D):
	if i % 2 == 0:
		time.append(d)
	else:
		time.append(24-d)
time.sort()
time.append(24)
ans = INF
for i in range(N):
	ans = min(ans, time[i+1]-time[i])
print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    D = [int(i) for i in input().split()]
    ans = 0
    cnt = [0]*24
    from collections import Counter
    c = Counter(D)
    cnt[0] = 1
    s = [0]*13
    for k, v in c.items():
        if v >= 3:
            ans = 0
            return print(ans)
        elif k == 0 or k == 12:
            cnt[k] += v
        elif v >= 2:
            cnt[k] += 1
            cnt[24 - k] += 1
        else:
            s[k] += 1
    if any(c >= 2 for c in cnt):
        return print(0)
    for bit in range(1 << 11):
        cur = cnt[:]
        for i in range(11):
            if s[i+1]:
                if bit & (1 << i):
                    cur[24 - (i+1)] += 1
                else:
                    cur[i+1] += 1
        pre = 0
        mi = 24
        for i, ne in enumerate(cur[1:] + [1], start=1):
            if ne:
                if i - pre < mi:
                    mi = i - pre
                pre = i
        ans = max(ans, mi)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    D = [int(i) for i in input().split()]
    ans = 0
    cnt = [0]*24
    from collections import Counter
    c = Counter(D)
    cnt[0] = 1
    s = [0]*13
    for k, v in c.items():
        if v >= 3 or k == 0:
            ans = 0
            return print(ans)
        elif v >= 2:
            cnt[k] += 1
            cnt[24 - k] += 1
        elif k == 12:
            cnt[12] += v
        else:
            s[k] += 1
    if any(c >= 2 for c in cnt):
        return print(0)
    for bit in range(1 << 11):
        cur = cnt[:]
        for i in range(11):
            if s[i+1]:
                if bit & (1 << i):
                    cur[24 - (i+1)] += 1
                else:
                    cur[i+1] += 1
        if any(c >= 2 for c in cnt):
            continue
        pre = 0
        mi = 24
        for i, ne in enumerate(cur[1:] + [1], start=1):
            if ne:
                if i - pre < mi:
                    mi = i - pre
                pre = i
        if mi != 24:
            ans = max(ans, mi)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int,input().split())) + [0]
from collections import Counter

ctr = Counter(D)
if ctr[0] >= 2 or ctr[12] >= 2:
    print(0)
    exit()
if any(v >= 3 for v in ctr.values()):
    print(0)
    exit()

ans = 0
for b in range(1<<11):
    ts = [0] * 24
    ts[0] = 1
    if ctr[12]: ts[12] = 1
    for i in range(11):
        t = i+1
        if ctr[t] == 0: continue
        if ctr[t] == 2:
            ts[t] = ts[24-t] = 1
            continue
        if b&(1<<i):
            ts[t] = 1
        else:
            ts[24-t] = 1
    tt = [i for i,b in enumerate(ts) if b]
    tmp = 24 - tt[-1]
    for t1,t2 in zip(tt,tt[1:]):
        tmp = min(tmp, t2-t1)
    ans = max(tmp, ans)
print(ans)

Submission status: Accepted

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

memo = {0: 1}
for i in range(n):
    if d[i] not in memo:
        memo[d[i]] = 1
    else:
        memo[d[i]] += 1

used = [] * n
li = []
for i in memo:
    if memo[i] >= 3:
        print(0)
        exit()
    elif memo[i] == 2:
        used.append(i)
        used.append(24 - i)
    else:
        li.append(i)

ans = 0
for bit_state in range(1 << len(li)):
    tmp = used[0:]
    for i in range(len(li)):
        if bit_state & (1 << i):
            tmp.append(li[i])
        else:
            tmp.append(24 - li[i])
    tmp = sorted(tmp)
    diff = 10 ** 18
    for i in range(1, len(tmp)):
        diff = min(diff, tmp[i] - tmp[i - 1])
    diff = min(diff, 24 - (tmp[-1] - tmp[0]))
    ans = max(ans, diff)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))
ans = 0
lb, ub = -1, min(D) + 1
while ub - lb > 1:
    m = (lb + ub) // 2
    group = [0] * N
    for k in range(N):
        if group[k] != 0:
            continue
        group[k] = 1
        stack = [k]
        while stack:
            i = stack.pop()
            for j in range(N):
                if i == j or group[j] != 0:
                    continue
                diff = abs(D[i]-D[j])
                if min(diff, 24-diff) < m:
                    group[j] = group[i] * -1
                    stack.append(j)
    ok = True
    for i in range(N):
        for j in range(i):
            diff = abs(D[i]*group[i]-D[j]*group[j])
            if min(diff, 24-diff) < m:
                ok = False
                break
        if not ok:
            break
    if not ok:
        ub = m
    else:
        lb = m
print(lb)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
def saiki(num,lis):
    if num==13:
        #print(lis)
        temp=10**9
        lis.sort()
        for i in range(len(lis)-1):
            temp=min(temp,abs(lis[i+1]-lis[i]))
        if temp!=10**9:
            ans.append(temp)
        
    else:
        if dp[num]>2:
            print(0)
            exit()
        elif dp[num]==2:
            saiki(num+1,lis+[num]+[24-num])
            saiki(num+1,lis+[num]+[num])
            saiki(num+1,lis+[24-num]+[24-num])
        elif dp[num]==1:
            saiki(num+1,lis+[num])
            saiki(num+1,lis+[24-num])
        else:
            saiki(num+1,lis)            


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

dp=[0]*13
#dp[0]+=1
for d in D:
    dp[d]+=1

ans=[]
saiki(0,[0,24])

print(max(ans))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    D = list(map(int, input().split()))
    D.append(0)
    h = [0 for _ in range(13)]
    for d in D:
        h[d] += 1
    if h[0] >= 2 or h[12] >= 2:
        return 0
    for i in range(1, 12):
        if h[i] >= 3:
            return 0

    res = 0
    for i in range(2**11):
        L = [0]
        for j in range(1, 12):
            if h[j] == 2:
                L.append(j)
                L.append(24-j)
            if h[j] == 1:
                if i & 2**(j-1) != 0:
                    L.append(j)
                else:
                    L.append(24-j)
        if h[12] == 1:
            L.append(12)
        L.append(24)
        L.sort()
        tres = 25
        for i in range(len(L)-1):
            tres = min(tres, L[i+1]-L[i])
        res = max(res, tres)
    return res


print(main())

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import combinations, product

def gap(s, t):
    d = abs(s-t)
    return min(d, 24 - d)

N = int(input())
*D, = map(int, input().split())
cnt = Counter(D + [0])
time_definite = []
time_indefinite = []
for k, v in cnt.items():
    if (k != 0 and v >= 3) or (k == 0 and v >= 2):
        ans = 0; break
    else:
        if v == 2:
            time_definite.append(k); time_definite.append(-k)
        else:
            if k == 0: time_definite.append(k)
            else: time_indefinite.append(k)
else:
    ans = 0
    def_min = 12
    for t1, t2 in combinations(time_definite, 2):
        def_min = min(def_min, gap(t1, t2))
    cand = len(time_indefinite)
    for bit in range(1 << cand):
        time_temp = [t if (bit & (1 << i)) else -t for i, t in enumerate(time_indefinite)]
        indef_min = 12
        for t1, t2 in combinations(time_temp, 2):
            indef_min = min(indef_min, gap(t1, t2))
        temp = min(def_min, indef_min)
        for t1, t2 in product(time_temp, time_definite):
            temp = min(temp, gap(t1, t2))
        ans = max(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
def f(a, b):
    d = abs(a-b)
    return min(d, 24-d)

N = int(input())
D = list(map(int, input().split())) + [0]
D.sort()
for i in range(N+1):
    if i%2:
        D[i] = 24 - D[i]
D.sort()
ans = f(D[0], D[-1])
for i in range(N):
    ans = min(ans, f(D[i], D[i+1]))
print(ans)

Submission status: Accepted

Solution code:
from itertools import product
from collections import Counter
def main():
    N,*D=map(int, open(0).read().split())
    C=Counter(D)
    ans=min(D)
    for i in range(1,12):
        if i in C and C[i]>=3:
            ans=0
    if 12 in C and C[12]>=2:
        ans=0
    if ans:
        X1=[k for k,v in C.items() if v==1]
        X2=[k for k,v in C.items() if v==2]
        Y=[False]*24
        for x in X2:
            Y[x]=Y[-x]=True
        res=0
        for p in product((True, False), repeat=len(X1)):
            Z=Y.copy()
            for t,x in zip(p,X1):
                if t:
                    Z[x]=True
                else:
                    Z[-x]=True
            res2=12
            I=[i for i,p in enumerate(Z) if p]
            for i in I:
                for j in I:
                    if i==j: continue
                    res2=min(res2,abs(i-j))
            res=max(res,res2)
        ans=min(ans,res)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
D = list(map(int, input().split()))

C = Counter(D)

P = [0] #takahashi
if C[0] > 0:
  print(0)
  exit(0)
  
hidarif = True
for i in range(1,13):
  if C[i] == 0:
    continue
  if C[i] > 2:
    print(0)
    exit(0)
  if C[i] == 2:
    P.append(i)
    P.append(24-i)
    continue
  
  P.append(i if hidarif else 24-i)
  hidarif = not hidarif
  
P.sort()

ans = 24
for i in range(0,len(P)):
  compare = 0 if i == len(P) - 1 else i+1
  ans = min(ans, abs(P[compare] - P[i]))
  
print(ans)

Submission status: Accepted

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

D.append(0)
D.sort()
for i in range(1, N+1):
    if i%2 == 0:
        D[i] = 24-D[i]
D.sort()
ans = min(D[-1], 24-D[-1])
for i in range(N):
    ans = min(ans, D[i+1]-D[i])
print(ans)