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: Wrong Answer

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

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

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:
    ans=dif([],d)
    print(ans)

Submission status: Wrong Answer

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

if N > 24:
    print(0)
else:
    ans = 0
    for i in range(2 ** N):
        bit = bin(i)[2:].zfill(N)
        D_mod24 = [0] * N
        for j in range(N):
            if bit[j] == '0':
                D_mod24[j] = D[j]
            else:
                D_mod24[j] = 24 - D[j]
        D_mod24.sort()
        ans_temp = min(max(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: Wrong Answer

Solution code:
from itertools import product, combinations

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

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

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product, combinations

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

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

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product, combinations

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

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

# 出力
print(ans)

Submission status: Wrong Answer

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])
print(max(mn1,mn2))

Submission status: Wrong Answer

Solution code:
a= int(input())
x = list(map(int, input().split()))
x.sort()
l = []
for n,i in enumerate(x):
    if n%2 != 0:
        l.append(i)
    else:
        l.append((24-i)%24)

s = 0
l.sort()
t = l[-1]
m = 0
for i in l[:-1]:
    s = max(m,abs(t-i))
    t = i
if s == 24:s = 0
print(s)

Submission status: Wrong Answer

Solution code:
a= int(input())
x = list(map(int, input().split()))
x.sort()
l = []
for n,i in enumerate(x):
    if n%2 != 0:
        l.append(i)
    else:
        l.append((24-i)%24)

s = 0
l.sort()
t = l[-1]
m = 0
for i in l[:-1]:
    s = max(m,abs(t-i))
    t = i
print(s)

Submission status: Wrong Answer

Solution code:
a= int(input())
x = list(map(int, input().split()))
x.sort()
l = []
for n,i in enumerate(x):
    if n%2 != 0:
        l.append(i)
    else:
        l.append(24-i)

s = 0
l.sort()
t = l[-1]
m = 0
for i in l[:-1]:
    s = max(m,abs(t-i))
    t = i
print(s)

Submission status: Wrong Answer

Solution code:
a= int(input())
x = list(map(int, input().split()))
x.sort()
l = []
for n,i in enumerate(x):
    if n%2 == 0:
        l.append(i)
    else:
        l.append(24-i)

s = 0
l.sort()
t = l[-1]
m = 0
for i in l[:-1]:
    s = max(m,abs(t-i))
    t = i
print(s)

Submission status: Wrong Answer

Solution code:
a= int(input())
x = list(map(int, input().split()))
x.sort()
l = []
for n,i in enumerate(x):
    if n%2 == 0:
        l.append(i)
    else:
        l.append(24-i)

s = 0
l.sort()
t = l[-1]
m = 0
for i in l[:-1]:
    s = max(m,abs(t-i))
print(s)

Submission status: Wrong Answer

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

def f(list,n):
  list.sort()
  r=[0]*n
  r[0]=min(list[0],24-list[0])
  for i in range(1,n):
    r[i]+=min(list[i]-list[i-1],24-(list[i]-list[i-1]))
  return min(r)
ans=0
for c in itertools.product(range(2),repeat=n):
  DD=[0]*n
  for i in range(n):
    if c[i]==0:
      DD[i]+=D[i]
    else:
      DD[i]+=24-D[i]
  ans=max(ans,f(DD,n))
print(ans)

Submission status: Wrong Answer

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**10+1):
        S=copy.deepcopy(T)
        for j in range(11):
            if (i//(2**(9-j)))==1:
                S[A[j][1]]=1
            else:
                S[A[j][0]]=1
        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: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = D[0]
else:
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			if a > 6:
				a = 12 - a
			if b > 6:
				b = 12 - b
			if c > 6:
				c = 12 - c
			if d > 6:
				d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	if D[0] != 0:
		answer = max(D[0], 24 - D[0])
else:
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			if a > 6:
				a = 12 - a
			if b > 6:
				b = 12 - b
			if c > 6:
				c = 12 - c
			if d > 6:
				d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	if D[0] != 0:
		answer = max(D[0], 24 - D[0])
else:
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			# if a > 6:
			# 	a = 12 - a
			# if b > 6:
			# 	b = 12 - b
			# if c > 6:
			# 	c = 12 - c
			# if d > 6:
			# 	d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	a, b = max(a, b), min(a, b)
	b += 24 - a
	return min(b, 24 - b)
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = D[0]
else:
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			temp_s = min(min(a, b), min(c, d))
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	a, b = max(a, b), min(a, b)
	b += 24 - a
	return min(b, 24 - b)
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = min(D[0], 12 - D[0])
else:
	for i in range(N - 1):
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			s = min(min(a, b), min(c, d))
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	a, b = max(a, b), min(a, b)
	b += 24 - a
	return min(b, 24 - b)
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = D[0]
else:
	for i in range(N - 1):
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			s = min(min(a, b), min(c, d))
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	a, b = max(a, b), min(a, b)
	b += 24 - a
	return min(b, 24 - b)
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = D[0]
else:
	for i in range(N - 1):
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			s = min(min(a, b), min(c, d))
		answer = max(answer, s)
	if answer > 6:
		answer = 12 - answer
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	a, b = max(a, b), min(a, b)
	b += 24 - a
	return min(b, 24 - b)
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = min(D[0], 12 - D[0])
else:
	for i in range(N - 1):
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			s = min(min(a, b), min(c, d))
		answer = max(answer, s)
if answer > 6:
	answer = 12 - answer
print(answer)

Submission status: Wrong Answer

Solution code:
def g(a, b):
	if a == b:
		return 1e9
	else:
		return max(a, b) - min(a, b)

N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
if N == 1:
	answer = min(D[0], 12 - D[0])
else:
	for i in range(N - 1):
		for j in range(i + 1, N):
			A = D[i]
			AA = 24 - D[i]
			B = D[j]
			BB = 24 - D[j]
			a = g(A, B)
			b = g(AA, B)
			c = g(A, BB)
			d = g(AA, BB)
			s = min(min(a, b), min(c, d))
		answer = max(answer, s)
if answer > 6:
	answer = 12 - answer
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
if N == 1:
	answer = min(D[0], 24 - D[0])
else:
	answer = 0
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			a = min(a, 24 - a)
			b = abs(24 - D[i] - D[j])
			b = min(b, 24 - b)
			c = abs(D[i] - (24 - D[j]))
			c = min(c, 24 - c)
			d = abs(24 - D[i] - (24 - D[j]))
			d = min(d, 24 - d)
			if a > 6:
				a = 12 - a
			if b > 6:
				b = 12 - b
			if c > 6:
				c = 12 - c
			if d > 6:
				d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
if answer > 6:
	answer = 12 - answer
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
if N == 1:
	answer = D[i]
	# if answer > 6:
	# 	answer = 12 - answer
else:
	answer = 0
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			if a > 6:
				a = 12 - a
			if b > 6:
				b = 12 - b
			if c > 6:
				c = 12 - c
			if d > 6:
				d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
if N == 1:
	answer = D[i]
	if answer > 6:
		answer = 12 - answer
else:
	answer = 0
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			if a > 6:
				a = 12 - a
			if b > 6:
				b = 12 - b
			if c > 6:
				c = 12 - c
			if d > 6:
				d = 12 - d
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
if N == 1:
	print(D[0])
else:
	answer = 0
	for i in range(N - 1):
		s = 1e9
		for j in range(i + 1, N):	
			a = abs(D[i] - D[j])
			b = abs(24 - D[i] - D[j])
			c = abs(D[i] - (24 - D[j]))
			d = abs(24 - D[i] - (24 - D[j]))
			temp_s = 1e9
			if a != 0:
				temp_s = min(temp_s, a)
			if b != 0:
				temp_s = min(temp_s, b)
			if c != 0:
				temp_s = min(temp_s, c)
			if d != 0:
				temp_s = min(temp_s, d)
			s = min(s, temp_s)
		answer = max(answer, s)
	print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = input().split()
for i in range(N):
	D[i] = int(D[i])
answer = 0
for i in range(N - 1):
	s = 1e9
	for j in range(i + 1, N):	
		a = abs(D[i] - D[j])
		b = abs(24 - D[i] - D[j])
		c = abs(D[i] - (24 - D[j]))
		d = abs(24 - D[i] - (24 - D[j]))
		temp_s = 1e9
		if a != 0:
			temp_s = min(temp_s, a)
		if b != 0:
			temp_s = min(temp_s, b)
		if c != 0:
			temp_s = min(temp_s, c)
		if d != 0:
			temp_s = min(temp_s, d)
		s = min(s, temp_s)
	answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

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))
        ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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 = max(min_score, D[i])
    if N==1 :
        min_score = D[0]

    print min_score


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(n, ds):
    if n >= 24: return 0
    if max([ds.count(v) for v in range(13)]) >= 3:
        return 0
    if ds.count(0) > 0: return 0
    if ds.count(12) > 1: return 0
    if n >= 12: return 1
    
    res = 0
    for mask in range(1<<n):
        pos = ds[::]
        s = 24
        for i in range(n):
            if mask & 1<<i:
                pos[i] = 24 - pos[i]
        pos.append(0)
        for i in range(n):
            for j in range(i):
                s = min(s, abs(pos[i] - pos[j]), 24 - abs(pos[i] - pos[j]))
        res = max(res, s)
    return res

n = int(input())
ds = list(map(int, input().split()))
print(solve(n, ds))

Submission status: Wrong Answer

Solution code:
def solve(n, ds):
    if n > 24: return 0
    if max([ds.count(v) for v in range(13)]) >= 3:
        return 0
    if ds.count(0) > 0: return 0
    if ds.count(12) > 1: return 0
    if n > 12: return 1
    
    res = 0
    for mask in range(1<<n):
        pos = ds[::]
        s = 24
        for i in range(n):
            if mask & 1<<i:
                pos[i] = 24 - pos[i]
        for i in range(n):
            for j in range(i):
                s = min(s, abs(pos[i] - pos[j]), 24 - abs(pos[i] - pos[j]))
        res = max(res, s)
    return res

n = int(input())
ds = list(map(int, input().split()))
print(solve(n, ds))

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    D = list(map(int, input().split()))
    c = [0] * 13
    for d in D:
        c[d] += 1

    if max(c) >= 3 or c[0] >= 2 or c[12] >= 2 or N == 1:
        print(0)
        return

    ans = 0
    # 2 ^ nの組み合わせの全列挙
    n = 11  # 0と12は選択の余地がない
    for b in range(2 ** n):
        a = []
        if c[0]:
            a.append(0)
        if c[12]:
            a.append(12)

        for i in range(n):
            d = i + 1
            if c[d] == 0:
                continue
            elif c[d] == 2:
                a += [d, 24 - d]
            elif b & 1 << i:
                a.append(d)
            else:
                a.append(24 - d)
        a.sort()
        mini = INF
        for i in range(len(a) - 1):
            mini = min(mini, abs(a[i] - a[i + 1]))
        mini = min(mini, abs(24 + a[0] - a[-1]))
        
        ans = max(ans, mini)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    D = list(map(int, input().split()))
    c = [0] * 13
    for d in D:
        c[d] += 1

    if max(c) >= 3 or c[0] >= 2 or c[12] >= 2 or N == 1:
        print(0)
        return

    ans = 0
    # 2 ^ nの組み合わせの全列挙
    n = 11  # 0と12は選択の余地がない
    for b in range(2 ** n):
        a = []
        if c[0]:
            a.append(0)
        if c[12]:
            a.append(12)
        for i in range(n):
            d = i + 1
            if c[d] == 0:
                continue
            if c[d] == 2:
                a += [d, 24 - d]
                continue
            if b & 1 << i:
                a.append(d)
            else:
                a.append(24 - d)
        a.sort()
        ans = max(ans, min([abs(a[i] - a[i + 1]) for i in range(len(a) - 1)]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

    ans=0

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
def main():
    n=int(input())
    d=list(map(int,input().split()))
    if n>=24 or 0 in d:
        print(0)
    else:
        k=min(d)
        ans=0
        for i in range(2**n):
            time=[]
            for j in range(n):
                if (i>>j)&1:
                    time.append(d[j])
                else:
                    time.append(24-d[j])
            time.sort()
            mini=k
            for j in range(n-1):
                mini=min(mini,time[j+1]-time[j])
            ans=max(ans,mini)
        print(ans)
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
from typing import List, Counter, Tuple
from collections import Counter
from itertools import permutations


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


INF = 10**9

def solve() -> int:
    N = read_int()
    D = read_ints()
    if N>23:
        return 0
    max_distance = -INF
    for mask in range(1<<N):
        distances = [0]
        min_distance = INF
        temp = mask
        for i in range(N):
            if temp&1 == 0:
                d = D[i]
            else:
                d = 24-D[i]
            current = min(min(abs(d0-d), 24-abs(d0-d)) for d0 in distances)
            distances.append(d)
            if current < min_distance:
                min_distance = current
            temp >>= 1
        if min_distance > max_distance:
            max_distance = min_distance
    return max_distance
        


if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit


@njit("i1(i1,i1[:])", cache=True)
def solve(N, D):
    ans = 0
    for i in range(1 << N):
        clocks = np.zeros((N + 2), dtype=np.int8)
        clocks[-1] = 24
        for j in range(N):
            if i >> j & 1:
                clocks[j + 1] = D[j]
            else:
                clocks[j + 1] = 24 - D[j]
        clocks.sort()
        ans = max(ans, np.min(np.diff(clocks)))
    return ans


N = int(input())
D = np.array(input().split(), dtype=np.int8)
if N >= 24:
    print(0)
else:
    print(solve(N, D))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def solve(N, D):
    ans = 0
    for i in range(1 << N):
        clocks = np.zeros((N + 2), dtype=np.int8)
        clocks[-1] = 24
        for j in range(N):
            if i >> j & 1:
                clocks[j + 1] = D[j]
            else:
                clocks[j + 1] = 24 - D[j]
        clocks.sort()
        ans = max(ans, np.min(np.diff(clocks)))
    return ans


N = int(input())
D = np.array(input().split(), dtype=np.int8)
if N >= 24:
    print(0)
else:
    print(solve(N, D))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit


@njit("i1(i1,i1[:])")
def solve(N, D):
    ans = 0
    for i in range(1 << N):
        clocks = np.zeros((N + 2), dtype=np.int8)
        clocks[-1] = 24
        for j in range(N):
            if i >> j & 1:
                clocks[j + 1] = D[j]
            else:
                clocks[j + 1] = 24 - D[j]
        clocks.sort()
        ans = max(ans, np.min(np.diff(clocks)))
    return ans


N = int(input())
D = np.array(input().split(), dtype=np.int8)
if N >= 24:
    print(0)
else:
    print(solve(N, D))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

ans = 0
for pr in product([0, 1], repeat=N):
    clocks = []
    for i, is_flipped in enumerate(pr):
        if is_flipped:
            clocks.append(D[i])
        else:
            clocks.append(24 - D[i])
    res = 25
    clocks = [0] + sorted(clocks) + [24]
    for cf, cb in zip(clocks, clocks[1:]):
        res = min(res, cb - cf)
    ans = max(ans, res)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarizeList(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

n=I()
l=LI()
l+=[0]

def dfs(lst,cnt):
  if cnt>n:
    # print(lst)
    mn=24
    lenlst=len(lst)
    st=-heapq.heappop(lst)
    lastyou=st
    while lst:
      nxt=-heapq.heappop(lst)
      mn=min(mn,abs(st-nxt))
      st=nxt
    mn=min(mn,abs(lastyou-st))
    return mn

  lst1=copy.deepcopy(lst)
  lst2=copy.deepcopy(lst)
  heapq.heappush(lst1,-l[cnt])
  heapq.heappush(lst2,-(24-l[cnt]))
  x1=dfs(lst1,cnt+1)
  x2=dfs(lst2,cnt+1)

  return max(x1,x2)

l.sort()
sl=summarizeList(l)

for x,c in sl:
  if c>2:
    print(0)
    exit()

q=[]
heapq.heappush(q,0)
print(dfs(q,0))

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarizeList(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

n=I()
l=LI()
l+=[0]

def dfs(lst,cnt):
  if cnt>n:
    mn=24
    lst.sort()
    lenlst=len(lst)
    for i in range(lenlst):
      mn=min(mn,abs(lst[i-1]-lst[i]))
    return mn

  x1=dfs(lst+[l[cnt]],cnt+1)
  x2=dfs(lst+[24-l[cnt]],cnt+1)

  return max(x1,x2)

l.sort()
sl=summarizeList(l)

for x,c in sl:
  if c>2:
    print(0)
    exit()

print(dfs([0],0))

Submission status: Time Limit Exceeded

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

if N >= 24:
    print(0)

else:
    res = 0
    for bit in range(2**N):
        d = [0 for _ in range(24)]
        for i in range(N):
            if (bit >> i) & 1:
                d[D[i]] += 1
            else:
                d[(24 - D[i]) % 24] += 1
        md = 25
        tmp = 0
        for i in range(24):
            if d[i] > 1:
                md = 0
                break
            elif d[i] == 1:
                md = min(md, i - tmp)
                tmp = i
        md = min(24 - tmp, md)
        res = max(res, md)
    print(res)