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 = list( map( int, input().split()))
D.sort()
ans = 24
c = 1
A = []
B = []
now = 0
for i in range(N):
    if D[i] == now:
        c += 1
    if c == 2:
        ans = 0
    if i%2 == 0:
        A.append(D[i])
    else:
        B.append(D[i])
s = 0
if A:
    for a in A:
        ans = min(ans, a-s)
        s = a
s = 0
if B:
    for b in B:
        ans = min(ans, b-s)
        s=b
if A and B:
    ans = min( ans, 24-A[-1]-B[-1], A[-1]+B[-1])
print(ans)

Submission status: Accepted

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(len(pos)):
            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: Accepted

Solution code:
n = int(input()) + 1
ds = [0] + sorted(map(int, input().split()))
for i in range(0, n, 2):
    ds[i] = (24 - ds[i]) % 24
print(min(abs(ds[i]-ds[j]) for i in range(n) for j in range(i+1, n)))

Submission status: Accepted

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
    c[0] = 1
    for d in D:
        c[d] += 1
    if max(c) >= 3 or c[0] >= 2 or c[12] >= 2:
        print(0)
        return

    ans = 0
    # 2 ^ nの組み合わせの全列挙
    n = 11  # 0と12は選択の余地がない
    for b in range(2 ** n):
        a = [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[-1]))

        ans = max(ans, mini)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

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

if any((D+[0,0,12]).count(i) >2 for i in range(0,13)):
    print(0)
    exit(0)


l= [0]
if D.count(12):
    l.append(12)

can_select=[]
for i in range(1,12):
    if (D).count(i) ==2:
        l.append((i))
        l.append((24 - i))

    if D.count(i)==1:
        can_select.append(i)

def change12(v):
    return min(24-v,v)

def get_closest(l):
    min_val =24
    for i in range(len(l)-1):
        min_val= min(change12(l[i+1]-l[i]),min_val)

    return min(change12(l[-1] - l[0]),min_val )

tmp_ans=0
for seed in list(itertools.product(range(2), repeat=len(can_select))):
    tmp_list = list(l)
    for i,val in enumerate(can_select):
        if seed[i]==1:
            tmp_list.append(val)
        else:
            tmp_list.append(24-val)
    tmp_list.sort()
    tmp_ans =max(tmp_ans, get_closest(tmp_list))



print(tmp_ans)

Submission status: Accepted

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

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

Submission status: Runtime Error

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: num = d[j]
            else: num = 24 - d[j]

            if (t >> num)&1:
                ret = -1
                break
        if ret == -1:
            continue

        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: Runtime Error

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

Submission status: Runtime Error

Solution code:
from itertools import product
from collections import Counter
N=int(input())
D=list(map(int, input().split()))
print(list(sorted(D)))
res=0
c=Counter(D).values()
if c[0]>0 or max(c)>2:print(0);exit()
if N>13:print(1);exit()
for b in product(*((d,24-d) for d in D)):
    d0, m= 0,1<<30
    for d in sorted(b):
        m=min((d-d0),24-(d-d0),m)
        d0=d
    m=min(d0,24-d0,m)
    res = max(res,m)
print(res)

Submission status: Runtime Error

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
from fractions import Fraction
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
INF = float("inf")
MOD = 10**9+7

N = int(input())
A = list(map(int,input().split()))
d = defaultdict(int)

if 0 in A:
    print(0)
    exit()

for i in range(N):
    d[A[i]] += 1

T = [0]
W = []
for k in d.keys():
    if d[k] >= 2:
        T.append(k)
        T.append(24-k)
    else:
        W.append(k)
#print(T,W)

ans = []

w = len(W)
K = 2 # 選択肢の数
for i in range(K**w):
    TF=[0]*w

    for j in range(w):
        TF[w-j-1] = i % K
        i //= K
    
    l = copy.deepcopy(T)
    for j in range(w):
        if TF[j] == 1:
            l.append(24-W[j])
        else:
            l.append(W[j])
    
    l.sort()
    m = 24
    for j in range(1,N+1):
        m = min(m,l[j] - l[j-1])
    ans.append(m)

print(max(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
d = sorted(list(map(int, input().split())) + [0])

bit, cnt = [0] * 24, 1
for num in d:
    bit[(num - 12) * cnt + 12] += 1
    cnt *= -1

if any(i > 1 for i in bit):
    print(0)
    exit()
ans = [i for i, li in enumerate(bit * 2) if li == 1]
s = min(ans[i] - ans[i - 1] for i in range(1, len(ans)))
print(s)

Submission status: Runtime Error

Solution code:
from itertools import product
import random

def solve(n,dl):
    # n = int(input())
    # dl = list(map(int, input().split()))

    if 0 in dl:
        return 0

    doubles = []
    dl1 = []
    dl2 = []
    for i in range(1,13):
        i_cnt = dl.count(i)
        if i_cnt > 2:
            return 0
        elif i_cnt == 2:
            dl2.append(24-i)
            dl2.append(i)
        elif i_cnt == 1:
            dl1.append(i)        

    ans = 0
    ite = list(product(range(2),repeat=len(dl1)))
    for pattern in ite:
        dl24 = [0,24]
        for i, v in enumerate(pattern):
            if v == 1:
                dl24.append(24-dl1[i])
            else:
                dl24.append(dl1[i])
        dl24 = dl24 + dl2
        dl24.sort()
        diff_min = 24
        # print(dl24)
        if len(dl24) == 1:
            ans = max(ans, min(dl24[0], 24-dl24[0]))
        else:
            for a,b in zip(dl24[:-1],dl24[1:]):
                diff = b-a
                if diff != 24
                    diff_min = min(diff_min,diff)
            ans = max(diff_min,ans)
    return ans


def solve2(n,dl):
    ite = list(product(range(2),repeat=len(dl)))
    ans = 0
    for pattern in ite:
        dl_new = [0]
        for i, v in enumerate(pattern):
            if v == 1:
                dl_new.append(24-dl[i])
            else:
                dl_new.append(dl[i])
        dl_new.sort()
        diff_min = 24
        for a,b in zip(dl_new[:-1], dl_new[1:]):
            diff = b-a
            diff_min = min(diff_min, diff)
        ans = max(ans,diff_min)
    return ans


if __name__ == "__main__":
    n = int(input())
    dl = list(map(int, input().split()))
    ans = solve(n,dl)
    print(ans)
    # for _ in range(10):
    #     n = random.randint(1,10)
    #     dl = [random.randint(1,12) for _ in range(n)]
    #     ans1 = solve(n, dl[:])
        # ans2 = solve2(n,dl[:])
        # if ans1 != ans2:
        #     print(n)
        #     print(dl)
        #     print(ans1)
        #     print(ans2)
        #     print('----')

Submission status: Runtime Error

Solution code:
from itertools import product
import numpy as np

n = int(input())
dl = list(map(int, input().split()))

if 0 in dl:
    print(0)
    exit()


doubles = []
dl1 = []
dl2 = []
for i in range(1,13):
    i_cnt = dl.count(i)
    if i_cnt > 2:
        print(0)
        exit()
    elif i_cnt == 2:
        dl2.append(24-i)
        dl2.append(i)
    elif i_cnt == 1:
        dl1.append(i)        


ans = 0
ite = list(product(range(2),repeat=len(dl1)))
for pattern in ite:
    dl24 = []
    for i, v in enumerate(pattern):
        if v == 1:
            dl24.append(24-dl1[i])
        else:
            dl24.append(dl1[i])
    dl24 = dl24 + dl2
    dl24.sort()
    diff = list(np.diff(dl24))
    ans = max(min(diff),ans)

print(ans)

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/tenka1-2014-qualb/tasks/tenka1_2014_qualB_b
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()

def main():
  n=I()
  s=S()
  k=len(s)
  l=[S() for _ in range(n)]

  dp=[0]*(k+1)
  dp[0]=1
  for i in range(k+1):
    for j,x in enumerate(l):
      lnx=len(x) # 文字の長さ
      if i-lnx<0: # 長すぎたら当然一致しないので
        continue
      
      # マッチするか見る
      if x==s[i-lnx:i]:
        dp[i]+=dp[i-lnx]
        dp[i-lnx]%mod

  return dp[k]%mod

# main()
print(main())

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline())
D = list(map(int, sys.stdin.readline().split()))

times = [0] * 24
for d in D:
    times[d] += 1

if max(times) > 2 or times[0] == 2 or times[12] == 2:
    print(0)
else:
    ans = 0
    # 1 - 11時間の時差の場合、Diと24 - Diの両方を試す
    for i in range(2**11):
        positions = []
        if times[0] == 1:
            positions.append(0)
        for j in range(11):
            if times[j+1] == 2:
                positions.append(j+1)
                positions.append(24 - j-1)
            elif times[j+1] == 1:
                if (i >> j) & 1:
                    positions.append(24 - j-1)
                else:
                    positions.append(j+1)
        if times[12] == 1:
            positions.append(12)
        positions.sort()
        # print(positions)
        lp = len(positions)
        d = float("inf")
        for p in range(lp):
            # 0をまたぐ場合がある
            if p == lp -1:
                diff = 24 - positions[p] + positions[0]
            else:
                diff = positions[p+1] - positions[p]
            d = min(d, diff if diff != 24 else 0)
        ans = max(ans, d)

print(ans)

Submission status: Runtime Error

Solution code:
import sys

N = int(sys.stdin.readline())
D = list(map(int, sys.stdin.readline().split()))

times = [0] * 24
for d in D:
    times[d] += 1

if max(times) > 2 or times[0] == 2 or times[12] == 2:
    print(0)
else:
    ans = 0
    # 1 - 11時間の時差の場合、Diと24 - Diの両方を試す
    for i in range(2**11):
        positions = []
        if times[0] == 1:
            positions.append(0)
        for j in range(11):
            if times[j+1] == 2:
                positions.append(j+1)
                positions.append(24 - j-1)
            elif times[j+1] == 1:
                if (i >> j) & 1:
                    positions.append(24 - j-1)
                else:
                    positions.append(j+1)
        if times[12] == 1:
            positions.append(12)
        positions.sort()
        # print(positions)
        lp = len(positions)
        d = float("inf")
        for p in range(lp):
            # 0をまたぐ場合がある
            diff = abs(positions[(p+1) % lp] - positions[p])
            d = min(d, diff)
        ans = max(ans, d)

print(ans)

Submission status: Runtime Error

Solution code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<tgmath.h>
#include<stdbool.h>
#include<sys/queue.h>
#include<time.h>
#define ll long long
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
int cmp(const int *a, const int *b){return *a - *b;}

int n, d[55];

int main(){
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%d", &d[i]);
    }
    d[n] = 0; n++;
    qsort(d, n, 4, cmp);
    for(int i=1; i<n; i++){
        if(d[i-1] > 0)d[i] *= -1;
    }
    qsort(d, n, 4, cmp);
    int ans = d[1] - d[0];
    for(int i=1; i<n; i++){
        ans = min(ans, d[i] - d[i-1]);
    }
    printf("%d\n", ans);
    return 0;
}

Submission status: Runtime Error

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

from collections import defaultdict
d = defaultdict(int)

d[0] = 1
for d_ in ds:
    d[d_] += 1
    if d_ == 12 and d[d_] == 2:
        print(0)
        exit()
    elif d_ == 0 and d[d_] == 2: 
        print(0)
        exit()
    elif d[d_] == 3:
        print(0)
        exit()

def calcmin(tmp):
    n = len(tmp)
    ret = 1 << 50
    for i in range(n):
        for j in range(i+1,n):
            ret = min(ret, abs(tmp[i]-tmp[j]))
    return ret

two = []
x = []
for k in d.keys():
    if d[k] == 2:
        two.append(k)
        two.append(24-k)
    if d[k] == 1:
        x.append(k)
two = list(set(two))
n = len(x)

ans = 0
for i in range(1 << n):
    tmp[] = x[:]
    for j in range(n):
        if i & (1 << j):
            tmp[j] = 24-x[j]
    tmp.extend(two)
    ans = max(calcmin(tmp), ans)
print(ans)

Submission status: Runtime Error

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

from collections import defaultdict
d = defaultdict(int)

d[0] = 1
for d_ in ds:
    d[d_] += 1
    if d_ == 12 and d[d_] == 2:
        print(0)
        exit()
    elif d_ == 0 and d[d_] == 2: 
        print(0)
        exit()
    elif d[d_] == 3:
        print(0)
        exit()

def calcmin(tmp):
    n = len(tmp)
    ret = 1 << 50
    for i in range(n):
        for j in range(i+1,n):
            ret = min(ret, abs(tmp[i]-tmp[j]))
    return ret

two = []
x = []
for k in d.keys():
    if d[k] == 2:
        two.append(k)
        two.append(24-k)
    if d[k] == 1:
        x.append(k)
two = list(set(two))
n = len(x)

ans = 0
for i in range(1 << n):
    tmp[:] = x[:]
    for j in range(n):
        if i & (1 << j):
            tmp[j] = 24-x[j]
    tmp.extend(two)
    ans = max(calcmin(tmp), ans)
print(ans)

Submission status: Runtime Error

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

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


Times = [0]*24
ones =[]
for i,t in enumerate(T):
    if t>=3 or t==2 and (i==0 or i==12):
        print(0)
        exit()
    elif t==2:
        Times[i] = 1
        Times[24-i] = 1
    elif t==1:
        ones.append(i)

ans = 0
L = len(ones)
for i in range(2**L):
    stats = [False]*L
    temp = i
    for j in range(L):
        stats[j] = temp&1
        temp >>= 1
    
    newTime = [t for t in Times]
    res = 10**9
    for j,t in enumerate(ones):
        nt = t if stats[j] else 24-t
        newTime[nt] += 1
    
    gaps = []
    for j,t in enumerate(newTime):
        if t==1:
            gaps.append(j)
    for j in range(len(gaps)-1):
        res = min(res,gaps[j+1]-gaps[j])
    if len(gaps)>=2:
        res = min(res, gaps[0]+24-gaps[1])
    ans = max(ans, res)
print(ans)

Submission status: Runtime Error

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

N = ir()
D = lr()

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

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

possible_lists = [[]]

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

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

print(Max)

Submission status: Runtime Error

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


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

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

    size = len(m)
    ans = 0

    g = lambda x: min(x, 24-x)

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

        a = min(map(g, c))
        b = min(f(j, k) for j, k in combinations(range(N), 2))
        ans = max(ans, min(a, b))

    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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


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

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

    size = len(m)
    ans = 0

    g = lambda x: min(x, 24-x)

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

        a = min(map(g, c))
        b = min(f(j, k) for j, k in combinations(range(N), 2))
        ans = max(ans, min(a, b))

    print(ans)

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
from itertools import product
from collections import Counter


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

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

    size = len(m)
    ans = 0

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

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

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

Submission status: Runtime Error

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
D=[int(i) for i in input().split()]
if n>=24:
  print(0)
else:
  ans=0
  for i in range(2**(n-1)):
    C=[0,D[0],24]
    for j in range(n-1):
      if i&(1<<j):
        C.append(24-D[j+1])
      else:
        C.append(D[j+1])
    C.sort()
    cur=24
    for j in range(n):
      cur=min(cur,C[j+1]-C[j])
    ans=max(ans,cur)
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define INF bit(60)
#define pb push_back
#define mod 1000000007

using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

#define FACSIZE 200002

ll invfac[FACSIZE];
ll fac[FACSIZE];

template <class T>
bool chmax(T &, const T &);
template <class T>
bool chmin(T &, const T &);
ll gcd(ll, ll);
ll powLL(ll x, ll y);

ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
ll comb(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void fact_init();

int main(void)
{
  ll n;
  cin >> n;
  vector<ll> D(n);
  rep(i, n) cin >> D[i];

  vector<ll> vec;
  vec.push_back(0);
  vec.push_back(D[0]);

  for (ll i = 1; i < n; i++)
  {
    ll d1, d2;
    d1 = D[i];
    d2 = (24 - D[i]) % 24;
    ll min1 = 100, min2 = 100;
    for (auto x : vec)
    {
      ll diff = abs(x - d1);
      min1 = min(diff, min1);
    }
    for (auto x : vec)
    {
      ll diff = abs(x - d2);
      min2 = min(diff, min2);
    }
    if (min1 < min2)
      vec.push_back(d2);
    else
      vec.push_back(d1);
  }

  ll ans = 100;
  rep(i, n + 1)
  {
    for (ll j = i + 1; j < n + 1; j++)
    {
      ans = min(ans, abs(vec[i] - vec[j]) % 24);
    }
  }
  cout << ans << endl;

  return 0;
}

ll mod_pow(ll x, ll r)
{
  if (r == 0)
    return 1;
  else if (r == 1)
    return x % mod;
  else if (r % 2 == 0)
  {
    ll t = mod_pow(x, r / 2) % mod;
    return mod_mul(t, t);
  }
  else
  {
    ll t = mod_pow(x, r / 2) % mod;
    ll k = mod_mul(t, t);
    return (k % mod) * (x % mod);
  }
}
ll mod_add(ll a, ll b)
{
  return ((a % mod) + (b % mod)) % mod;
}

ll mod_mul(ll a, ll b)
{
  return ((a % mod) * (b % mod)) % mod;
}

ll mod_div(ll a, ll b)
{
  return mod_mul(a, mod_pow(b, mod - 2));
}

void fact_init()
{
  make_fact(FACSIZE - 1);
  make_invfact(FACSIZE);
}

void make_fact(ll n)
{
  fac[0] = 1;
  rep(i, n)
  {
    fac[i + 1] = mod_mul(fac[i], i + 1);
  }
}

void make_invfact(ll n)
{
  invfac[n] = mod_pow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
  {
    invfac[i] = mod_mul(invfac[i + 1], i + 1);
  }
}

ll comb(ll n, ll r)
{

  return mod_mul(mod_mul(fac[n], invfac[r]), invfac[n - r]);
}
template <class T>
bool chmax(T &a, const T &b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
  if (b < a)
  {
    a = b;
    return 1;
  }
  return 0;
}

ll qp(ll a, ll b)
{
  ll ans = 1LL;
  do
  {
    if (b & 1LL)
      ans = 1LL * mod_mul(ans, a) % mod;
    a = 1LL * mod_mul(a, a) % mod;
  } while (b >>= 1LL);
  return ans;
}
ll qp(ll a, ll b, ll mo)
{
  ll ans = 1LL;
  do
  {
    if (b & 1LL)
      ans = 1LL * (ans % mo) * (a % mo);
    a = 1LL * (a % mo) * (a % mo);
  } while (b >>= 1LL);
  return ans;
}
ll gcd(ll a, ll b)
{
  return b ? gcd(b, a % b) : a;
}
ll powLL(ll x, ll y)
{
  ll ans = 1LL;
  for (ll i = 0LL; i < y; i++)
    ans *= x;
  return ans;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

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

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

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

Submission status: Runtime Error

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

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

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

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

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

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