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:
import sys
import random
N = int(input())
D = [int(d) for d in input().split()]

if 0 in D:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] < 12:
        D[i] = 24 - D[i]
D.append(0)

max_val = 0

for i in range(5000):
    D.sort()
    min_val = 24
    min_idx = 0
    prev = D[0]
    for i in range(1, N + 1):
        if D[i] - prev < min_val:
            min_val = D[i] - prev
            min_idx = i
        prev = D[i]
    if min_val > max_val:
        max_val = min_val
    if random.random() > 0.5:
        D[min_idx - 1] = 24 - D[min_idx - 1]
    else:
        D[min_idx] = 24 - D[min_idx]

print(max_val)

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    arr = [int(i) for i in input().split()]
    arrset = set(arr)
 
    if max(arrset) == 0:
        print(0)
        return
    
    ans = 0 
    for i, v in enumerate(arr):
        for j, w in enumerate(arr):
            if i == j and v == w:
                continue
            tarr = [i, j, 24 - i, 24 - j]
            tans = []
            for k in tarr:
                for l in tarr:
                    tans.append(min(abs(k - l), 24 - abs(k - l)))
            ans = max(ans, max(tans))
    print(ans) 
 
 
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
ds = list(map(int, input().split()))
ans = 0
for i in range(n):
    mx = 1000
    for j in range(n):
        if i!=j:
            if ds[i]==ds[j]:
                mx = min(mx, abs(ds[i]-(24-ds[j])))
            else:
                mx = min(mx, abs(ds[i]-ds[j]), abs(ds[i]-(24-ds[j])))
    ans = max(ans, mx)
print(ans if n>1 else 0)

Submission status: Wrong Answer

Solution code:
# C問題

def solve(List1,List2):
	maxval = -1
	for i in List1:
		for j in List2:
			dif = abs(i-j)
			if dif > 12:
				continue
			if maxval < dif:
				maxval = dif

	return maxval

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

D = list(map(lambda x:[x,24-x],D))

D.append([0,24])

table = []
for i in range(N+1):
	for j in range(i+1,N+1):
		table.append(solve(D[i],D[j])) 

print(min(table))

Submission status: Wrong Answer

Solution code:
# C問題

def solve(List1,List2):
	maxval = -1
	for i in List1:
		for j in List2:
			if maxval < abs(i - j):
				maxval = abs(i - j)
	return maxval

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

D = list(map(lambda x:[x,24-x],D))

D.append([0,24])

table = []
for i in range(N+1):
	for j in range(i+1,N+1):
		table.append(solve(D[i],D[j])) 

print(min(table))

Submission status: Wrong Answer

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

if 0 in D:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] < 12:
        D[i] = 24 - D[i]
D.append(0)

max_val = 0

for i in range(N):
    D.sort()
    min_val = 24
    min_idx = 0
    prev = D[0]
    for i in range(1, N + 1):
        if D[i] - prev < min_val:
            min_val = D[i] - prev
            min_idx = i
        prev = D[i]
    if min_val > max_val:
        max_val = min_val
    if D[min_idx] == 12:
        D[min_idx - 1] = 24 - D[min_idx - 1]
    else:
        D[min_idx] = 24 - D[min_idx]

print(max_val)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import itertools

N = int(input())
Ds = list(map(int, input().split())) + [0]

cnt = Counter(Ds)

if max(cnt.values()) >= 3:
    print(0)
    exit()

citys = []
rest = []
for k, v in cnt.items():
    if v == 2:
        if k == 12:
            print(0)
            exit()
        citys.append(v)
        citys.append(24 -v)
    else:
        rest.append(k)


def check(citys):
    if len(citys) <= 1:
        return 24

    citys.sort()

    ans = 24
    prev = 24 - citys[-1]
    for c in citys:
        ans = min(ans, c - prev)
        prev = c

    return ans


ans = 0
for ops in itertools.product('+-', repeat=len(rest)):
    citys2 = citys.copy()
    for i in range(len(rest)):
        if ops[i] == '+':
            citys2.append(rest[i])
        else:
            citys2.append(24 - rest[i])

    ans = max(ans, check(citys2))

print(ans)

Submission status: Wrong Answer

Solution code:
def timdiff(dif):
    l=[]
    for i in range(24):
        if dif[i]==1:
            l+=[i]
        if dif[i]>=2:
            return 0
    m=24
    for i in range(len(l)):
        if i < len(l)-1:
            m=min(m,l[i+1]-l[i])
    if len(l)>=2:
        m=min(m,24-l[len(l)-1])
    return m

n=int(input())
d=list(map(int,input().split(' ')))
diff=[0]*24
if n>=24:
    print(0)
elif n<12: #global search
    m=0
    for i in range(2**n):
        for j in range(1,24):
            diff[j]=0
        diff[0]=1
        for j in range(n):
            if (i >> j)%2==1:
                diff[d[j]]+=1
            elif d[j]==0:
                diff[0]+=1
            else:
                diff[24-d[j]]+=1
        m=max(m,timdiff(diff))
    print(m)
else: #judge 0 or 1
    bl=False
    for i in range(13):
        if bl:
            break
        m=0
        for j in range(n):
            if d[j] == i:
                m+=1
            if m>2 or (i==0 and d[j]==0) or (i==12 and m>=2):
                break
        bl=True
    if bl:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

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

if 0 in D:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] < 12:
        D[i] = 24 - D[i]
D.append(0)

max_val = 0

for i in range(N):
    D.sort()
    min_val = 24
    min_idx = 0
    prev = D[0]
    for i in range(1, N + 1):
        if D[i] - prev < min_val:
            min_val = D[i] - prev
            min_idx = i
        prev = D[i]
    if min_val > max_val:
        max_val = min_val
    D[min_idx] = 24 - D[min_idx]

print(max_val)

Submission status: Wrong Answer

Solution code:
def timdiff(dif):
    l=[]
    for i in range(24):
        if diff[i]==1:
            l+=[i]
        if diff[i]>=2:
            return 0
    m=24
    for i in range(len(l)):
        if i < len(l)-1:
            m=min(m,l[i+1]-l[i])
    if len(l)>=2:
        m=min(m,24-l[len(l)-1])
    return m

n=int(input())
d=list(map(int,input().split(' ')))
diff=[0]*24
if n>=24:
    print(0)
elif n<12: #global search
    m=0
    for i in range(2**n):
        for j in range(1,24):
            diff[j]=0
        diff[0]=1
        for j in range(n):
            if (i >> j)%2==1:
                diff[d[j]]+=1
            elif d[j]==0:
                diff[0]+=1
            else:
                diff[24-d[j]]+=1
        m=max(m,timdiff(diff))
    print(m)
else: #judge 0 or 1
    bl=False
    for i in range(13):
        if bl:
            break
        m=0
        for j in range(n):
            if d[j] == i:
                m+=1
            if m>2 or (i%12==0 and d[j]%12==0):
                break
        bl=True
    if bl:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

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

if 0 in D:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] > 12:
        D[i] = 24 - D[i]
D.append(0)

max_val = 0

for i in range(N ** 2):
    D.sort()
    min_val = 24
    min_idx = 0
    prev = D[0]
    for i in range(1, N + 1):
        if D[i] - prev < min_val:
            min_val = D[i] - prev
            min_idx = i
        prev = D[i]
    if min_val > max_val:
        max_val = min_val
    D[min_idx] = 24 - D[min_idx]

print(max_val)

Submission status: Wrong Answer

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

if 0 in D:
    print(0)
    sys.exit()

for i in range(N):
    if D[i] > 12:
        D[i] = 24 - D[i]
D.append(0)

max_val = 0

for i in range(N):
    D.sort()
    min_val = 24
    min_idx = 0
    prev = D[0]
    for i in range(1, N + 1):
        if D[i] - prev < min_val:
            min_val = D[i] - prev
            min_idx = i
        prev = D[i]
    if min_val > max_val:
        max_val = min_val
    D[min_idx] = 24 - D[min_idx]

print(max_val)

Submission status: Wrong Answer

Solution code:
n = int(input())
dn = list(map(int, input().split()))
all_time = []
for i in range(n):
    all_time.append(dn[i])
    all_time.append(24 - dn[i])
all_time.sort()
print(abs(all_time[1] - all_time[-1]))

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    arr = [0] + [int(i) for i in input().split()]
    arrset = set(arr)

    if max(arrset) == 0:
        print(0)
        return
    
    ans = 12
    for i in arrset:
        for j in arrset:
            i24 = 24 - i
            j24 = 24 - j
            t = [e for e in [abs(i - j), abs(i24 - j), abs(i - j24), abs(i24 - j24)] if e > 0]
            if t:
                ans = min(ans, min(t))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
d = map(int, raw_input().split())
s = 12
for i in xrange(N):
    s = min(s, min(d[i], 24 - d[i]))

for i in xrange(N):
    x = d[i]
    for j in xrange(i + 1, N):
        d1 = (d[j] - x + 24) % 24
        dm1 = min(d1, 24 - d1)
        d2 = (24 - d[j] - x + 24) % 24
        dm2 = min(d2, 24 - d2)
        if dm1 > dm2:
            x = d[j]
            s = min(s, dm1)
        else:
            x = 24 - d[j]
            s = min(s, dm2)
print s

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
d = map(int, raw_input().split())
s = 12
for i in xrange(N):
    s = min(s, min(d[i], 24 - d[i]))

for i in xrange(N):
    x = d[i]
    for j in xrange(i + 1, N):
        d1 = (d[j] - x + 24) % 24
        d2 = (24 - d[j] - x + 24) % 24
        if d1 > d2:
            x = d[j]
            s = min(s, min(d1, 24 - d1))
        else:
            x = 24 - d[j]
            s = min(s, min(d2, 24 - d2))
print s

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import itertools


dd = {}
for i in range(13):
    for j in range(i, 13):
        dd[(i, j)] = max(min(i + j, 24 - i - j), abs(i - j))

def main():
    n = int(input())
    d = list(map(int, input().split()))
    d.append(0)
    s = 12
    for i in range(n + 1):
        a = d[i]
        for j in range(i + 1, n + 1):
            b = d[j]
            s = min(s, dd[(min(a, b), max(a, b))])
    return s

print(main())

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
D = list(map(int,input().split()))
# from random import randint

# n = randint(1, 50)
# D = [randint(0, 12) for i in range(n)]

C = [0] * 13
C[0] = 1
for i in range(n):
    C[D[i]] += 1

if C[0] >= 2 or C[12] >=2:
    print(0)
    sys.exit()

for i in range(13):
    if C[i] >= 3:
        print(0)
        sys.exit()

D = []
for i in range(13):
    for j in range(C[i]):
        D.append(i)
n = len(D)
A = [0] * n
ans0 = 0
for i in range(2**n):
    ib = format(i, "b").zfill(n)
    for j in range(n):
        A[j] = D[j] * (-1) ** int(ib[j])
    ans = 30
    for j in range(n):
        for k in range(j+1, n):
            di = abs(A[k] - A[j])
            # print(ans)
            ans = min(ans, min(di, 24-di))
            if ans <= ans0:
                break
        if ans <= ans0:
            break
    # print(ib, ans0,ans)
    ans0 = max(ans0, ans)
print(ans0)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
D = list(map(int,input().split()))
# from random import randint

# n = randint(1, 50)
# D = [randint(0, 12) for i in range(n)]

C = [0] * 13
C[0] = 1
for i in range(n):
    C[D[i]] += 1

if C[0] >= 2 or C[12] >=2:
    print(0)
    sys.exit()

for i in range(13):
    if C[i] >= 3:
        print(0)
        sys.exit()

D = []
for i in range(13):
    for j in range(C[i]):
        D.append(i)
n = len(D)
A = [0] * n
ans0 = 0
for i in range(2**n):
    ib = format(i, "b").zfill(n)
    for j in range(n):
        A[j] = D[j] * (-1) ** int(ib[j])
    ans = 30
    for j in range(n):
        for k in range(j+1, n):
            di = abs(A[k] - A[j])
            # print(ans)
            ans = min(ans, min(di, 24-di))
    # print(ib, ans0,ans)
    ans0 = max(ans0, ans)
print(ans0)

Submission status: Time Limit Exceeded

Solution code:
n=input()
a=map(int,raw_input().split())
r=0
for i in range(1<<n):
 b=[a[j]*((i>>j&1)*2-1)%24 for j in range(n)]+[0]
 s=24
 for j in range(n):
  for k in range(j+1,n+1):s=min(s,(b[j]-b[k])%24,(b[k]-b[j])%24)
 r=max(r,s)
print r

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
D = list(map(int,input().split()))
# from random import randint

# n = randint(1, 50)
# D = [randint(0, 12) for i in range(n)]

C = [0] * 13
C[0] = 1
for i in range(n):
    C[D[i]] += 1

if C[0] >= 2 or C[12] >=2:
    print(0)
    sys.exit()

for i in range(13):
    if C[i] >= 3:
        print(0)
        sys.exit()

# D = []
# for i in range(13):
#     for j in range(C[i]):
#         D.append(i)
# n = len(D)
# A = [0] * n

# print(C)
ans0 = 0
for i in range(2**13):
    zero = 0
    ib = format(i, "b").zfill(13)
    A = []
    for j in range(13):
        if C[j] == 1:
            A.append(j * (-1) ** int(ib[j]))
        if C[j] == 2:
            if int(ib[j]) == 0:
                A.append(j)
                A.append(-j)
            else:
                zero = 1
    if zero == 1:
        continue
    # print("not cont")
    ans = 30
    # if len(A)>1:
        # print(A)
    # print(ib, A)
    for j in range(len(A)):
        for k in range(j+1, len(A)):
            di = abs(A[k] - A[j])
            # print(ans)
            ans = min(ans, min(di, 24-di))
            if ans <= ans0:
                break
        if ans <= ans0:
            break
    # print(ib, ans0,ans)
    ans0 = max(ans0, ans)
print(ans0)

Submission status: Accepted

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    d = list(map(int, input().split()))
    if n == 1:
        print(d[0])
        exit()
    dc = defaultdict(int)
    dc[0] = 1
    for i in range(n):
        v = d[i]
        if dc[v] >= 2 or (dc[v] == 1 and (v == 12 or v == 0)):
            print(0)
            exit()
        dc[v] += 1
    
    dd = []
    ddt = []
    for i in range(0, 13):
        if (i == 0 or i == 12) and dc[i] == 1:
            ddt.append(i)
        elif dc[i] == 1:
            dd.append(i)
        elif dc[i] == 2:
            ddt.append(i)
            ddt.append(24 - i)


    
    ans = 0
    for i in range(1 << len(dd)):
        d3 = []
        for j in range(len(ddt)):
            d3.append(ddt[j])

        for j in range(len(dd)):
            if (i & (1 << j)):
                d3.append(dd[j])
            else:
                d3.append(24 - dd[j])
        
        d3 = sorted(d3)
        t = float("inf")
        for i in range(len(d3) - 1):
            t = min(t, d3[i + 1] - d3[i])
        t = min(t , 24 - d3[-1])
        ans = max(t, ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	D = getlist()
	if N >= 24:
		print(0)
	elif N >= 12:
		d = defaultdict(int)
		judge = "Yes"
		for i in D:
			d[i] += 1
		if d[0] >= 1:
			judge = "No"
		if d[12] >= 2:
			judge = "No"
		for i in range(1, 12):
			if d[i] >= 3:
				judge = "No"
		if judge == "No":
			print(0)
		else:
			print(1)
	else:
		ans = 0
		for i in range(1 << N):
			L = [0, 24]
			for j in range(N):
				if ((i >> j) & 1) == 1:
					L.append(D[j])
				else:
					L.append(24 - D[j])
			L = sorted(L)
			val = 24
			for i in range(N + 1):
				val = min(val, L[i + 1] - L[i])
			ans = max(ans, val)
		print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque


def main():
    num = int(input())
    data = list(map(int, input().split()))
    count = Counter(data)

    ans = 0
    for i in range(2 ** 11):
        bin_list = bin(i)[2:]
        bin_list = bin_list.zfill(11)
        bin_list = list(map(int, list(bin_list)))
        now_data = [0, 24]

        if count[12] >= 2:
            print(0)
            sys.exit()
        elif count[12] == 1:
            now_data.append(12)

        if count[0] >= 1:
            print(0)
            sys.exit()


        for j in range(1, 12):
            if count[j] == 0:
                continue
            elif count[j] >= 3:
                print(0)
                sys.exit()
            elif count[j] == 2:
                now_data.append(j)
                now_data.append(24 - j)
            elif bin_list[j - 1]:
                now_data.append(j)
            else:
                now_data.append(24 - j)
        now_data.sort()

        kari = 12
        # print(now_data)
        for j in range(len(now_data) - 1):
            kari = min(kari, now_data[j + 1] - now_data[j])
        ans = max(ans, kari)

    print(ans)




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

Submission status: Accepted

Solution code:
N = int(input())
D = sorted([int(_) for _ in input().split()])
#最小値の最大値
if D[0] == 0:
    print(0)
    exit()

p = m = 0
a = 99

for d in D:
    #asign positive
    e = min(a, d - p, 24 - d - m)
    #asign negative
    f = min(a, d - m, 24 - d - p)
    if e < f:
        a = f
        m = d
    else:
        a = e
        p = d
print(a)

Submission status: Accepted

Solution code:
from copy import copy
import sys
from collections import Counter
n = int(input())
d = sorted(list(map(int, input().split())))
if d[0] == 0:
	print(0)
	sys.exit()
c = Counter(d)
if max(c.values()) >= 3:
	print(0)
	sys.exit()
if c[12] >= 2:
	print(0)
	sys.exit()

reserved = []
alternatives = []
for k, v in c.items():
	if v == 2:
		reserved.append(k)
		reserved.append(24-k)
	elif k == 12:
		reserved.append(12)
	else:
		alternatives.append(k)

m = len(alternatives)
#print(m)
ans = 0
for i in range(2**m):
	zones = copy(reserved)
	for j, a in enumerate(alternatives):
		if (i>>j)&1:
			zones.append(24-a)
		else:
			zones.append(a)
	zones.sort()
	zones = [0] + zones + [24]
	#print(zones)
	res = 24
	for j in range(len(zones)-1):
		res = min(res, zones[j+1]-zones[j])
	ans = max(ans, res)

print(ans)

Submission status: Accepted

Solution code:
import itertools


def calc(s):
    ret = 24
    for i in range(1, len(s)):
        ret = min(ret, s[i] - s[i-1])
    return ret


def main():
    N = int(input())
    D = list(map(int, input().split()))
    if D.count(0) >= 1 or D.count(12) >= 2:
        print(0)
        return
    cnt = [0] * 13
    for d in D:
        cnt[d] += 1
    fixed = [0, 24]
    variable = []
    for i in range(1, 12):
        if cnt[i] >= 3:
            print(0)
            return
        elif cnt[i] == 2:
            fixed.append(i)
            fixed.append(24-i)
        elif cnt[i] == 1:
            variable.append(i)
    if cnt[12] == 1:
        fixed.append(12)
    ans = 0
    for p in itertools.product((False, True), repeat=len(variable)):
        s = list(fixed)
        for i in range(len(variable)):
            if p[i]:
                s.append(variable[i])
            else:
                s.append(24-variable[i])
        s.sort()
        ans = max(ans, calc(s))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import itertools

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

counter = [1] + [0] * 12
for x in D:
    counter[x] += 1

bl = counter[0] > 1 or counter[12] > 1 or any(x >= 3 for x in counter)
if bl:
    print(0)
    exit()

itrs = [[[0]]]
for x in range(1,12):
    if counter[x] == 0:
        itr = [[]]
    elif counter[x] == 1:
        itr = [[x],[-x]]
    else:
        itr = [[x,-x]]
    itrs.append(itr)
itrs.append([[]] if counter[12] == 0 else [[12]])

def F(arr):
    f = lambda x,y: min((x-y)%24, (y-x)%24)
    return min(f(x,y) for x,y in itertools.combinations(arr,2))

answer = 0
for p in itertools.product(*itrs):
    arr = []
    for x in p:
        arr += x
    answer = max(answer, F(arr))

print(answer)

Submission status: Accepted

Solution code:
import copy

N=int(input())
D=list(map(int,input().split()))
D.sort()
time=[]
kakutei=[]
tmp=0
for d in D: 
  if d==0:
    print(0)
    exit()
  elif tmp==d:
    count+=1
    if count>=2 or (d==12 and count>=1):
      print(0)
      exit()
  else:
    count=0
  if d not in time:
    if d==12:
      kakutei.append(d)
    else:
      time.append(d)
  elif 24-d not in time:
    kakutei.append(d)
    kakutei.append(24-d)
    time.remove(d)
  tmp=d
kakutei.append(0)
kakutei.append(24)
ans=0
if len(time)==0:
  kakutei.sort()
  minclock=12
  for j in range(len(kakutei)-1):
    minclock=min(minclock,kakutei[j+1]-kakutei[j])
  ans=max(ans,minclock)
  print(ans)
  exit()
for i in range(2**len(time)):
  clock=kakutei.copy()
  bit = format(i, "b").zfill(len(time))
  for i in range(len(bit)):
    if bit[i]=='0':
      clock.append(time[i])
    else:
      clock.append(24-time[i])
  clock.sort()
  minclock=12
  for j in range(len(clock)-1):
    minclock=min(minclock,clock[j+1]-clock[j])
  ans=max(ans,minclock)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

D=list(map(int,input().split()))
flg =False
A = [0 for i in range(13)]
A[0]=1
for i in range(N):
    j = min(D[i],24-D[i])
    if j==0:
        flg=True
        break
    elif j==12:
        if A[j]==1:
            flg=True
            break
        else:
            A[j]+=1
    else:
        if A[j]==2:
            flg=True
            break
        else:
            A[j]+=1

import copy

if flg:
    print(0)
else:
    count=0
    one_times=[]
    times=[0]
    for i in range(1,12): 
        if A[i]==1:
            count+=1
            one_times.append(i)
        if A[i]==2:
            times.append(i)
            times.append(24-i)
    if A[12]==1:
        times.append(12)
    score_max=0
    for i in range(2**count):
        t=copy.copy(times)
        for j in range(count):
            if (i//(2**j))%2==0:
                t.append(one_times[j])
            else:
                t.append(24-one_times[j])
        t.sort()
        
        score_min=24
        for j in range(len(t)):
            if j==len(t)-1:
                score_min=min(t[0]+24-t[len(t)-1],score_min)
            else:
                score_min=min(t[j+1]-t[j],score_min)
        score_max=max(score_min,score_max)
    print(score_max)

Submission status: Accepted

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

for i in range(13):
    Dcount[i] = D.count(i)
if Dcount[0] > 0 or Dcount[12] > 1: print(0)
else:
    for i in Dcount[1:12]:
        if i >= 3:
            print(0)
            break
    else:
        base = [0, 24]
        if Dcount[12] == 1: base.append(12)
        Single = []
        for i in range(1, 12):
            if Dcount[i] == 2:
                base.append(i)
                base.append(24-i)
            elif Dcount[i] == 1:
                Single.append(i)
        lensingle = len(Single)
        minmax = 0
        for i in range(2**lensingle):
            Clock = base.copy()
            bit = format(i, "b").zfill(lensingle)
            for j in range(lensingle):
                if bit[j] == "0": Clock.append(Single[j])
                else: Clock.append(24 - Single[j])
            Clock.sort()
            minsub = 24
            for j in range(1, len(Clock)):
                minsub = min(minsub, Clock[j] - Clock[j-1])
            minmax = max(minmax, minsub)
        print(minmax)

Submission status: Accepted

Solution code:
N=int(input())
from collections import defaultdict
data=defaultdict(int)
data[0]+=1
for d in map(int,input().split()):
    data[d]+=1
check=[[]]
ans=1
for d in data.keys():
    if data[d]>2:
        ans=0
        break
    checkc=check.copy()
    if data[d]==2:
        if d==0 or d==12:
            ans=0
            break
        for c in check:
            c+=[d,-1*d]
    elif data[d]==1:
        if d==0:
            for c in check:
                c.append(d)
            continue
        check=[]
        for c in checkc:
            check.append(c+[d])
            check.append(c+[-1*d])
if ans!=0:
    ans=0
    for c in check:
        c.sort()
        now=24
        for i in range(len(c)):
            now=min(now,(c[i]-c[i-1])%24)
        ans=max(now,ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
t = [0]*24
t[0] = 1
a = 1
for i in sorted(list(map(int, input().split()))):
    t[a*i] += 1
    a *= -1
for i in t:
  if i > 1:
    print(0)
    break
else:
  m = 24
  c = 1
  t += [1]
  for i in t[1:]:
    if i == 1:
      if m > c:
        m = c
      c = 1
    else:
      c += 1       
  print(m)

Submission status: Accepted

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

D1 = [0] + [(D[i] if i % 2 == 0 else 24 - D[i]) for i in range(N)]
D2 = [0] + [(D[i] if i % 2 == 0 else 24 - D[i]) for i in range(N)]

ans1 = float('inf')
ans2 = float('inf')

for i in range(N + 1):
    for j in range(i + 1, N + 1):
        ans1 = min(ans1, abs(D1[i] - D1[j]))
        ans2 = min(ans2, abs(D2[i] - D2[j]))

print(max(ans1, ans2))

Submission status: Accepted

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

if rec[0] >= 1:
    print(0)
    exit()

ans = 0
for i in range(1 << 12):
    tmp = [0]
    for j in range(13):
        if rec[j] >= 3:
            print(0)
            exit()
        elif rec[j] == 2:
            tmp.append(j)
            tmp.append(24 - j)
        elif rec[j] == 1:
            if i & 1 << j:
                tmp.append(j)
            else:
                tmp.append(24 - j)
    tmp = sorted(tmp)
    pre = tmp[0]
    num = float("inf")
    for i in range(1, len(tmp)):
        num = min(num, tmp[i] - pre)
        pre = tmp[i]

    ans = max(min(num, 24 - tmp[-1]), ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from heapq import heappush, heappop

n = ni()
d = list(li())

def isok(dur: int, d: list):
    pque = [24]
    for di in d:
        heappush(pque, di)

    cur = 0
    while pque:
        tmp = heappop(pque)
        if tmp - cur < dur and tmp >= 12:
            return False
        
        elif tmp - cur < dur:
            heappush(pque, 24-tmp)
            
        else:
            cur = tmp
        
    return True

ans = 13
for cand in range(12, -1, -1):
    if isok(cand, d):
        ans = cand
        break
        
print(ans)

Submission status: Accepted

Solution code:
import collections


def main(N, D):
    Dcounter = collections.Counter(D)
    a = [0, 24]
    b = []
    for i, v in Dcounter.items():
        if v > 2 or (i == 0 and v > 0) or (i == 12 and v > 1):
            print(0)
            return

        if v == 2:
            a.append(i)
            a.append(24-i)
        if v == 1:
            b.append(i)
    ans = 0
    for i in range(1 << len(b)):
        use = a.copy()
        for j in range(len(b)):
            if ((i >> j) & 1) == 1:
                use.append(b[j])
            else:
                use.append(24-b[j])
        use = sorted(use)
        distance = [use[i+1] - use[i] for i in range(len(use)-1)]
        ans = max(ans, min(distance))
    print(ans)

if __name__ == "__main__":
    N = int(input())
    D = list(map(int, input().split(" ")))
    main(N, D)

Submission status: Accepted

Solution code:
import sys
N=int(input())
D=[int(i) for i in input().split()]
L=[0]*13
L[0]=1
for d in D:
    L[d]+=1
for s in L:
    if s>=3:
        print(0)
        sys.exit()
if L[0]>=2 or L[12]>=2:
    print(0)
    sys.exit()
#print(L)
def check(K):
    sta=0
    #end=0
    num=10**9
    ans=10**9
    while sta<24:
        if K[sta]==1:
            ans=min(ans,num)
            num=0
        sta+=1
        num+=1
        #print(num,sta,ans)
    ans=min(num,ans)
    return ans


ans=-1
for s in range(1<<11):
    K=[0]*24
    K[0]=1
    if L[12]==1:
        K[12]=1
    for j in range(11):
        if L[j+1]==0:
            continue
        if L[j+1]==2:
            K[j+1]=1
            K[24-(j+1)]=1
            continue
        if (s&(1<<j)):
            K[j+1]=1
        else:
            K[24-(j+1)]=1
    ans=max(ans,check(K))
print(ans)