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(v) for v in input().split(' ')]

D.append(0)
L = len(D)

D.sort()


min_diff = D[1] - D[0]

if L >= 3:
    d = (24 - D[L - 2]) - D[L - 1]
    if min_diff > d:
        min_diff = d

for i in range(2, L):
    d = D[i] - D[i - 2]
    if min_diff > d:
        min_diff = d

print(min_diff)

Submission status: Accepted

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

def minGap(hour):
    ret = float('inf')
    for i in range(len(hour)):
        for j in range(len(hour)):
            if i == j:
                continue
            h1 = hour[i]
            h2 = hour[j]
            gap = max(h1, h2) - min(h1, h2)
            ret = min(ret, gap, 24 - gap)
    return ret

hour = [0]
for d in D:
    back = minGap(hour + [d])
    front = minGap(hour + [24 - d])
    if back > front:
        hour += [d]
    else:
        hour += [24 - d]
ans = minGap(hour)

print(ans)

Submission status: Accepted

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

def minGap(hour):
    ret = float('inf')
    for i in range(len(hour)):
        for j in range(len(hour)):
            if i == j:
                continue
            h1 = hour[i]
            h2 = hour[j]
            gap = max(h1, h2) - min(h1, h2)
            ret = min(ret, gap, 24 - gap)
    return ret

D.sort()
hour = [0]
for i, d in enumerate(D):
    if i % 2 == 0:
        hour.append(d)
    else:
        hour.append(24 - d)

print(minGap(hour))

Submission status: Accepted

Solution code:
N=int(input())
D=list(map(int,input().split()))
from collections import defaultdict
dic=defaultdict(int)
for d in D:
    dic[d]+=1
a=[0,24]
f=0
T=[]
for k in dic.keys():
    for i in range(dic[k]):
        T.append(k)
T.sort()
for t in T:
    if f:
        t=24-t
    a.append(t)
    f^=1
a.sort()
r=12
for i in range(len(a)-1):
    r=min(r,a[i+1]-a[i])
print(r)

Submission status: Accepted

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

def doit():
  n = int(input())
  if n >= 24:
    return 0

  d = [int(x) for x in input().split()]
  if n == 1:
    return d[0]
  
  res = [0] * 13
  for x in d:
    if x == 0:
      return 0
    res[x] += 1;
    if x == 12:
      if res[x] == 2:
        return 0
    if res[x] == 3:
      return 0
  
  #ban = [False] * 24
  mm = 12
  flip = False
  prev = [0, 24]
  for i, x in enumerate(res):
    diff = 12
    if x == 2:
      #ban[i] = ban[24 - i] = True
      diff = i - prev[0]
      prev[0] = i
      prev[1] = 24 - i
      #print("x2", i, diff, file=sys.stderr)
      flip = not flip
    if x == 1:
      if flip:
        #ban[i] = True
        diff = i - prev[0]
        prev[0] = i
      else:
        #ban[24 - i] = True
        diff = prev[1] - (24 - i)
        prev[1] = 24 - i
      flip = not flip
      #print("x1", i, diff, file=sys.stderr)
    if diff < mm:
      mm = diff
      #print(mm, file=sys.stderr)
  #last = abs(prev[1] - prev[0])
  last = prev[1] - prev[0]
  #print("last", prev[0], prev[1], file=sys.stderr)
  if last < mm:
    return last
  return mm


print(doit())

Submission status: Accepted

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

n = int(input())
d = [int(x) for x in input().split()]

def doit():
  if n >= 24:
    return 0
  if n == 1:
    return d[0]
  
  res = [0] * 13
  for x in d:
    if x == 0:
      return 0
    res[x] += 1;
    if x == 12:
      if res[x] == 2:
        return 0
    if res[x] == 3:
      return 0
  
  ban = [False] * 24
  mm = 12
  flip = False
  prev = [0, 24]
  for i, x in enumerate(res):
    diff = 12
    if x == 2:
      ban[i] = ban[24 - i] = True
      diff = i - prev[0]
      prev[0] = i
      prev[1] = 24 - i
      print("x2", i, diff, file=sys.stderr)
      flip = not flip
    if x == 1:
      if flip:
        ban[i] = True
        diff = i - prev[0]
        prev[0] = i
      else:
        ban[24 - i] = True
        diff = prev[1] - (24 - i)
        prev[1] = 24 - i
      flip = not flip
      print("x1", i, diff, file=sys.stderr)
    if diff < mm:
      mm = diff
      print(mm, file=sys.stderr)
  #last = abs(prev[1] - prev[0])
  last = prev[1] - prev[0]
  print("last", prev[0], prev[1], file=sys.stderr)
  if last < mm:
    return last
  return mm


print(doit())

Submission status: Accepted

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

line=[0]*13
clock = [0]*24
tag=0
ans=25
ansL=[]

for i in D:
    line[i%13]+=1

if(line[0]>=2 or line[12]>=2):
    ans=0
else:
    #lineを円周上の時計に置き換える
    for i in range(len(line)):
        if (line[i] > 2):#線上に３人以上 ->どうやっても同時刻に２人以上存在することになるのでans=0
            ans = 0
            break
        elif (line[i] == 2): #線上に２人
            clock[i] = 1
            clock[24 - i] = 1
        elif (line[i] == 1):#線上に１人 ー＞交互に
            if (tag % 2 == 0):
                clock[i] = 1
            else:
                clock[24 - i] = 1
            tag += 1

    #one-hotベクトルに変換
    for i in range(len(clock)):
        if(clock[i]==1):
            ansL.append(i)

    ansL.reverse()
    before = 24
    for i in range(len(ansL)):
        if (ans > before - ansL[i]):
            ans = before - ansL[i]
        before=ansL[i]

print(ans)

Submission status: Accepted

Solution code:
import sys

#+++++

def dd_max(ring):
	mm=25
	s=1
	for r in ring[1:]:
		#print(s,r,mm)
		if r >= 1:
			mm=min(s,mm)
			s=1
		else:
			s+=1
	return mm
	
		
def main():
	n = int(input())
	dl = list(map(int, input().split()))
	dl.append(0)
	
	#0が2個あったら0
	if dl.count(0) >=2:
		return 0
	#12が2個あったら0
	if dl.count(12) >=2:
		return 0
	#1-11が3個以上あったら0
	for i in range(1,11+1):
		if dl.count(i) >= 3:
			return 0
	
	ll=[0]*25
	ll[0]=1
	ll[24]=1
	cc=[]
	if dl.count(12) == 1:
		ll[12]=1
	for i in range(1,11+1):
		if dl.count(i)==2:
			ll[i]=1
			ll[24-i]=1
		if dl.count(i)==1:
			cc.append(i)
			
	if len(cc)==0:
		print(dd_max(ll))
		#print(ll)
		return 
	
	#1-11が2個あったらプラスとマイナスに配置が必須
	ret=0
	n_cc=len(cc)
	cc_max=(2**n_cc)-1
	for i in range(cc_max):
		tll=ll.copy()
		for j,v in enumerate(cc):
			if i & (2**j) > 0:
				tll[24-v]=1
			else:
				tll[v]=1
				
		tr=dd_max(tll)
		ret=max(ret, tr)
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=s((x,24-x)[i%2]for i,x in enumerate(s(map(int,d.split()))));print(min(j-i for i,j in zip([0]+l,l)))

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=s((24-x,x)[i%2]for i,x in enumerate([0]+s(map(int,d.split()))));print(min(j-i for i,j in zip([0]+l,l)))

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=s((24-x,x)[i%2]for i,x in enumerate(s(map(int,d.split()))));print(min(j-i for i,j in zip([0]+l,l+[24])))

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=s([(24-x,x)[i%2]for i,x in enumerate(s(map(int,d.split())))]);print(min(j-i for i,j in zip([0]+l,l+[24])))

Submission status: Accepted

Solution code:
N=int(input())
D =[0]+[int(x) for x in input().split()]
m=D[1]
D.sort()
for i in range(1,N+1):
  D[i]=D[i] if i&1 else 24-D[i]
D.sort()
for i in range(1,N+1):
  m= min(m,D[i]-D[i-1])
print(m)

Submission status: Accepted

Solution code:
N,*D=map(int,open(0).read().split())
T=[0]+sorted(d if i%2 else (24-d)%24 for i,d in enumerate(sorted(D)))+[24]
print(min(a-b for a,b in zip(T[1:],T)))

Submission status: Accepted

Solution code:
N, *D = map(int, open(0).read().split())
D.sort()
T = [0] + sorted(d if i % 2 else (24 - d) % 24 for i, d in enumerate(D)) + [24]
print(min(T[i + 1] - T[i] for i in range(N + 1)))

Submission status: Accepted

Solution code:
N, *D = map(int, open(0).read().split())
D.append(0)
D.sort()
T = [0] * 24
for i, d in enumerate(D):
    if i % 2 == 0 and d % 12 != 0:
        d = 24 - d
    T[d] += 1
if any(t >= 2 for t in T):
    print(0)
    quit()
T = [i for i, t in enumerate(T) if t != 0] + [24]
print(min(T[i + 1] - T[i] for i in range(len(T) - 1)))

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=[0]+s([[24-x,x][i%2]for i,x in enumerate(s(map(int,d.split())))])+[24];print(min(j-i for i,j in zip(l,l[1:])))

Submission status: Accepted

Solution code:
_,d=open(0);s=sorted;l=[0]+s([x if i%2else 24-x for i,x in enumerate(s(map(int,d.split())))])+[24];print(min(j-i for i,j in zip(l,l[1:])))

Submission status: Accepted

Solution code:
n,*d=map(int,open(0).read().split());s=sorted
l=[0]+s([x if i%2 else 24-x for i,x in enumerate(s(d))])+[24]
print(min(j-i for i,j in zip(l,l[1:])))

Submission status: Accepted

Solution code:
n,*d=map(int,open(0).read().split())
if 0 in d or d.count(12)>1 or any(d.count(x)>2 for x in range(1,12)) or n>24:
	print(0)
elif n>11:
	print(1)
else:
	m=0
	for i in range(2**n):
		l=[0,24]
		for j in range(n):
			if i>>j&1:
				l.append(24-d[j])
			else:
				l.append(d[j])
		l.sort()
		m=max(m,min(j-i for i,j in zip(l,l[1:])))
	print(m)

Submission status: Accepted

Solution code:
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def main():
    N = I()
    D = LI()

    pigeon = [0] * 13

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

    if pigeon[0] >= 1:
        return 0
    if pigeon[12] >=2:
        return 0
    for i in range(1,12):
        if pigeon[i] >= 3:
            return 0

    if N > 11:
        return 1

    Time = [[0,24] for _ in range(2**N)]

    ans = 0
    for x in range(2**N):
        for i in range(N):
            if (x>>i)%2:
                Time[x].append(D[i])
            else:
                Time[x].append(24-D[i])
        Time[x].sort()
        ans = max(ans,min([a-b for a,b in zip(Time[x][1:],Time[x][:-1])]))

    return(ans)

if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
import copy

def minTG(TG):
    indexes1 = [i for i, x in enumerate(TG) if x == 1]
    indexes2 = [i for i, x in enumerate(TG) if x == 2]
    TG24 = [1] + [0] * 23    
    if 12 in indexes1:
        indexes1.pop(-1)
        TG24[12] = 1
    for i in indexes1 + indexes2:
        TG24[i], TG24[24-i] = 1, 1
    
    ans = 0
    for i in range(2**len(indexes1)):
        TG24c = copy.copy(TG24)
        bit = i
        for j in range(len(indexes1)):
            if bin(bit>>(j+1))[-1] == '1':
                TG24c[indexes1[j]] = 0
            else:
                TG24c[24 - indexes1[j]] = 0
        temp = minTG_search(TG24c)
        if ans < temp:  ans = temp
    return ans

def minTG_search(TG24):
    s, temp, i = 24, 0, 1
    while i < 24:
        if TG24[i] == 1:
            if i - temp < s:
                s = i - temp
            temp = i
        i += 1
    if 24 - temp < s:
        s = 24 - temp
    return s

N = int(input())
D = sorted(list(map(int, input().split())))
TG = [0] * 13
for i in range(N):
    TG[D[i]] += 1

if TG[0] > 0 or max(TG) > 2:
    print(0)
else:
    print(minTG(TG))

Submission status: Accepted

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

T = [0 for _ in range(13)]

for d in D:
    if d > 12:
        T[24-d] += 1
    else:
        T[d] += 1

which = []
A = []
ans = -1
for i, t in enumerate(T):
    if t == 0:
        continue
    elif t == 1:
        which.append(i)
    elif t == 2:
        A.extend([i, 24-i])
    else:
        ans = 0
        break

def dfs(which, i, l, p):
    if i == len(which):
        p.append(l)
        return p
    t = which[i]
    p = dfs(which, i+1, l+[t], p)
    p = dfs(which, i+1, l+[24-t], p)
    return p

if ans == 0:
    print(0)
else:
    p = dfs(which, 0, [], [])
    #print(p)
    ans = 0
    for l in p:
        B = sorted(l+A)
        pre = 0
        tmp = 100
        for b in B:
            tmp = min(tmp, b-pre)
            pre = b
        tmp = min(tmp, 24-pre)
        ans = max(ans, tmp)
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#CODEFESTIVAL2017 Final C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
D = LI()
D.sort()
a = D[:]
b = D[:]
if n == 1:
    print(D[0])
    quit()
for i in range(n):
    if i % 2:
        continue
    a[i] = 24 - a[i]
a.sort()
for i in range(n):
    if i % 2:
        b[i] = 24 - b[i]
b.sort()
d = [0] + a + [24]
ans1 = inf
for i in range(n+1):
    ans1 = min(ans1,d[i+1] - d[i])
d = [0] + b + [24]
ans2 = inf
for i in range(n+1):
    ans2 = min(ans2,d[i+1] - d[i])
print(max(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
D = [int(x) for x in input().split()]
D.sort()
d = D[0]
D = [(d * (-1) ** i) % 24 for i, d in enumerate(D)]
D.sort()
for i in range(N - 1):
  d = min(D[i + 1] - D[i], d)
print(d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*13
for i in map(int, input().split()):
    a[i] += 1

ans = 0
if not (any(i>2 for i in a) or a[0] or a[12]>1):
    s = [[0]]
    for i in range(1, 13):
        nxt = []
        if a[i] == 0:
            nxt = s
        elif a[i] == 1:
            for si in s:
                nxt.append(si+[i])
                nxt.append(si+[24-i])
        else:
            for si in s:
                nxt.append(si+[i, 24-i])
        s = nxt
    for si in s:
        si.sort()
        tmp = float("Inf")
        for i in range(len(si)):
            k = abs(si[i]-si[i-1])
            k = min(k, 24-k)
            tmp = min(tmp, k)
        ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*13
for i in map(int, input().split()):
    a[i] += 1

ans = 0
if any(i>2 for i in a) or a[0] or a[12]>1:
    pass
else:
    s = [[0]]
    for i in range(1, 13):
        nxt = []
        if a[i] == 0:
            nxt = s
        elif a[i] == 1:
            for si in s:
                nxt.append(si+[i])
                nxt.append(si+[24-i])
        else:
            for si in s:
                nxt.append(si+[i, 24-i])
        s = nxt
    for si in s:
        si.sort()
        tmp = float("Inf")
        for i in range(len(si)):
            k = abs(si[i]-si[i-1])
            k = min(k, 24-k)
            tmp = min(tmp, k)
        ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = list(map(int, input().split()))
h = [0]*13
for i in d:
    h[i] += 1

ans = 0
def rec(now, state):
    global ans
    if now == 13:
        state.sort()
        m = float("Inf")
        for i in range(len(state)):
            m = min(m, min(abs(state[i-1]-state[i]), 24-abs(state[i-1]-state[i])))
        ans = max(ans, m)
    elif now == 0:
        if h[now]:
            return
        rec(now+1, state)
    elif now == 12:
        if h[now]==0:
            rec(now+1, state)
        elif h[now]==1:
            state.append(now)
            rec(now+1, state)
    else:
        if h[now]==0:
            rec(now+1, state)
        elif h[now]==1:
            s = state[:]
            s.append(now)
            rec(now+1,s)
            s = state[:]
            s.append(24-now)
            rec(now+1, s)
        elif h[now]==2:
            state.append(now)
            state.append(24-now)
            rec(now+1, state)
rec(0, [0])
print(ans)

Submission status: Accepted

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

hour = [0]

for i, d in enumerate(D):
    if i % 2 == 0:
        hour.append(d)
    else:
        hour.append(24 - d)

ans = float('inf')
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        ti = hour[i]
        tj = hour[j]
        ti, tj = ti - min(ti, tj), tj - min(ti, tj)
        gap = max(ti, tj)
        ans = min(ans, gap, 24 - gap)

print(ans)

Submission status: Accepted

Solution code:
n,t=open(0)
n=int(n)+1
d=[t if i%2else(24-t)%24for i,t in enumerate([0]+sorted(map(int,t.split())))]
print(min(abs(d[i]-d[j])for i in range(n)for j in range(i+1,n)))

Submission status: Accepted

Solution code:
#C問題
N = int(input())
D = list(map(int,input().split()))

D.sort()
left = 0
flag = 0
cir = [0 for i in range(24)]
for i in range(N):
    if D[i] == 0 or D[i] == 12:
        cir[D[i]] += 1
        if cir[D[i]] == 2:
            flag = 1
            break
    else:
        if left == 0:
            cir[D[i]]+=1
            left = 1
            if cir[D[i]] == 2:
                flag = 1
                break
        else:
            cir[24-D[i]]+=1
            left = 0
            if cir[24-D[i]] == 2:
                flag = 1
                break
                
if flag == 1:
    print(0)
else:
    MIN = 12
    point = [0]
    for i in range(24):
        if cir[i] == 1:
            point.append(i)
    point.append(0)
    #print(point)
    for i in range(len(point)-1):
        MIN = min(MIN,abs(point[i+1]-point[i]))
    print(MIN)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
d = wi()

time = [[] for _ in range(13)]

for t in d:
    time[t].append(t)

for idx, kosu in enumerate(time):
    if len(kosu) >= 3:
        print(0)
        exit()

if n > 11:
    if len(time[0]) >= 1 or len(time[12]) > 1:
        print(0)
        exit()
    else:
        print(1)
        exit()

time = functools.reduce(lambda a, b: a + b, time)
ans = INF; A = 0

for ti in itertools.product([0, 1], repeat=len(time)):  # 11**2
    kyu = []
    for kk in range(len(ti)):
        if ti[kk] == 0:
            kyu.append(time[kk])
        else:
            kyu.append(24 - time[kk])
    kyu.append(0)

    for k in itertools.combinations(kyu, 2):  # nC2
        ans = min(ans, abs(k[0] - k[1]), 24 - abs(k[0] - k[1]))
    A = max(ans, A)
    ans = INF  # 初期化を忘れない
print(A)

Submission status: Accepted

Solution code:
s=[0]*24
s[12]=1
n=int(input())
a=list(map(int,input().split()))
d=[a.count(i)for i in range(13)]
d[0]+=1
if max(d[0],d[12])<2 and max(d[1:-1])<3:
    if d[12]==1:
        s[0]=1
    t=[]
    for i in range(1,12):
        if d[i]==2:
            s[12-i]=1
            s[12+i]=1
        elif d[i]==1:
            t.append(i)
    k=0
    for i in range(2**len(t)):
        p=bin(i)[2:]
        p="0"*(len(t)-len(p))+p
        r=s[:]
        for j in range(len(t)):
            if p[j]=="0":
                r[12-t[j]]=1
            else:
                r[12+t[j]]=1
        h=0
        q=0
        ans=30
        for j in 2*r:
            if j==1:
                if h==0:
                    q=1
                    h=1
                else:
                    ans=min(q,ans)
                    q=1
            else:
                q+=1
        k=max(ans,k)
    print(k)
else:
    print(0)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
cnt=[0]*13
cnt[0]=1
ans=0
for val in arr:
  cnt[val]+=1
flag=True
for i in range(13):
  if i==0 or i==12:
    if cnt[0]>=2:
      flag=False
      break
  else:
    if cnt[i]>=3:
      flag=False
      break
if flag==False:
  print(0)
else:
  ans=0
  base=[0]*24
  base[0]=1
  if cnt[12]!=0:
    base[12]=1
  for i in range(2**11):
    pos=base[:]
    bit=format(i,'b')
    if len(bit)!=11:
      bit='0'*(11-len(bit))+bit
    for j in range(1,12):
      if cnt[j]==0:
        continue
      elif cnt[j]==2:
        pos[j]=1
        pos[24-j]=1
      else:
        if bit[j-1]=='0':
          pos[j]=1
        else:
          pos[24-j]=1
    tmp=10**10
    for j in range(24):
      if pos[j]==0:
        continue
      for k in range(1,24):
        if pos[(j+k)%24]==1:
          tmp=min(tmp,k)
          break
    ans=max(ans,tmp)
  print(ans)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
pos=[0]*24
pos[0]=1
flag=0
for val in arr:
  if flag==0:
    pos[val]+=1
    flag=1
  else:
    pos[(24-val)%24]+=1
    flag=0
ans=10**10
for i in range(24):
  if pos[i]>=2:
    ans=0
    break
  elif pos[i]==1:
    for j in range(1,24):
      if pos[(i+j)%24]>=1:
        ans=min(ans,j)
        break
  else:
    continue    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=[int(i) for i in input().split()]
d.sort()
a=1
ans=[]

def dif(lout,lin):
    lo=[i for i in lout]
    li=[i for i in lin]
    if li:
        x=li.pop()
        if x in li:
            li.remove(x)
            lo.append(x)
            lo.append(24-x)
            dif(lo,li)
        else:
            dif(lo+[x],li)
            dif(lo+[24-x],li)
    else:
        pre=0
        rt=24
        lo=sorted(lo)+[24]
        for i in lo:
            rt=min(i-pre,rt)
            pre=i
        ans.append(abs(rt))
        return

for i in range(12+1):
    if d.count(i)>2:
        a=0
if 0 in d:
    a=0
if d.count(12)>=2:
    a=0

if a==0:
    print(0)
else:
    dif([],d)
    print(max(ans))

Submission status: Accepted

Solution code:
from collections import Counter

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

if N > 23:
    print(0)
elif N > 11:
    D.append(0)
    D_count = Counter(D)
    if D_count.most_common()[0][1] > 2:
        print(0)
    else:
        print(1)
else:
    ans = 0
    for i in range(2 ** (N - 1)):
        bit = '0' + bin(i)[2:].zfill(N - 1)
        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: Accepted

Solution code:
from itertools import product, combinations

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

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

# 出力
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
b = [0]
c = [0]
for i in range(n):
    if i%2:
        b.append(a[i])
        c.append((24-a[i])%24)
    else:
        c.append(a[i])
        b.append((24-a[i])%24)
b.sort()
c.sort()
mn1=24
mn2=24
for i in range(n):
    mn1 = min(mn1,b[i+1]-b[i])
    mn2 = min(mn2,c[i+1]-c[i])
mn1 = min(mn1,b[0]+24-b[-1])
mn2 = min(mn2,c[0]+24-c[-1])
print(max(mn1,mn2))

Submission status: Accepted

Solution code:
import copy
N=int(input())

t=[0 for i in range(13)]
T=[0 for i in range(25)]
T[0]=1
T[24]=1

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

for i in range(N):
    t[D[i]]+=1
    
#print(t)
if t[0]>=1 or t[12]>=2 or max(t)>=3:
    print(0)
else:
    for i in range(len(t)):
        if t[i]==2:
            T[i]=1
            T[24-i]=1
    T[12]=t[12]
    #print(T)
    A=[[]for i in range(11)]
    for i in range(1,12):
        if t[i]==1:
            A[i-1]=[i,24-i]
    for i in range(11):
        if A[i]==[]:
            A[i]=[0,0]
    #print(A)
    
    MAX=0
    for i in range(2**11+1):
        S=copy.deepcopy(T)
        I=i
        for j in range(11):
            if (I//(2**(10-j)))==1:
                S[A[j][1]]=1
            else:
                S[A[j][0]]=1
            I%=(2**(10-j))
        L=100
        P=0
        for k in range(1,25):
            if S[k]==1:
                if L>(k-P):
                    L=(k-P)
                P=k
        if L>MAX:
            MAX=L
            #print(L)
            #print(S)
    print(MAX)

Submission status: Accepted

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

B=[0]*13
for d in D:B[d]+=1
ans=1
if max(B)>2 or max(B[0],B[12])>1:ans=0
if ans>0:
    v1=[]
    v2=[0]
    if B[12]==1:v2.append(12)
    for i,b in enumerate(B[1:-1],1):
        if b==1:v1.append(i)
        elif b==2:v2.append(i);v2.append(24-i)

    l=len(v1)
    for i in range(1<<l):
        v3=v2[:]
        for j,v in enumerate(v1):
            if i&(1<<j):
                v3.append(24-v)
            else:
                v3.append(v)
        tmp=24
        for v in v3:
            for vv in v3:
                if v==vv:continue
                tmp = min(tmp,abs(v-vv),24-abs(v-vv))
        ans = max(ans,tmp)
print(ans)

Submission status: Accepted

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

ans = 0
for s in range(0,13):
    Q = [-s,-s]
    ok = 1
    for d in D:
        if Q[0]+s <= d <= 24 - Q[1]-s:
            Q[0] = d
        elif Q[1]+s <= d <= 24 - Q[0] - s:
            Q[1] = d
        else:
            ok = 0
    if ok:
        ans = s

print(min(ans,min(D)))

Submission status: Accepted

Solution code:
from itertools import product
n = int(input())
d = [int(i) for i in input().split()]
if 0 in d or 23<n: ans = 0
elif n<12:
    ans = 0
    for i in product((0,1),repeat=n):
        x = [0,24]
        for j in range(n):
            if i[j]: x.append(d[j])
            else: x.append(24-d[j])
        x,num = sorted(x),24
        for j in range(n+1): num = min(num,x[j+1]-x[j])
        ans = max(ans,num)
else:
    d+=[0]
    f = 1
    for i in range(1,13):
        if d.count(i)>2: f = 0
    if f: ans = 1
    else: ans = 0
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from collections import Counter
from copy import copy

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
D=LIST()

L=[0]

C=Counter(D)
S=[]
for k, v in C.items():
    if v>=3:
        print(0)
        exit()
    elif v>=2:
        L.append(k)
        L.append(24-k)
    else:
        S.append(k)

mx=0
for i in range(1<<len(S)):
    L2=copy(L)
    for j in range(len(S)):
        if i>>j&1:
            L2.append(S[j])
        else:
            L2.append(24-S[j])
    L2.sort()
    L2=L2+[l+24 for l in L2]
    mn=INF
    for i in range(1, len(L2)):
        mn=min(mn, abs(L2[i-1]-L2[i])%24)
    mx=max(mx, mn)
print(mx)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    input_raw = read_func().strip().split()
    D = []
    for i in range(N):
        D.append(long(input_raw[i]))

    D.sort()

    D_ = []
    flag = False
    for i in range(N):
        if flag == False:
            D_.append(D[i])
            flag = True
        else:
            D_.append(24 - D[i])
            flag = False

    min_score = sys.maxint
    for i in range(N):
        for j in range(i + 1, N):
            min_score = min(min_score, abs(D_[i] - D_[j]))

    for i in range(N):
        min_score = min(min_score, D[i])

    print min_score


if __name__ == '__main__':
    main()